2021.10.07 08:29 PM
Hey Team,
I've been using an object in Python with Q via embedPy.
In using this, I've found that it appears that lists of floats from Q is represented by embedPy in Python as a numpy array of floats.
This was an issue in my case because the object I used was a little picky and wanted a vanilla python list.
I got around this by use of a function in Python that I called from Q in such a way asIn the end to convert the numpy array back to a list after the data had been projected into Python from Q.
The work I'm doing is not particularly reliant on speed so this isn't a big deal. However, it struck me this seems a little convoluted. Is there any functionality available to default the projection of the list of floats in Q to a list of floats in Python when using embedPy?
Regards,
Simon
2021.10.10 03:37 PM - edited 2021.10.10 03:38 PM
Hi Simon,
Please find some insight from our team as follows;
embedpy defaults to numpy arrays because that makes a lot more sense for the typical usage pattern.
numpy arrays behave enough like lists that this is usually not a problem.
In a rare case that it is, it's reasonable to do explicit conversion by calling list(). it's exposed to q as .p.list, so e.g. to pass a float vector to print() as a python list instead of a numpy array,
q).p.print .p.list 1 2 3f
[1.0, 2.0, 3.0]
That's exactly the same as doing that in python, but saves defining a function
q).p.eval["lambda x:print(list(x))";1 2 3f]
[1.0, 2.0, 3.0]
Thanks for adding your question on the portal!
Kind regards,
David
2021.10.08 09:31 AM
Hello Simon,
Great observation here! Thank you for sharing this with the community. 🙌
We're working on an update for you and will relay the feedback as soon as possible.
Kind regards,
Leah
2021.10.10 03:37 PM - edited 2021.10.10 03:38 PM
Hi Simon,
Please find some insight from our team as follows;
embedpy defaults to numpy arrays because that makes a lot more sense for the typical usage pattern.
numpy arrays behave enough like lists that this is usually not a problem.
In a rare case that it is, it's reasonable to do explicit conversion by calling list(). it's exposed to q as .p.list, so e.g. to pass a float vector to print() as a python list instead of a numpy array,
q).p.print .p.list 1 2 3f
[1.0, 2.0, 3.0]
That's exactly the same as doing that in python, but saves defining a function
q).p.eval["lambda x:print(list(x))";1 2 3f]
[1.0, 2.0, 3.0]
Thanks for adding your question on the portal!
Kind regards,
David
2021.10.10 06:33 PM
Thanks David and Leah - a good result. As you say not normally a problem but for a fussy application on the python side.
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.