如何通过 qpython 将包含分钟列表的字典传递给 kdb

how to pass a dictionary holding a list of minutes through qpython to kdb

我正在尝试通过 qpython 查询特定函数。该函数需要一个包含多个参数的字典,第一个是日期(类型 -14),第二个是分钟列表(类型 17)。

我写了这个小例子,希望能忠实地说明问题:

\d .test

testfun:{[args]
    
    one:args[`one];
    two:args[`two];
    
    ' string type args[`two];
    :42;
    };

现在当我通过 qpython 查询时:

from qpython import qconnection
from qpython.qcollection import QDictionary, qlist
import numpy as np
from qpython.qtype import QLONG_LIST, QSYMBOL_LIST, QMINUTE_LIST


query='{[arg_dict] :.test.testfun[arg_dict] }'

kdbconfig = {
        'q_host': 'q_host',
        'q_port': 42,
        'q_usr': 'q_usr',
        'q_pwd': 'q_pwd' 
        }

params = {
    "one" : np.datetime64('2021-01-06', 'D'),
    "two" : qlist([ np.timedelta64(10*60, 'm'), np.timedelta64(10*60+30, 'm')] , qtype = QMINUTE_LIST)
    }

qparams = QDictionary(list(params.keys()), list(params.values()))


with qconnection.QConnection(host=kdbconfig['q_host'],
                                     port=kdbconfig['q_port'],
                                     username=kdbconfig['q_usr'],
                                     password=kdbconfig['q_pwd'] ) as q:
        data = q.sendSync(query, qparams, pandas = True)
        if data is None:
            raise ValueError("didnt return any data")

但我得到 QException: b'-14' 而我希望输入 17
qparams.values 价值:[numpy.datetime64('2021-01-06'), QList([420, 450], dtype='timedelta64[m]')] 所以我觉得很合理。

请问有人知道如何进行这项工作吗?

密钥作为字符串而不是符号发送:

q).debug
"one"| 2021.01.06
"two"| 10:00 10:30

q).debug[`two]
0Nd
q)type .debug[`two]
-14h

`two 根据字典中第一项的类型返回 null。我实际上不确定 qpython 如何处理字符串与符号,但您可以将 q 函数更新为:

.test.testfun:{[args]
  one:args["one"];
  two:args["two"];
  string type args["two"]
  };

编辑:如何调试

如果查询发送到 q 进程正常,如果它没有按预期运行,最好在 kdb 端进行调试。我将测试 q 函数定义为:

.test.testfun:{[args].debug:args;};

这让我可以查看 kdb 正在接收什么。 np.string_ 上的不错,这将其定义为符号:

qparams = QDictionary(np.string_(list(params.keys())), list(params.values()))

q).debug
one| 2021.01.06
two| 10:00 10:30

您还可以按如下方式定义 q 函数,因此它 returns 到 python:

// .Q.s1 returns the string representation of an object
q).test.testfun:{[args].debug:args;.Q.s1 .debug};

python test.py
b'`one`two!(2021.01.06;10:00 10:30)'

我的脚本:

from qpython import qconnection
from qpython.qcollection import QDictionary, qlist
import numpy as np
from qpython.qtype import QLONG_LIST, QSYMBOL_LIST, QMINUTE_LIST


query='{[arg_dict] :.test.testfun[arg_dict] }'

params = {
    "one" : np.datetime64('2021-01-06', 'D'),
    "two" : qlist([ np.timedelta64(10*60, 'm'), np.timedelta64(10*60+30, 'm')] , qtype = QMINUTE_LIST)
    }

qparams = QDictionary(np.string_(list(params.keys())), list(params.values()))

with qconnection.QConnection(host="localhost",
                                     port=12345,
                                     username="",
                                     password="" ) as q:
        data = q.sendSync(query, qparams)
        if data is None:
            raise ValueError("didnt return any data")
print(data)