KxSystems / embedPy

Allows the kdb+ interpreter to call Python functions
https://code.kx.com/q/interfaces
Apache License 2.0
87 stars 42 forks source link

Lists of objects aren't being unwrapped recursively? #44

Closed mcg1969 closed 5 years ago

mcg1969 commented 6 years ago

I was playing with some advanced IPython widget usage that requires passing in lists whose first element is a Python object. Unfortunately, while bare Python objects are automatically unwrapped on the way back to Python, that's not true of Python objects in a list. I did discover that you can force them to be unwrapped, but it seems better if the embedPy functionality would do this recursively on lists and other composite objects.

p)class MyObject:
    pass
p)def bar():
    return MyObject()
p)def foo(x):
    if isinstance(x, list):
        print('[' + (', '.join(repr(y) for y in x)) + ']')
    else:
        print(repr(x))
foo:.p.get[`foo]
bar:.p.get[`bar]
foo(bar[])
foo((bar[];1))
foo((bar[]`.;1))
foo("a";1)

Output:

<__main__.MyObject object at 0x1196b2f98>
[<built-in method q) of PyCapsule object at 0x1155632d0>, 1]
[<__main__.MyObject object at 0x1196b2f98>, 1]
['a', 1]

What seems sensible is that the to have to use that back-tick notation, so that the second line behaves the way the third one does.

awilson-kx commented 6 years ago

Hey Michael

This was a design/performance decision, as recursive type checking proved too expensive.

When passing arguments using embedPy

Unwrapping of embedPy objects inside q 'containers' (lists, dictionaries, tables etc) will not take place by default. In most cases, no conversion is even necessary (as the argument consists solely of q data) and the performance hit is too great.

mcg1969 commented 6 years ago

Understood. I'd like to suggest, then, that the documentation offer an explicit caution about this (if it doesn't already; I may have missed it!). You've made it quite easy of course to use the backtick to do a "full" unwrapping, so I really don't find it too inconvenient, it's just good to know it's necessary sometimes.

jhanna-kx commented 6 years ago

Added .p.xunwrap to do this recursive unwrap in 1.2.0 release