Skip to content

Schema generation

Functionality to support the creation and manipulation of schemas.

Generated schemas can be used in combination with both insert and upsert functionality to create populated table and keyed table objects.

builder

builder(schema, *, key=None)

Generate an empty schema for a keyed or unkeyed table.

Parameters:

Name Type Description Default
schema Dict

The definition of the schema to be created mapping a 'str' to a pykx.* type object which is one of the types defined in pykx.schema._ktype_to_conversion.

required
key Optional[Union[str, List[str]]]

A str-like object or list of str objects denoting the columns within the table defined by schema to be treated as primary keys, see here for more information about q keyed tables.

None

Returns:

Type Description
k.K

A pykx.Table or pykx.KeyedTable matching the provided schema with zero rows.

Examples:

Create a simple pykx.Table with four columns of different types

>>> import pykx as kx
>>> qtab = kx.schema.builder({
        'col1' : kx.GUIDAtom,
        'col2': kx.TimeAtom,
        'col3': kx.BooleanAtom,
        'col4': kx.FloatAtom
        })
>>> qtab
pykx.Table(pykx.q('
col1 col2 col3 col4
-------------------
'))
>>> kx.q.meta(qtab)
pykx.KeyedTable(pykx.q('
c   | t f a
----| -----
col1| g
col2| t
col3| b
col4| f
'))

Create a pykx.KeyedTable with a single primary key.

>>> import pykx as kx
>>> qtab = kx.schema.builder({
        'col1': kx.TimestampAtom,
        'col2': kx.FloatAtom,
        'col3': kx.IntAtom},
        key = 'col1'
        )
>>> qtab
pykx.KeyedTable(pykx.q('
col1| col2 col3
----| ---------
'))
>>> kx.q.meta(qtab)
pykx.KeyedTable(pykx.q('
c   | t f a
----| -----
col1| p
col2| f
col3| i
'))

Create a pykx.KeyedTable with multiple primary keys.

>>> import pykx as kx
>>> qtab = kx.schema.builder({
        'col1': kx.TimestampAtom,
        'col2': kx.SymbolAtom,
        'col3': kx.IntAtom,
        'col4': kx.List},
        key = ['col1', 'col2']
        )
>>> qtab
pykx.KeyedTable(pykx.q('
col1 col2| col3 col4
---------| ---------
'))
>>> kx.q.meta(qtab)
pykx.KeyedTable(pykx.q('
c   | t f a
----| -----
col1| p
col2| s
col3| i
col4|
'))