Skip to content

Registering Custom Conversions

The purpose of this functionality is to provide an extension mechanism for PyKX allowing users to register extension logic for handling conversions from Pythonic types to create PyKX objects when using the pykx.toq function or any internal functionality which makes use of this conversion mechanism.


Functionality for the registration of conversion functions between PyKX and Python


py_toq(py_type, conversion_function, *, overwrite=False)

Register conversion logic for a specified Python type when converting it to a PyKX object.


The return of registered functions should be a valid pykx object type returns of Pythonic types can result in unexpected errors


Application of this functionality is at a users discretion, issues arising from overwritten default conversion types are unsupported


Name Type Description Default

The type signature used for determining when a conversion should be triggered for PyKX, in particular this will check the type(x) on incoming data to determine this.


The function/callable which will be used to convert the supplied object to a PyKX object specified by the user.

overwrite bool

If a definition for this type already exists should it be overwritten by default this is set to False to avoid accidental overwriting of conversion logic used within the library



Type Description

A None object on successful invocation


Register conversion logic for complex Python object types

>>> import pykx as kx
>>> kx.toq(complex(1, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pykx/toq.pyx", line 2543, in pykx.toq.ToqModule.__call__
  File "pykx/toq.pyx", line 245, in pykx.toq._default_converter
TypeError: Cannot convert <class 'complex'> '(1+2j)' to K object
>>> def complex_toq(data):
...    return kx.toq([data.real, data.imag])
>>> kx.register.py_toq(complex, complex_toq)
>>> kx.toq(complex(1, 2))
pykx.FloatVector(pykx.q('1 2f'))

Register conversion logic for complex Python objects overwriting previous logic above

>>> def complex_toq_upd(data):
...    return kx.q('{`real`imag!(x;y)}', kx.toq(data.real), kx.toq(data.imag)
>>> kx.register.py_toq(complex, complex_toq_upd, overwrite=True)
>>> kx.toq(complex(1, 2))
real| 1
imag| 2