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

2024-09-30 16:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过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]')] 所以在我看来这是合理的

任何人都会有一个想法如何使这项工作,请


Tags: fromimporthost参数portusrnppwd
1条回答
网友
1楼 · 发布于 2024-09-30 16:19:59

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

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进程ok,那么最好在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)'

我的剧本:

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)

相关问题 更多 >