Skip to content

Q & Embedded Q

pykx.Q

pykx.Q()
See Also

Abstract base class for all interfaces between Python and q.

_register

_register(name=None, path=None)

Obtain the definitions from a q/k script.

Parameters:

Name Type Description Default
name Optional[str]

Name of the context to be loaded. If path is not provided, a file whose name matches will be searched for, and loaded if found. If path is provided, name will be used as the name of the context that the script at path is executed in.

None
path Optional[Union[Path, str]]

Path to the script to load. If name is not provided, it will default to the filename sans extension.

None

Returns:

Type Description
str

The attribute name for the newly loaded module.

sql

sql(query, *args)

Wrapper around the KX Insights Core ANSI SQL interface.

Parameters:

Name Type Description Default
query str

The SQL query, using KX Insights Core SQL, documented at https://code.kx.com/insights/core/sql.html

required
*args Any

The arguments for the query, which will be interpolated into the query. Each argument will be converted into a pykx.K object.

()

Returns:

Type Description
'K'

The result of the evaluation of query with args interpolated.

Avoid interpolating the table into the query when running over IPC.

It's common to interpolate a pykx.Table object into the query as '$1'. This works well when running embedded within the process, but when the Q instance is an IPC connection this will result in the entire table being sent over the connection, which will negatively impact performance. Instead, when running over IPC, write the name of the table (as defined in the connected q server) directly into the query.

Examples:

Query a table by name:

q.sql('select * from trades')

Query a pykx.Table instance by interpolating it in as the first argument:

q.sql('select * from $1', trades) # where `trades` is a `pykx.Table` object

Query a table using interpolated conditions:

from datetime import date
q.sql('select * from trades where date = $1 and price < $2', date(2022, 3, 31), 32.1)

pykx.EmbeddedQ

pykx.EmbeddedQ()

Bases: Q

Interface for q within the current process; can be called to execute q code.

__call__

__call__(query, *args, wait=None, sync=None)

Run code in the q instance.

Parameters:

Name Type Description Default
query Union[str, bytes, wrappers.CharVector]

The code to run in the q instance.

required
*args Any

Arguments to the q query. Each argument will be converted into a pykx.K object. Up to 8 arguments can be provided, as that is the maximum supported by q.

()
wait Optional[bool]

Keyword argument provided solely for conformity with pykx.QConnection. All queries against the embedded q instance are synchronous regardless of what this parameter is set to. Setting this keyword argument to False results in q generic null (::) being returned, so as to conform with pykx.QConnection. This conformity enables one to call any pykx.Q instance the same way regardless of whether it is a pykx.EmbeddedQ or pykx.QConnection instance. For cases where the query executing asynchronously (and returning after it has been issued, rather than after is is done executing) is actually important, one can discriminate between pykx.Q instances using isinstance as normal.

None

Returns:

Type Description
wrappers.K

The value obtained by evaluating the query within the current process.

Raises:

Type Description
LicensedException

Attempted to execute q code outside of licensed mode.

TypeError

Too many arguments were provided - q queries cannot have more than 8 parameters.