我正在尝试通过在pyq中运行以下代码将数据上载到已扩展的kdb数据库中...

from bittrex.bittrex import Bittrex, API_V2_0
from datetime import datetime
import time
from pyq import q, K

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

q.set(':db/alpha/', q('.Q.en', ':db', q('([]Name:`$(); Ask:`float$(); Bid:`float$(); Last:`float$(); Volume:`float$(); Time:`timestamp$())')))

while True:
    market_result = get_bittrex.get_market_summaries()['result']
    for res in market_result:
        market_name = res['MarketName']
        ask = float(res['Ask'])
        bid = float(res['Bid'])
        last = float(res['Last'])
        volume = float(res['Volume'])
        dt = res['TimeStamp']
        if market_name in ['USDT-BTC', 'USDT-ETH', 'USDT-LTC', 'USDT-XRP', 'USDT-NEO', 'USDT-BCC', 'USDT-ZEC', 'USDT-XMR', 'USDT-DASH']:
            ts = datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S.%f')
            data = [market_name[5:], ask, bid, last, volume, ts]
            q.upsert(':db/alpha/', q('.Q.en', ':db', [data]))
            q.get(':db/alpha/').show()
    time.sleep(30.0)


它总是抛出相同的错误:_k.error: type。我相信这是因为我没有在q.upsert中一次登记一行要追加的数据。

当我在未展开的桌子上使用相同的脚本时,效果很好...

from bittrex.bittrex import Bittrex, API_V2_0
from datetime import datetime
import time
from pyq import q, K

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

q.set(':alpha', q('([]Name:`$(); Ask:`float$(); Bid:`float$(); Last:`float$(); Volume:`float$(); Time:`timestamp$())'))

while True:
    market_result = get_bittrex.get_market_summaries()['result']
    for res in market_result:
        market_name = res['MarketName']
        ask = float(res['Ask'])
        bid = float(res['Bid'])
        last = float(res['Last'])
        volume = float(res['Volume'])
        dt = res['TimeStamp']
        if market_name in ['USDT-BTC', 'USDT-ETH', 'USDT-LTC', 'USDT-XRP', 'USDT-NEO', 'USDT-BCC', 'USDT-ZEC', 'USDT-XMR', 'USDT-DASH']:
            ts = datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S.%f')
            data = [market_name[5:], ask, bid, last, volume, ts]
            q.upsert(':alpha', [data])
            q.get(':alpha').show()
    time.sleep(30.0)


而且,当我尝试同时手动设置和增加2行时,它会返回预期的结果...

(pyq36-64) ❯ pyq
Python 3.6.3 (default, Nov 30 2017, 16:33:45)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> q.set(':db/alpha/', q('.Q.en', ':db', q('([]Name:`$(); Ask:`float$(); Bid:`float$(); Last:`float$(); Volume:`float$(); Time:`timestamp$())')))
k('`:db/alpha/')
>>> q.upsert(':db/alpha/', q('.Q.en', ':db', q('([]Name:`f`g; Ask:70.0 80.0; Bid:60.0 70.0; Last:50.0 60.0; Volume:645.32 745.33; Time:2017.12.19D19:10:09.697000000 2017.12.19D19:10:09.697000000)')))
k('`:db/alpha/')
>>> q.get(':db/alpha/').show()
Name Ask Bid Last Volume Time
------------------------------------------------------
f    70  60  50   645.32 2017.12.19D19:10:09.697000000
g    80  70  60   745.33 2017.12.19D19:10:09.697000000
>>> q()
q)\ls -l -a db/alpha
"total 64"
"drwxr-xr-x  10 marrowgari  staff  340 Dec 19 16:42 ."
"drwxr-xr-x   4 marrowgari  staff  136 Dec 19 15:59 .."
"-rw-r--r--   1 marrowgari  staff   38 Dec 19 16:42 .d"
"-rw-r--r--   1 marrowgari  staff   32 Dec 19 16:42 Ask"
"-rw-r--r--   1 marrowgari  staff   32 Dec 19 16:42 Bid"
"-rw-r--r--   1 marrowgari  staff   32 Dec 19 16:42 Last"
"-rw-r--r--   1 marrowgari  staff   32 Dec 19 16:42 Name"
"-rw-r--r--   1 marrowgari  staff   32 Dec 19 16:42 Time"
"-rw-r--r--   1 marrowgari  staff   16 Dec 19 16:19 Vol"
"-rw-r--r--   1 marrowgari  staff   32 Dec 19 16:42 Volume"
q)


如何将变量data从上面的代码中追加到已展开的表中?

q.set(':db/alpha/', q('.Q.en', ':db', q('([]Name:`$(); Ask:`float$(); Bid:`float$(); Last:`float$(); Volume:`float$(); Time:`timestamp$())')))
q.upsert(':db/alpha/', q('.Q.en', ':db', [data]))

最佳答案

当您提出关于SO的问题时,请努力用易于再现的代码来介绍您的问题。理想情况下,请尝试在没有第三方软件包的情况下重现您的问题。在您的特定情况下,该问题与Bittrex API没有关系,可以简单地表示为

>>> q.set(':db/alpha/', q('.Q.en[`:db]', q('([]Name:`$(); Ask:`float$())')))
k('`:db/alpha/')
>>> data = ['A', 70.0]
>>> q.upsert(':db/alpha/', q('.Q.en', ':db', [data]))
Traceback (most recent call last):
  ..
_k.error: type


错误的原因是.Q.en需要一个表,该表在q中是字典列表,并且您传递了一个列表列表。

一种解决方案是在将数据传递给q之前将其转换为字典:

>>> data_dict = {a:b for a, b in zip(['Name', 'Ask'], data)}
>>> q.upsert(':db/alpha/', q('.Q.en', ':db', [data_dict]))
k('`:db/alpha/')


另一个(可能更快)的解决方案是枚举data中的字符串元素:

>>> data[0] = q('`:db/sym?', data[0])
>>> data
[k('`sym$`A'), 70.0]


并使用insert函数将数据追加到表中:

>>> q.insert(':db/alpha/', data)
k(',1')

关于python - PyQ:q.upsert到张开的 table ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47895776/

10-12 21:45