Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kola

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kola

a Python Polars interface to kdb+/q

  • 1.5.2
  • PyPI
  • Socket score

Maintainers
1

kola

a Python Polars Interface to kdb+/q

Basic Data Type Map

Deserialization

Atom
k typensizepython typenote
boolean11bool
guid216str
byte41int
short52int
int64int
long78int
real84float
float98float
char101str
string101str
symbol11*str
timestamp128datetime
month134-
date144date0001.01.01 - 9999.12.31
datetime158datetime
timespan168timedelta
minute174time00:00 - 23:59
second184time00:00:00 - 23:59:59
time194time00:00:00.000 - 23:59:59.999
Composite Data Type
k typensizepython type
boolean list11pl.Boolean
guid list216pl.List(pl.Binary(16))
byte list41pl.Uint8
short list52pl.Int16
int list64pl.Int32
long list78pl.Int64
real list84pl.Float32
float list98pl.Float64
char list101pl.Utf8
string list101pl.Utf8
symbol list11*pl.Categorical
timestamp list128pl.Datetime
month list134-
date list144pl.Date
datetime list158pl.Datetime
timespan list168pl.Duration
minute list174pl.Time
second list184pl.Time
time list194pl.Time
table98*pl.DataFrame
dictionary99*-
keyed table99*pl.DataFrame

performance is impacted by converting guid to string, deserialize the uuid to 16 fixed binary list, use .hex() to convert binary to string if required

real/float 0n is mapped to Polars null not NaN

short/int/long 0Nh/i/j, 0Wh/i/j and -0Wh/i/j are mapped to null

df.with_columns([
    (pl.col("uuid").apply(lambda u: u.hex()))
    ])

Serialization

Basic Data Type
python typek typenote
boolboolean
intlong
floatfloat
strsymbol
bytesstring
datetimetimestamp
datedate0001.01.01 - 9999.12.31
datetimedatetime
timedeltatimespan
timetime00:00:00.000 - 23:59:59.999
Dictionary, Series and DataFrame
python typek type
dictdict
pl.Booleanboolean
pl.List(pl.Binary(16))guid
pl.Uint8byte
pl.Int16short
pl.Int32int
pl.Int64long
pl.Float32real
pl.Float64float
pl.Utf8char
pl.Categoricalsymbol
pl.Datetimetimestamp
pl.Datedate
pl.Datetimedatetime
pl.Durationtimespan
pl.Timetime
pl.DataFrametable

Limited Support for dictionary as arguments, requires string as keys.

Quick Start

Create a Connection

import polars as pl
import kola
q = kola.Q('localhost', 1800)

# with retries for IO Errors, 1s, 2s, 4s ...
q = kola.Q('localhost', 1800, retries=3)

# with read timeout error, 2s, "Resource temporarily unavailable"
q = kola.Q('localhost', 1800, retries=3, timeout=2)

Connect(Optional)

Automatically connect when querying q process

q.connect()

Disconnect

Automatically disconnect if any IO error

q.disconnect()

String Query

q.sync("select from trade where date=last date")

Lambda Query

When the first string starts with { and ends with }, it is treated as a lambda.

d = {"a": 1, "b": 2}
q.sync("{key x}", d)

Functional Query

For functional query, kola supports Python Basic Data Type, pl.Series, pl.DataFrame and Python Dictionary with string keys and Python Basic Data Type and pl.Series values.

from datetime import date, time

q.sync(
    ".gw.query",
    "table",
    {
        "date": date(2023, 11, 21),
        "syms": pl.Series("", ["sym0", "sym1"], pl.Categorical),
        # 09:00
        "startTime": time(9),
        # 11:30
        "endTime": time(11, 30),
    },
)

Send DataFrame

# pl_df is a Polars DataFrame
q.sync("upsert", "table", pl_df)
# pd_df is a Pandas DataFrame, use pl.DateFrame to cast Pandas DataFrame
q.sync("upsert", "table", pl.DataFrame(pd_df))

Async Query

# pl_df is a Polars DataFrame
q.asyn("upsert", "table", pl_df)

Subscribe

from kola import QType

q.sync(".u.sub", pl.Series("", ["table1", "table2"], QType.Symbol), "")

# specify symbol filter
q.sync(
    ".u.sub",
    pl.Series("", ["table1", "table2"], QType.Symbol),
    pl.Series("", ["sym1", "sym2"], QType.Symbol),
)

while true:
    # ("upd", "table", pl.Dataframe)
    upd = self.q.receive()
    print(upd)

Generate IPC

import polars as pl
from kola import generate_ipc

df = pl.DataFrame(
    {
        "sym": pl.Series("sym", ["a", "b", "c"], pl.Categorical),
        "price": [1, 2, 3],
    }
)
# without compression
buffer = generate_ipc("sync", False, ["upd", "table", df])

# with compression
buffer = generate_ipc("sync", True, ["upd", "table", df])

Polars Documentations

Refer to

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc