Skip to main content

Pure Python asyncio connector to KDB

Project description

PyPI version

aiokdb

Python asyncio connector to KDB. Pure python, so does not depend on the k.h bindings or kdb shared objects, or numpy/pandas. Fully type hinted to comply with PEP-561. No non-core dependancies, and tested on Python 3.9 - 3.11.

Peer review & motivation

qPython is a widely used library for this task and it maps objects to Pandas Dataframes which might be more suitable for the majority of applications.

This library takes a different approach and aims to replicate using the KDB C-library functions, ie. being 100% explicit about KDB types. It was built working from the publically documented Serialization Examples and C API for kdb+ pages. Users might also need to be familiar with k.h.

A simple example:

from aiokdb.socket import khpu
# run ./q -p 12345 &

h = khpu("localhost", 12345, "kdb:pass")
result = h.k("2.0+3.0")

# if the remote returns a Q Exception, this gets raised, unless khpu(..., raise_krr=False)
assert result.aF() == 5.0

The result object is a K-like Python object (a KObj), having the usual signed integer type available as result.type. Accessors for the primitive types are prefixed with an a and check at runtime that the accessor is appropriate for the stored type (.aI(), .aJ(), .aH(), .aF() etc.). Atoms store their value to a bytes object irrespective of the type, and encode/decode on demand. Atomic values can be set with (.i(3), .j(12), .ss("hello")).

Arrays are implemented with subtypes that use Python's native arrays module for efficient array types. The MutableSequence arrays are returned using the usual array accessor functions .kI(), .kB(), .kS() etc.

Serialisation is handled by b9 which returns a python bytes, and d9 which takes a bytes and returns a K-object.

  • Atoms are created by ka, kb, ku, kg, kh, ki, kj, ke, kf, kc, ks, kt, kd, kz, ktj
  • Lists with ktn and knk
  • Dictionaries with xd and tables with xt.

Python manages garbage colleciton of our objects, so none of the refcounting primitives exist, ie. k.r and functions r1, r0 and m9, setm have no equivelent.

RPC

Client support using python asyncio is built into the package, and uses prompt_toolkit for line editing:

$ pip install aiokdb prompt_toolkit
$ ./q -p 12345 &
$ python -m aiokdb.cli --host localhost --port 12345
(eval) > ([s:7 6 0Nj]x:3?0Ng;y:2)
s| x                                    y
-|---------------------------------------
7| 409031f3-b19c-6770-ee84-6e9369c98697 2
6| 52cb20d9-f12c-9963-2829-3c64d8d8cb14 2
 | cddeceef-9ee9-3847-9172-3e3d7ab39b26 2
(eval) > \\
$

Tests

The unit tests in test/test_rpc.py will use a real KDB binary to test against (over RPC) if you set KDB_PYTEST_SERVICE to a URL of the form kdb://user:password@hostname:port, otherwise that test is skipped and they are self contained.

  • Formatting with ruff check .
  • Formatting with black .
  • imports isort --check --profile black .
  • Check type annotations with mypy --strict .
  • Run pytest . in the root directory

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiokdb-0.0.16.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

aiokdb-0.0.16-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file aiokdb-0.0.16.tar.gz.

File metadata

  • Download URL: aiokdb-0.0.16.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for aiokdb-0.0.16.tar.gz
Algorithm Hash digest
SHA256 e0ce678343d4559c603452c505800485a0724ac77531bb8ff8d3609551cbbd8e
MD5 1d92fecb429f3b2e49cfa121c6dfa87f
BLAKE2b-256 81df4138b67d6ef3c721036207a648c79f4b7f50990c42253e99cc70c91b4c72

See more details on using hashes here.

File details

Details for the file aiokdb-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: aiokdb-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for aiokdb-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 0d55f334717c40ea90474543ed5f763feb6bc41a9203c1df73cbc2a0256f295e
MD5 defa562bb0bb779cfc592f9bd71d7ff0
BLAKE2b-256 4b0f5b08bdb144c124b949e2f7c5fcdd655b1ae1d2ddb558e9e43508652b722b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page