qpython package

qpython.qconnection module

exception qpython.qconnection.QConnectionException

Bases: exceptions.Exception

Raised when a connection to the q service cannot be established.

exception qpython.qconnection.QAuthenticationException

Bases: qpython.qconnection.QConnectionException

Raised when a connection to the q service is denied.

class qpython.qconnection.MessageType

Bases: object

Enumeration defining IPC protocol message types.

ASYNC = 0
SYNC = 1
RESPONSE = 2
class qpython.qconnection.QConnection(host, port, username=None, password=None, timeout=None, encoding='latin-1', reader_class=None, writer_class=None, **options)

Bases: object

Connector class for interfacing with the q service.

Provides methods for synchronous and asynchronous interaction.

The QConnection class provides a context manager API and can be used with a with statement:

with qconnection.QConnection(host = 'localhost', port = 5000) as q:
    print(q)
    print(q('{`int$ til x}', 10))
Parameters:
  • host (string) - q service hostname
  • port (integer) - q service port
  • username (string or None) - username for q authentication/authorization
  • password (string or None) - password for q authentication/authorization
  • timeout (nonnegative float or None) - set a timeout on blocking socket operations
  • encoding (string) - string encoding for data deserialization
  • reader_class (subclass of QReader) - data deserializer
  • writer_class (subclass of QWriter) - data serializer
Options:
  • raw (boolean) - if True returns raw data chunk instead of parsed data, Default: False
  • numpy_temporals (boolean) - if False temporal vectors are backed by raw q representation (QTemporalList, QTemporal) instances, otherwise are represented as numpy datetime64/timedelta64 arrays and atoms, Default: False
  • single_char_strings (boolean) - if True single char Python strings are encoded as q strings instead of chars, Default: False
protocol_version

Retrieves established version of the IPC protocol.

Returns:integer – version of the IPC protocol
open()

Initialises connection to q service.

If the connection hasn’t been initialised yet, invoking the open() creates a new socket and performs a handshake with a q service.

Raises:QConnectionException, QAuthenticationException
close()

Closes connection with the q service.

is_connected()

Checks whether connection with a q service has been established.

Connection is considered inactive when:
  • it has not been initialised,
  • it has been closed.
Returns:booleanTrue if connection has been established, False otherwise
query(msg_type, query, *parameters, **options)

Performs a query against a q service.

In typical use case, query is the name of the function to call and parameters are its parameters. When parameters list is empty, the query can be an arbitrary q expression (e.g. 0 +/ til 100).

Calls a anonymous function with a single parameter:

>>> q.query(qconnection.MessageType.SYNC,'{til x}', 10)

Executes a q expression:

>>> q.query(qconnection.MessageType.SYNC,'til 10')
Parameters:
  • msg_type (one of the constants defined in MessageType) - type of the query to be executed
  • query (string) - query to be executed
  • parameters (list or None) - parameters for the query
Options:
  • single_char_strings (boolean) - if True single char Python strings are encoded as q strings instead of chars, Default: False
Raises:

QConnectionException, QWriterException

sendSync(query, *parameters, **options)

Performs a synchronous query against a q service and returns parsed data.

In typical use case, query is the name of the function to call and parameters are its parameters. When parameters list is empty, the query can be an arbitrary q expression (e.g. 0 +/ til 100).

Executes a q expression:

>>> print(q.sendSync('til 10'))
[0 1 2 3 4 5 6 7 8 9]

Executes an anonymous q function with a single parameter:

>>> print(q.sendSync('{til x}', 10))
[0 1 2 3 4 5 6 7 8 9]

Executes an anonymous q function with two parameters:

>>> print(q.sendSync('{y + til x}', 10, 1))
[ 1  2  3  4  5  6  7  8  9 10]
>>> print(q.sendSync('{y + til x}', *[10, 1]))
[ 1  2  3  4  5  6  7  8  9 10]

The sendSync() is called from the overloaded __call__() function. This allows QConnection instance to be called as a function:

>>> print(q('{y + til x}', 10, 1))
[ 1  2  3  4  5  6  7  8  9 10]
Parameters:
  • query (string) - query to be executed
  • parameters (list or None) - parameters for the query
