PyQ - 将数据类型转换为空表

PyQ - casting datatypes to empty tables

在 PyQ 中我可以创建一个字典并用 flip 转置它以获得 table...

q.set(':alpha', q('!', ["Name", "Ask", "Bid", "Time"], ()).flip)

然而,正如预期的那样,当我在 q 中加载它并检索元数据时,它显示所有列的 char 类型为符号...

>>> q.get(':alpha').show()
Name  Ask              Bid            Time
---------------------------------------------------------------
"BTC" "16351.0"        "16350.0"      "2017-12-12T17:32:33.09"
"ETH" "589.89999993"   "585.25"       "2017-12-12T17:32:32.697"
"LTC" "297.0"          "296.29570358" "2017-12-12T17:32:32.353"
"BTC" "16355.0"        "16351.0"      "2017-12-12T17:32:44.777"
"ETH" "589.89999993"   "585.25"       "2017-12-12T17:32:42.15"
"LTC" "297.99940398"   "296.29570359" "2017-12-12T17:32:44.433"
"BTC" "16359.99999998" "16350.0"      "2017-12-12T17:32:53.713"
"ETH" "589.89999993"   "585.2500001"  "2017-12-12T17:32:53.197"
"LTC" "297.99940398"   "295.0"        "2017-12-12T17:32:51.37"
"BTC" "16355.0"        "16350.0"      "2017-12-12T17:33:02.433"
"ETH" "585.2500001"    "585.25"       "2017-12-12T17:33:03.497"
"LTC" "297.99940397"   "295.0"        "2017-12-12T17:33:01.463"
>>> q()
q)\l alpha
`alpha
q)meta alpha
c   | t f a
----| -----
Name| s
Ask | s
Bid | s
Time| s
q)

我认为这是导致 'type 在我执行以下查询时控制台抛出错误的原因...

q)select Name, max Ask, max Bid, Time from alpha
'type
q)select max Ask, max Bid, Time by Name from alpha
'type
q)select from alpha where Bid=(max;Bid) fby Name
'type
q)select from alpha where Name=`BTC
Name Ask Bid Time
-----------------

我知道在 q 中我可以发出以下命令来实现这个...

q)alpha:([]Name:`symbol$(); Ask:`float$(); Bid:`float$(); Time:`datetime$())
q)meta alpha
c   | t f a
----| -----
Name| s
Ask | f
Bid | f
Time| z
q)

在 PyQ 中为每一列定义 char 类型的正确语法是什么?

您也可以在 PyQ 中指定类型:

>>> from pyq import K
>>> q('!', ["Name", "Ask", "Bid", "Time"], [K.symbol([]), K.float([]), K.float([]), K.timestampt([])]).flip.meta.show()
c   | t f a
----| -----
Name| s
Ask | f
Bid | f
Time| p

或者在你的情况下,你可以使用这样的东西:

>>> q('!', ["Name", "Ask", "Bid", "Time"], [K.symbol(["LTC-USD","BTC-USD"]), 
... K.float([310.,16700.]), 
... K.float([310.01,16700.92]), 
... K.timestamp([datetime.datetime(2017,12,13,17,40,44), datetime.datetime(2017,12,13,17,40,45)])]).flip.show()
Name    Ask   Bid      Time
----------------------------------------------------
LTC-USD 310   310.01   2017.12.13D17:40:44.000000000
BTC-USD 16700 16700.92 2017.12.13D17:40:45.000000000

上面的示例可以进一步简化,因为 PyQ 知道如何将 Python 类型转换为 q 类型。

>>> q('!', ["Name", "Ask", "Bid", "Time"], [["LTC-USD","BTC-USD"], [310.,16700.], [310.01,16700.92], 
... [datetime.datetime(2017,12,13,17,40,44), datetime.datetime(2017,12,13,17,40,45)]]).flip.meta.show()
c   | t f a
----| -----
Name| s
Ask | f
Bid | f
Time| p

您可以在 PyQ 用户指南的 Constructs and casts 部分阅读更多内容。

我将日期时间字符类型传递给时间戳列。在 q 中,我将这些定义为...

q)table:([]date:`datetime$(); name:`symbol$())
q)meta table
c   | t f a
----| -----
date| z
name| s

q)table:([]date:`timestamp$(); name:`symbol$())
q)meta table
c   | t f a
----| -----
date| p
name| s

我能够通过将日期时间解析为时间戳并转换为 K.timestamp([])...

来解决这个问题

下面的代码...

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.load(':alpha')

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(60.0)

现在 returns 我的 table 中的正确字符类型...

q)meta alpha
c   | t f a
----| -----
Name| s
Ask | f
Bid | f
Last| f
Vol | f
Time| p