KxSystems / pyq

PyQ — Python for kdb+
http://code.kx.com/q/interfaces
Apache License 2.0
190 stars 49 forks source link

Boolean comparison not implemented #92

Closed antipisa closed 4 years ago

antipisa commented 5 years ago

Could we allow for inequality operations from the python side?

>>> K(2) >  1
True
>>> K(2) > K(1)
True

This works:

>>> q('>',q.til(5),1)
k('00111b')

But:

>>> q.til(5) >  1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/envs/dev/lib/python3.6/site-packages/pyq/__init__.py", line 612, in dunder
    raise NotImplementedError
NotImplementedError
abalkin commented 5 years ago

There are two "natural" choices: map python < and > to q < and > or map them to lexicographical compare so that K([a, ...]) > K([b, ...]) is the same as [a, ...] > [b, ...]. So far we have followed the Zen of Python: "In the face of ambiguity, refuse the temptation to guess."

Given that == is mapped to ~ rather than = by a deliberate decision, mapping < and > to vector operation would make pyq equally confusing to python and to q programmers.

How useful is lexicographical comparison?

antipisa commented 5 years ago

What's the reason you chose to do it this way, rather than following the same semantic as both q and numpy? Python lists are terribly difficult to work with, and without the basic vector comparison operators, manipulating K vectors in python is very painful. If the goal was to give python the full power of q, i think it’s crucial to supporting vector comparison.

Also, vector arithmetic seems to work just fine, when they’re also not supported by python lists. i.e.

>>> K([True,False,True]) | K([False,True,False])
k('111b’)
>>> [True,False,True] | [False,True,False]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'list' and ‘list'
>>> q.til(3)+q.til(3)
k('0 2 4’)
>>> [0,1,2]+[0,1,2]
[0, 1, 2, 0, 1, 2]

Not that I'm complaining (please don’t break it). It seems very inconsistent to support arithmetic operators, but but not the comparison operators.

github-actions[bot] commented 4 years ago

Stale issue message