PyQ - q.upsert和q.set附加到splayed kdb数据库

时间:2017-12-09 03:22:03

标签: python-3.x kdb pyq

当我在pyq ...

中运行以下代码时
from bittrex.bittrex import Bittrex, API_V2_0
import time, json
from pyq import q, K

get_bittrex = Bittrex(None, None)
starttime = time.time()

market_result = get_bittrex.get_market_summaries()['result']

while True:
    for res in market_result:
        market_name = res['MarketName']
        ask = str(res['Ask'])
        bid = str(res['Bid'])
        last = str(res['Last'])
        timeStamp = str(res['TimeStamp'])
        if market_name in ['USDT-BTC', 'USDT-ETH', 'USDT-LTC']:
            mkt = {"Name": market_name,"Bid": bid,"Ask": ask,"Time":timeStamp}
            q.set(':alpha', [mkt])
            q.upsert(':alpha', mkt)
            q.get(':alpha').show()

    time.sleep(10.0)

它每10秒返回一次控制台......

Name     Bid            Ask            Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name     Bid          Ask      Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name     Bid          Ask          Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
Name     Bid            Ask            Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name     Bid          Ask      Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name     Bid          Ask          Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703

为什么每行打印2次?另外,我如何修改此内容,以便将每个market_name添加到同一个表格中,即它当前正在将每个market_name打印到自己的表格中,而不是附加到现有表格中表

理想的输出结果如下......

Name     Bid            Ask            Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703

谢谢。

1 个答案:

答案 0 :(得分:1)

您在循环中执行的操作等同于以下q代码:

q)`:alpha set enlist`a`b!1 2
q)`:alpha upsert`a`b!1 2
q)show get`:alpha
a b
---
1 2
1 2

这里没有什么可惊讶的。 set命令在文件alpha中保存1行表(回想一下qa表中是字典列表),然后upsert命令附加一行与保存的那个,最后get读取生成的2行表。

你可能想要的是在循环外初始化表,在循环中运行一系列upsert并在循环后显示结果。

请注意,您可以像这样初始化表:

>>> q.set(':alpha', q('!', ["Name","Bid","Ask","Time"], ()).flip)
k('`:alpha')
>>> q.get(':alpha').show()
Name Bid Ask Time
-----------------

最后,如果您的目标是保存Feed,则很快将每封邮件一旦到达就将其写入磁盘。大多数饲料处理人员在内存中收集一天的市场数据,并在一天结束时将其保存在组屋中。