【问题标题】:how to pass a dictionary holding a list of minutes through qpython to kdb如何通过q​​python将保存分钟列表的字典传递给kdb
【发布时间】:2021-06-30 17:04:49
【问题描述】:

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

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

【问题讨论】:

    标签: python kdb


    【解决方案1】:

    键是作为字符串而不是符号发送的:

    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 端进行调试。我将 test 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)
    

    【讨论】:

    • 谢谢马特! sym 明天用 numpy.string_ ill test 表示,但这是有道理的。但是你是怎么得到字典的价值的呢?目前我正在使用异常从我的分析师那里将值发送回 python 进程,这并不理想......
    • 就是这样。谢谢你的详细解释,这将非常有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 2011-07-13
    • 2011-09-23
    相关资源
    最近更新 更多