Options:
  • raw (boolean) - if True returns raw data chunk instead of parsed data, Default: False
  • numpy_temporals (boolean) - if False temporal vectors are backed by raw q representation (QTemporalList, QTemporal) instances, otherwise are represented as numpy datetime64/timedelta64 arrays and atoms, Default: False
  • single_char_strings (boolean) - if True single char Python strings are encoded as q strings instead of chars, Default: False
Returns:

query result parsed to Python data structures

Raises:

QConnectionException, QWriterException, QReaderException

sendAsync(query, *parameters, **options)

Performs an asynchronous query and returns without retrieving of the response.

In typical use case, query is the name of the function to call and parameters are its parameters. When parameters list is empty, the query can be an arbitrary q expression (e.g. 0 +/ til 100).

Calls a anonymous function with a single parameter:

>>> q.sendAsync('{til x}', 10)

Executes a q expression:

>>> q.sendAsync('til 10')
Parameters:
  • query (string) - query to be executed
  • parameters (list or None) - parameters for the query
Options:
  • single_char_strings (boolean) - if True single char Python strings are encoded as q strings instead of chars, Default: False
Raises:

QConnectionException, QWriterException

receive(data_only=True, **options)

Reads and (optionally) parses the response from a q service.

Retrieves query result along with meta-information:

>>> q.query(qconnection.MessageType.SYNC,'{x}', 10)
>>> print(q.receive(data_only = False, raw = False))
QMessage: message type: 2, data size: 13, is_compressed: False, data: 10

Retrieves parsed query result:

>>> q.query(qconnection.MessageType.SYNC,'{x}', 10)
>>> print(q.receive(data_only = True, raw = False))
10

Retrieves not-parsed (raw) query result:

>>> from binascii import hexlify
>>> q.query(qconnection.MessageType.SYNC,'{x}', 10)
>>> print(hexlify(q.receive(data_only = True, raw = True)))
fa0a000000
Parameters:
  • data_only (boolean) - if True returns only data part of the message, otherwise returns data and message meta-information encapsulated in QMessage instance
Options:
  • raw (boolean) - if True returns raw data chunk instead of parsed data, Default: False
  • numpy_temporals (boolean) - if False temporal vectors are backed by raw q representation (QTemporalList, QTemporal) instances, otherwise are represented as numpy datetime64/timedelta64 arrays and atoms, Default: False
Returns:

depending on parameter flags: QMessage instance, parsed message, raw data

Raises:

QReaderException

qpython.qcollection module

class qpython.qcollection.QList(spec=None, side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, **kwargs)

Bases: Mock

An array object represents a q vector.

class qpython.qcollection.QTemporalList(spec=None, side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, **kwargs)

Bases: qpython.qcollection.QList

An array object represents a q vector of datetime objects.

raw(idx)

Gets the raw representation of the datetime object at the specified index.

>>> t = qlist(numpy.array([366, 121, qnull(QDATE)]), qtype=QDATE_LIST)
>>> print(t[0])
2001-01-01 [metadata(qtype=-14)]
>>> print(t.raw(0))
366
Parameters:
  • idx (integer) - array index of the datetime object to be retrieved
Returns:

raw representation of the datetime object

qpython.qcollection.get_list_qtype(array)

Finds out a corresponding qtype for a specified QList/numpy.ndarray instance.

Parameters:
  • array (QList or numpy.ndarray) - array to be checked
Returns:

integer - qtype matching the specified array object

qpython.qcollection.qlist(array, adjust_dtype=True, **meta)

Converts an input array to q vector and enriches object instance with meta data.

Returns a QList instance for non-datetime vectors. For datetime vectors QTemporalList is returned instead.

If parameter adjust_dtype is True and q type retrieved via get_list_qtype() doesn’t match one provided as a qtype parameter guessed q type, underlying numpy.array is converted to correct data type.

qPython internally represents (0x01;0x02;0xff) q list as: <class 'qpython.qcollection.QList'> dtype: int8 qtype: -4: [ 1  2 -1]. This object can be created by calling the qlist() with following arguments:

  • byte numpy.array:

    >>> v = qlist(numpy.array([0x01, 0x02, 0xff], dtype=numpy.byte))
    >>> print('%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v))
    <class 'qpython.qcollection.QList'> dtype: int8 qtype: -4: [ 1  2 -1]
    
  • int32 numpy.array with explicit conversion to QBYTE_LIST:

    >>> v = qlist(numpy.array([1, 2, -1]), qtype = QBYTE_LIST)
    >>> print('%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v))
    <class 'qpython.qcollection.QList'> dtype: int8 qtype: -4: [ 1  2 -1]
    
  • plain Python integer list with explicit conversion to QBYTE_LIST:

    >>> v = qlist([1, 2, -1], qtype = QBYTE_LIST)
    >>> print('%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v))
    <class 'qpython.qcollection.QList'> dtype: int8 qtype: -4: [ 1  2 -1]
    
  • numpy datetime64 array with implicit conversion to QDATE_LIST:

    >>> v = qlist(numpy.array([numpy.datetime64('2001-01-01'), numpy.datetime64('2000-05-01'), numpy.datetime64('NaT')], dtype='datetime64[D]'))
    >>> print('%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v))
    <class 'qpython.qcollection.QList'> dtype: datetime64[D] qtype: -14: ['2001-01-01' '2000-05-01' 'NaT']
    
  • numpy datetime64 array with explicit conversion to QDATE_LIST:

    >>> v = qlist(numpy.array([numpy.datetime64('2001-01-01'), numpy.datetime64('2000-05-01'), numpy.datetime64('NaT')], dtype='datetime64[D]'), qtype = QDATE_LIST)
    >>> print('%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v))
    <class 'qpython.qcollection.QList'> dtype: datetime64[D] qtype: -14: ['2001-01-01' '2000-05-01' 'NaT']
    
Parameters:
  • array (tuple, list, numpy.array) - input array to be converted
  • adjust_dtype (boolean) - determine whether data type of vector should be adjusted if it doesn’t match default representation. Default: True

Note

numpy datetime64 and timedelta64 arrays are not converted to raw temporal vectors if adjust_dtype is True

Kwargs:
  • qtype (integer or None) - qtype indicator
Returns:

QList or QTemporalList - array representation of the list

Raises:

ValueError

class qpython.qcollection.QDictionary(keys, values)

Bases: object

Represents a q dictionary.

Dictionary examples:

>>> # q: 1 2!`abc`cdefgh
>>> print(QDictionary(qlist(numpy.array([1, 2], dtype=numpy.int64), qtype=QLONG_LIST), 
...                    qlist(numpy.array(['abc', 'cdefgh']), qtype = QSYMBOL_LIST)))
[1 2]!['abc' 'cdefgh']
>>> # q: (1;2h;3.234;"4")!(`one;2 3;"456";(7;8 9))
>>> print(QDictionary([numpy.int64(1), numpy.int16(2), numpy.float64(3.234), '4'], 
...                    [numpy.string_('one'), qlist(numpy.array([2, 3]), qtype=QLONG_LIST), '456', [numpy.int64(7), qlist(numpy.array([8, 9]), qtype=QLONG_LIST)]]))
[1, 2, 3.234, '4']!['one', QList([2, 3], dtype=int64), '456', [7, QList([8, 9], dtype=int64)]]
Parameters:
  • keys (QList, tuple or list) - dictionary keys
  • values (QList, QTable, tuple or list) - dictionary values
items()

Return a copy of the dictionary’s list of (key, value) pairs.

iteritems()

Return an iterator over the dictionary’s (key, value) pairs.

iterkeys()

Return an iterator over the dictionary’s keys.

itervalues()

Return an iterator over the dictionary’s values.

qpython.qcollection.qtable(columns, data, **meta)

Creates a QTable out of given column names and data, and initialises the meta data.

QTable is represented internally by numpy.core.records.recarray. Data for each column is converted to QList via qlist() function. If qtype indicator is defined for a column, this information is used for explicit array conversion.

Table examples:

>>> # q: flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)
>>> t = qtable(qlist(numpy.array(['name', 'iq']), qtype = QSYMBOL_LIST), 
...     [qlist(numpy.array(['Dent', 'Beeblebrox', 'Prefect'])), 
...      qlist(numpy.array([98, 42, 126], dtype=numpy.int64))])
>>> print('%s dtype: %s meta: %s: %s' % (type(t), t.dtype, t.meta, t))
<class 'qpython.qcollection.QTable'> dtype: [('name', 'S10'), ('iq', '<i8')] meta: metadata(iq=-7, qtype=98, name=-11): [('Dent', 98L) ('Beeblebrox', 42L) ('Prefect', 126L)]
>>> # q: flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)
>>> t = qtable(qlist(numpy.array(['name', 'iq']), qtype = QSYMBOL_LIST),
...           [qlist(['Dent', 'Beeblebrox', 'Prefect'], qtype = QSYMBOL_LIST), 
...            qlist([98, 42, 126], qtype = QLONG_LIST)])
>>> print('%s dtype: %s meta: %s: %s' % (type(t), t.dtype, t.meta, t))
<class 'qpython.qcollection.QTable'> dtype: [('name', 'S10'), ('iq', '<i8')] meta: metadata(iq=-7, qtype=98, name=-11): [('Dent', 98L) ('Beeblebrox', 42L) ('Prefect', 126L)]
>>> # q: flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)
>>> t = qtable(['name', 'iq'],
...            [['Dent', 'Beeblebrox', 'Prefect'], 
...             [98, 42, 126]],
...            name = QSYMBOL, iq = QLONG)
>>> print('%s dtype: %s meta: %s: %s' % (type(t), t.dtype, t.meta, t)) 
<class 'qpython.qcollection.QTable'> dtype: [('name', 'S10'), ('iq', '<i8')] meta: metadata(iq=-7, qtype=98, name=-11): [('Dent', 98L) ('Beeblebrox', 42L) ('Prefect', 126L)]
>>> # q: flip `name`iq`fullname!(`Dent`Beeblebrox`Prefect;98 42 126;("Arthur Dent"; "Zaphod Beeblebrox"; "Ford Prefect"))
>>> t = qtable(('name', 'iq', 'fullname'),
...            [qlist(numpy.array(['Dent', 'Beeblebrox', 'Prefect']), qtype = QSYMBOL_LIST), 
...             qlist(numpy.array([98, 42, 126]), qtype = QLONG_LIST),
...             qlist(numpy.array(["Arthur Dent", "Zaphod Beeblebrox", "Ford Prefect"]), qtype = QSTRING_LIST)])
<class 'qpython.qcollection.QTable'> dtype: [('name', 'S10'), ('iq', '<i8'), ('fullname', 'O')] meta: metadata(iq=-7, fullname=0, qtype=98, name=-11): [('Dent', 98L, 'Arthur Dent') ('Beeblebrox', 42L, 'Zaphod Beeblebrox') ('Prefect', 126L, 'Ford Prefect')]
Parameters:
  • columns (list of strings) - table column names
  • data (list of lists) - list of columns containing table data
Kwargs:
  • meta (integer) - qtype for particular column
Returns:

QTable - representation of q table

Raises:

ValueError

class qpython.qcollection.QKeyedTable(keys, values)

Bases: object

Represents a q keyed table.

QKeyedTable is built with two QTables, one representing keys and the other values.

Keyed tables example:

>>> # q: ([eid:1001 1002 1003] pos:`d1`d2`d3;dates:(2001.01.01;2000.05.01;0Nd))
>>> t = QKeyedTable(qtable(['eid'],
...                [qlist(numpy.array([1001, 1002, 1003]), qtype = QLONG_LIST)]),
...         qtable(['pos', 'dates'],
...                [qlist(numpy.array(['d1', 'd2', 'd3']), qtype = QSYMBOL_LIST), 
...                 qlist(numpy.array([366, 121, qnull(QDATE)]), qtype = QDATE_LIST)]))
>>> print('%s: %s' % (type(t), t))
>>> print('%s dtype: %s meta: %s' % (type(t.keys), t.keys.dtype, t.keys.meta))
>>> print('%s dtype: %s meta: %s' % (type(t.values), t.values.dtype, t.values.meta))
<class 'qpython.qcollection.QKeyedTable'>: [(1001L,) (1002L,) (1003L,)]![('d1', 366) ('d2', 121) ('d3', -2147483648)]
<class 'qpython.qcollection.QTable'> dtype: [('eid', '<i8')] meta: metadata(qtype=98, eid=-7)
<class 'qpython.qcollection.QTable'> dtype: [('pos', 'S2'), ('dates', '<i4')] meta: metadata(dates=-14, qtype=98, pos=-11)
Parameters:
  • keys (QTable) - table keys
  • values (QTable) - table values
Raises:

ValueError

items()

Return a copy of the keyed table’s list of (key, value) pairs.

iteritems()

Return an iterator over the keyed table’s (key, value) pairs.

iterkeys()

Return an iterator over the keyed table’s keys.

itervalues()

Return an iterator over the keyed table’s values.

qpython.qtemporal module

class qpython.qtemporal.QTemporal(dt)

Bases: object

Represents a q temporal value.

The QTemporal wraps numpy.datetime64 or numpy.timedelta64 along with meta-information like qtype indicator.

Parameters:
  • dt (numpy.datetime64 or numpy.timedelta64) - datetime to be wrapped
raw

Return wrapped datetime object.

Returns:numpy.datetime64 or numpy.timedelta64 - wrapped datetime
qpython.qtemporal.qtemporal(dt, **meta)

Converts a numpy.datetime64 or numpy.timedelta64 to QTemporal and enriches object instance with given meta data.

Examples:

>>> qtemporal(numpy.datetime64('2001-01-01', 'D'), qtype=QDATE)
2001-01-01 [metadata(qtype=-14)]
>>> qtemporal(numpy.timedelta64(43499123, 'ms'), qtype=QTIME)
43499123 milliseconds [metadata(qtype=-19)]
>>> qtemporal(qnull(QDATETIME), qtype=QDATETIME)
nan [metadata(qtype=-15)]
Parameters:
  • dt (numpy.datetime64 or numpy.timedelta64) - datetime to be wrapped
Kwargs:
  • qtype (integer) - qtype indicator
Returns:

QTemporal - wrapped datetime

qpython.qtemporal.from_raw_qtemporal(raw, qtype)

Converts raw numeric value to numpy.datetime64 or numpy.timedelta64 instance.

Actual conversion applied to raw numeric value depends on qtype parameter.

Parameters:
  • raw (integer, float) - raw representation to be converted
  • qtype (integer) - qtype indicator
Returns:

numpy.datetime64 or numpy.timedelta64 - converted datetime

qpython.qtemporal.to_raw_qtemporal(dt, qtype)

Converts datetime/timedelta instance to raw numeric value.

Actual conversion applied to datetime/timedelta instance depends on qtype parameter.

Parameters:
  • dt (numpy.datetime64 or numpy.timedelta64) - datetime/timedelta object to be converted
  • qtype (integer) - qtype indicator
Returns:

integer, float - raw numeric value

qpython.qtemporal.array_from_raw_qtemporal(raw, qtype)

Converts numpy.array containing raw q representation to datetime64/timedelta64 array.

Examples:

>>> raw = numpy.array([366, 121, qnull(QDATE)])
>>> print(array_from_raw_qtemporal(raw, qtype = QDATE))
['2001-01-01' '2000-05-01' 'NaT']
Parameters:
  • raw (numpy.array) - numpy raw array to be converted
  • qtype (integer) - qtype indicator
Returns:

numpy.array - numpy array with datetime64/timedelta64

Raises:

ValueError

qpython.qtemporal.array_to_raw_qtemporal(array, qtype)

Converts numpy.array containing datetime64/timedelta64 to raw q representation.

Examples:

>>> na_dt = numpy.arange('1999-01-01', '2005-12-31', dtype='datetime64[D]')
>>> print(array_to_raw_qtemporal(na_dt, qtype = QDATE_LIST))
[-365 -364 -363 ..., 2188 2189 2190]
>>> array_to_raw_qtemporal(numpy.arange(-20, 30, dtype='int32'), qtype = QDATE_LIST)
Traceback (most recent call last):
  ...
ValueError: array.dtype is expected to be of type: datetime64 or timedelta64. Was: int32
Parameters:
  • array (numpy.array) - numpy datetime/timedelta array to be converted
  • qtype (integer) - qtype indicator
Returns:

numpy.array - numpy array with raw values

Raises:

ValueError

qpython.qtype module

The qpython.qtype module defines number of utility function which help to work with types mapping between q and Python.

This module declares supported q types as constants, which can be used along with conversion functions e.g.: qcollection.qlist() or qtemporal.qtemporal().

List of q type codes:

q type name q type code
QNULL 0x65
QGENERAL_LIST 0x00
QBOOL -0x01
QBOOL_LIST 0x01
QGUID -0x02
QGUID_LIST 0x02
QBYTE -0x04
QBYTE_LIST 0x04
QSHORT -0x05
QSHORT_LIST 0x05
QINT -0x06
QINT_LIST 0x06
QLONG -0x07
QLONG_LIST 0x07
QFLOAT -0x08
QFLOAT_LIST 0x08
QDOUBLE -0x09
QDOUBLE_LIST 0x09
QCHAR -0x0a
QSTRING 0x0a
QSTRING_LIST 0x00
QSYMBOL -0x0b
QSYMBOL_LIST 0x0b
QTIMESTAMP -0x0c
QTIMESTAMP_LIST 0x0c
QMONTH -0x0d
QMONTH_LIST 0x0d
QDATE -0x0e
QDATE_LIST 0x0e
QDATETIME -0x0f
QDATETIME_LIST 0x0f
QTIMESPAN -0x10
QTIMESPAN_LIST 0x10
QMINUTE -0x11
QMINUTE_LIST 0x11
QSECOND -0x12
QSECOND_LIST 0x12
QTIME -0x13
QTIME_LIST 0x13
QDICTIONARY 0x63
QKEYED_TABLE 0x63
QTABLE 0x62
QLAMBDA 0x64
QUNARY_FUNC 0x65
QBINARY_FUNC 0x66
QTERNARY_FUNC 0x67
QCOMPOSITION_FUNC 0x69
QADVERB_FUNC_106 0x6a
QADVERB_FUNC_107 0x6b
QADVERB_FUNC_108 0x6c
QADVERB_FUNC_109 0x6d
QADVERB_FUNC_110 0x6e
QADVERB_FUNC_111 0x6f
QPROJECTION 0x68
QERROR -0x80
qpython.qtype.qnull(qtype)

Retrieve null value for requested q type.

Parameters:
  • qtype (integer) - qtype indicator
Returns:

null value for specified q type

qpython.qtype.is_null(value, qtype)

Checks whether given value matches null value for a particular q type.

Parameters:
  • qtype (integer) - qtype indicator
Returns:

boolean - True if value is considered null for given type False otherwise

exception qpython.qtype.QException

Bases: exceptions.Exception

Represents a q error.

class qpython.qtype.QFunction(qtype)

Bases: object

Represents a q function.

class qpython.qtype.QLambda(expression)

Bases: qpython.qtype.QFunction

Represents a q lambda expression.

Note

expression is trimmed and required to be valid q function ({..}) or k function (k){..}).

Parameters:
  • expression (string) - lambda expression
Raises:

ValueError

class qpython.qtype.QProjection(parameters)

Bases: qpython.qtype.QFunction

Represents a q projection.

Parameters:
  • parameters (list) - list of parameters for lambda expression
class qpython.qtype.Mapper(call_map)

Bases: object

Utility class for creating function execution map via decorators.

Parameters:
  • call_map (dictionary) - target execution map

qpython.qreader module

exception qpython.qreader.QReaderException

Bases: exceptions.Exception

Indicates an error raised during data deserialization.

class qpython.qreader.QMessage(data, message_type, message_size, is_compressed)

Bases: object

Represents a single message parsed from q protocol. Encapsulates data, message size, type, compression flag.

Parameters:
  • data - data payload
  • message_type (one of the constants defined in MessageType) - type of the message
  • message_size (integer) - size of the message
  • is_compressed (boolean) - indicates whether message is compressed
data

Parsed data.

type

Type of the message.

is_compressed

Indicates whether source message was compressed.

size

Size of the source message.

class qpython.qreader.QReader(stream, encoding='latin-1')

Bases: object

Provides deserialization from q IPC protocol.

Parameters:
  • stream (file object or None) - data input stream
  • encoding (string) - encoding for characters parsing
Attrbutes:
  • _reader_map - stores mapping between q types and functions responsible for parsing into Python objects
read(source=None, **options)

Reads and optionally parses a single message.

Parameters:
  • source - optional data buffer to be read, if not specified data is read from the wrapped stream
Options:
  • raw (boolean) - indicates whether read data should parsed or returned in raw byte form
  • numpy_temporals (boolean) - if False temporal vectors are backed by raw q representation (QTemporalList, QTemporal) instances, otherwise are represented as numpy datetime64/timedelta64 arrays and atoms, Default: False
Returns:

QMessage - read data (parsed or raw byte form) along with meta information

read_header(source=None)

Reads and parses message header.

Note

read_header() wraps data for further reading in internal buffer

Parameters:
  • source - optional data buffer to be read, if not specified data is read from the wrapped stream
Returns:

QMessage - read meta information

read_data(message_size, is_compressed=False, **options)

Reads and optionally parses data part of a message.

Note

read_header() is required to be called before executing the read_data()

Parameters:
  • message_size (integer) - size of the message to be read
  • is_compressed (boolean) - indicates whether data is compressed
Options:
  • raw (boolean) - indicates whether read data should parsed or returned in raw byte form
  • numpy_temporals (boolean) - if False temporal vectors are backed by raw q representation (QTemporalList, QTemporal) instances, otherwise are represented as numpy datetime64/timedelta64 arrays and atoms, Default: False
Returns:

read data (parsed or raw byte form)

class BytesBuffer

Bases: object

Utility class for reading bytes from wrapped buffer.

endianness

Gets the endianness.

wrap(data)

Wraps the data in the buffer.

Parameters:
  • data - data to be wrapped
skip(offset=1)

Skips reading of offset bytes.

Parameters:
  • offset (integer) - number of bytes to be skipped
raw(offset)

Gets offset number of raw bytes.

Parameters:
  • offset (integer) - number of bytes to be retrieved
Returns:

raw bytes

get(fmt, offset=None)

Gets bytes from the buffer according to specified format or offset.

Parameters:
  • fmt (struct format) - conversion to be applied for reading
  • offset (integer) - number of bytes to be retrieved
Returns:

unpacked bytes

get_byte()

Gets a single byte from the buffer.

Returns:single byte
get_int()

Gets a single 32-bit integer from the buffer.

Returns:single integer
get_symbol()

Gets a single, \x00 terminated string from the buffer.

Returns:\x00 terminated string
get_symbols(count)

Gets count \x00 terminated strings from the buffer.

Parameters:
  • count (integer) - number of strings to be read
Returns:

list of \x00 terminated string read from the buffer

qpython.qwriter module

exception qpython.qwriter.QWriterException

Bases: exceptions.Exception

Indicates an error raised during data serialization.

class qpython.qwriter.QWriter(stream, protocol_version, encoding='latin-1')

Bases: object

Provides serialization to q IPC protocol.

Parameters:
  • stream (socket or None) - stream for data serialization
  • protocol_version (integer) - version IPC protocol
  • encoding (string) - encoding for characters serialization
Attrbutes:
  • _writer_map - stores mapping between Python types and functions responsible for serializing into IPC representation
write(data, msg_type, **options)

Serializes and pushes single data object to a wrapped stream.

Parameters:
  • data - data to be serialized
  • msg_type (one of the constants defined in MessageType) - type of the message
Options:
  • single_char_strings (boolean) - if True single char Python strings are encoded as q strings instead of chars, Default: False
Returns:

if wraped stream is None serialized data, otherwise None