Skip to content

pykx.q Library Reference Card

This page documents the functions found in the pykx.q q library that are available.

This library can be installed by calling a helper function within PyKX, this function will move all the required files and libraries into your QHOME directory.

import pykx as kx
kx.install_into_QHOME()

.pykx.pyevalNoRet[x]

Evaluates a CharVector as python code and do not return the result.

q).pykx.pyevalNoRet"1+1"

.pykx.pyeval[x]

Evaluates a CharVector as python code and return the result as a K type.

q).pykx.pyeval"1+1"
2
q).pykx.pyeval["lambda x: x + 1"][5]
6

.pykx.eval[x]

Evaluates a CharVector as python code and return the result as a wrapped kx.Foreign type.

q).pykx.eval"1+1"
{[f;x].pykx.i.pykx[f;x]}[foreign]enlist
q).pykx.eval["1+1"]`
2
q).pykx.eval["lambda x: x + 1"][5]`
6

.pykx.pyexec[x]

Executes a CharVector as python code.

q).pykx.pyexec"1+1"
q).pykx.pyexec"a = 1+1"
q).pykx.pyeval"a"
2

.pykx.console[]

Opens an interactive python REPL, similar to launching python from the command line.

q).pykx.console[]
>>> 1+1
2
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

.pykx.wrap[f]

This function takes a foreign and wraps it into a callable q object. Most functions in this library return a pre-wrapped foreign object.

q)a:.pykx.pyeval"pykx.Foreign(5)"
q)a
foreign
q)b:.pykx.wrap a
q)b
{[f;x].pykx.i.pykx[f;x]}[foreign]enlist
q)b`
5

Applying the argument `. to a wrapped PyKX foreign will return the unwrapped foreign object.

q)a:.pykx.eval"1+1"
q)a`.
foreign

Applying the argument ` to a wrapped PyKX foreign will return the foreign object converted into an analogous q object.

q)a:.pykx.eval"1+1"
q)a`
2

Applying the argument `:x to a wrapped PyKX foreign is shorthand for calling .pykx.getattr[foreign; `x]. It returns an instance of a wrapped foreign object with the value obtained by calling the getattr(f, 'x') function within Python.

# a.py
class A:
    x = 'foo'
    y = b'bar'

q).pykx.pyexec"import a"
q)a:.pykx.eval"a.A()"
q)a`:x
{[f;x].pykx.i.pykx[f;x]}[foreign]enlist
q)a[`:x]`
`foo
q)a[`:y]`
"bar"
This wrapped object can also be used to call the foreign object as a python function. Calling a wrapped foreign object with generic null as the only parameter calls the python object with no arguments.

q)a:.pykx.eval"[1, b'foo', 'bar']"
q)a`
1
"foo"
`bar
// Get the len() method of the python object
q)alen:a`:__len__
// Call the len() method with no arguments
q)alen[::]`
3

It can also take multiple arguments.

q)a:.pykx.eval"lambda x, y, z: bytes(f'{x}-{y}-{z}', 'utf-8')"
q)a[1;2;3]`
"1-2-3"

If the last argument provided is a dictionary it will be used as key word arguments for the function.

# a.py
def foo(*args, **kwargs):
    print(args)
    print(kwargs)
q).pykx.pyexec"import a"
q)a:.pykx.eval"a.foo"
q)a[`bar; "baz"; 3; (`a`b`c)!(5; 7.5; `apple)]`
(pykx.SymbolAtom(pykx.q('`bar')), pykx.CharVector(pykx.q('"baz"')), pykx.LongAtom(pykx.q('3')))
{'a': 5, 'b': 7.5, 'c': 'apple'}

Note: You cannot call a function that takes a single argument and pass in a SymbolAtom. Due to the overloading of this wrapped object a single SymbolAtom argument will be used to try and get an attribute or the underlying foreign not call a function.

.pykx.getattr[f; x]

Get an attribute of a python foreign object, returns a foreign wrapping None if the attribute does not exist. Equivalent to calling Python's getattr(f, 'x') function.

Note: The wrapped foreign objects provide a shorthand version of calling .pykx.getattr.

q)a: .pykx.eval"list(range(10))"
q)alen: .pykx.wrap .pykx.getattr[a`.; `$"__len__"]
// identical to alen: a[`:__len__]
q)alen[::]` // Identity is used to call a function with no arguments
10
q).pykx.wrap[.pykx.getattr[a`.; `asdasd]]`
::

.pykx.setattr[f; a; x]

Set an attribute of a python foreign object. Equivalent to calling Python's setattr(f, a, x) function.

q)a[`:r]`
q).pykx.setattr[a`.; `r; 5]
`
q)a[`:r]`
5

Note: When the attribute is set within python it is converted into a python type. (This is done by calling .py()/.np()/.pd()/.pa() on the wrapped K type). You can also specify the type you would like it to be converted into by modifying the attribute parameter. If no type is specified the object will by default convert into a numpy object.

q).pykx.setattr[a`.; `r:py; (1 2 3 4 5)]
>>> a.r
[1, 2, 3, 4, 5]

q).pykx.setattr[a`.; `r:np; (1 2 3 4 5)]
>>> a.r
array([1, 2, 3, 4, 5])

q).pykx.setattr[a`.; `r:pd; ([] a: 10?10; b:10?10f)]
>>> a.r
   a         b
0  4  3.927524
1  6  5.170911
2  6  5.159796
3  1  4.066642
4  8  1.780839
5  5  3.017723
6  4  7.850330
7  9  5.347096
8  2  7.111716
9  7  4.115970

q).pykx.setattr[a`.; `r:pa; ([] a: 10?10; b:10?10f)]
>>> a.r
pyarrow.Table
a: int64
b: double
----
a: [[1,7,2,4,5,4,2,7,8,5]]
b: [[9.499750286340714,4.390809948090464,5.759051400236785,5.919004327151924,8.481566500850022,3.8905602344311774,3.9154298044741154,0.8123545907437801,9.36750340508297,2.7821216685697436]]

Note: If setting the attribute fails an integer will be returned denoting the error code of the call to setattr.

q)a:.pykx.eval"[1, 2, 3]"
q).pykx.setattr[a`.; `r; 5]
-1i

.pykx.repr[f]

Returns the result of calling the python function repr() on the object stored within the foreign. The result is returned as a CharVector type.

q)a: .pykx.eval"1+1"
q).pykx.repr a`.
,"2"
q)a: .pykx.eval"'hello world'"
q).pykx.repr a`.
"'hello world'"

.pykx.toq[f]

Convert an unwrapped PyKX foreign object into an analogous q type.

Note: The wrapped foreign objects provide a shorthand version of calling .pykx.toq.

q)a:.pykx.eval["1+1"]`. // return the value as an unwrapped foreign
q)a
foreign
q).pykx.toq a
2