Thanks to visit codestin.com
Credit goes to github.com

Skip to content

connection

Jo Shinonome edited this page Aug 17, 2024 · 1 revision

Q

from kola import Q

Parameters

parameter default note
host hostname or ip
port port number
user OS Username
passwd ""
enable_tls False
retries 0 times exponential back off retry, 1s, 2s, 4s ...
timeout 0 second timeout error, "Resource temporarily unavailable"

Examples

import kola

# disable retries and timeout
q = kola.Q('localhost', 1800)

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

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

Methods

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:
    # sample return, ("upd", "table", pl.Dataframe)
    upd = self.q.receive()
    print(upd)

Clone this wiki locally