PyQ

PyQ brings the Python programming language to the kdb+ database. It allows developers to seamlessly integrate Python and q codes in one application. This is achieved by bringing the Python and q interpreters in the same process so that codes written in either of the languages operate on the same data. In PyQ, Python and q objects live in the same memory space and share the same data.

Quick start

First, make sure that PyQ is installed and up-to-date. Start an interactive session:

$ pyq

Import the q object from pyq and the date class from the standard library module datetime:

>>> from pyq import q
>>> from datetime import date

Drop to the q) prompt and create an empty trade table:

>>> q()  
q)trade:([]date:();sym:();qty:())

Get back to the Python prompt and insert some data into the trade table:

q)\
>>> q.insert('trade', (date(2006,10,6), 'IBM', 200))
k(',0')
>>> q.insert('trade', (date(2006,10,6), 'MSFT', 100))
k(',1')

(In the following we will skip q() and \ commands that switch between q and Python.)

Display the result:

>>> q.trade.show()
date       sym  qty
-------------------
2006.10.06 IBM  200
2006.10.06 MSFT 100

Define a function in q:

q)f:{[s;d]select from trade where sym=s,date=d}

Call the q function from python and pretty-print the result:

>>> x = f('IBM', date(2006,10,6))
>>> x.show()
date       sym qty
------------------
2006.10.06 IBM 200

For an enhanced interactive shell, use pyq to start IPython:

$ pyq -m IPython

See the ipython section for details.