如何通过qpython向kdb传递一个持有分钟列表的字典

1 人关注

我试图通过qpython查询一个特定的函数。该函数期望一个有几个参数的字典,第一个参数是一个日期(类型-14),第二个参数是一个分钟列表(类型17)。

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

\d .test
testfun:{[args]
    one:args[`one];
    two:args[`two];
    ' string type args[`two];

now when I query through 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")

but I get QException: b'-14' while I would expect type 17
qparams.values是值得的。[numpy.datetime64('2021-01-06'), QList([420, 450], dtype='timedelta64[m]')] 所以在我看来是合理的。

有谁知道如何使其运作?

python
kdb
Will
Will
发布于 2021-07-01
1 个回答
Matt Moore
Matt Moore
发布于 2021-07-01
已采纳
0 人赞同

键被作为字符串而不是符号发送。

q).debug
"one"| 2021.01.06
"two"| 10:00 10:30
q).debug[`two]
q)type .debug[`two]

`two根据字典中第一项的类型返回一个null。实际上我不清楚qpython是如何处理字符串与符号的关系的,但你可以将q函数更新为这样。

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

Edit: How to debug

如果查询被发送到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函数,使其返回到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)'

My Script:

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: