Python:如何创建一个结构体.计算一个标准大小为6的整数



如果我看这里:https://docs.python.org/2/library/struct.html at section 7.3.2.2。格式化字符,对于标准大小为6的Python类型整数没有格式化字母。我尝试过'6p'甚至'3H',但这不起作用,例如:

import struct
struct.calcsize('!6p')

抱歉,如果这听起来很愚蠢…我正在学习如何将字符串转换为第一次打包的二进制数据,用于长度为6的字符串消息,其值为整数。那么,要获得整数的标准大小6,正确的字母格式是什么呢?

我的任务是转换包含在 NASDAQ TotalVIEW-ITCH 5.0规范文档中的二进制消息,这里 例如,如果你看第4.1节:系统事件消息(第3页),我将转换类型为'S'的消息如下:
class SystemEventMessage(Message): 
    type = 'S'
    description = "System Event Message"
    message_size = struct.calcsize("!HH6pc") + 1
def __init__(self, message):
    (self.locate,self.tracking,self.timestamp, self.code) = struct.unpack("!HH6pc", message[1:])
def pack(self):
    return struct.pack("!cHH6pc", self.type,self.locate,self.tracking, self.timestamp, self.code)

我得到这个错误:

struct.error: unpack requires a string argument of length 11

所以我假设我的错误与'6p'有关,因为我找不到任何标准大小为6的整数。

更新2

所以我使用下面的建议使用6s而不是6p,对于下面的消息类型"I"在文档第4.6节:净订单不平衡指标(NOII)消息,我做:

class NoiiMessage(ITCH41MarketMessage):
    type = 'I'
    description = "NOII Message"
    message_size = struct.calcsize("!HH6sQQc8sIIIcc") + 1
    def __init__(self, message):
        (self.locate,self.tracking,self.timestamp, self.pairedShares, self.imbalance,
         self.imbalanceDirection, self.stock, self.farPrice, self.nearPrice,
         self.currentRefPrice, self.crossType, self.priceVariationbsindicator
         ) = struct.unpack("!HH6sQQc8sIIIcc", message[1:])
    def pack(self):
        return struct.pack("!cHH6sQQc8sIIIcc", self.type,self.locate,
                           self.tracking, self.timestamp,
                           self.pairedShares, self.imbalance,
                           self.imbalanceDirection, self.stock,
                           self.farPrice, self.nearPrice,
                           self.currentRefPrice, self.crossType,
                           self.priceVariationbsindicator)

我得到这个错误:

struct.error: unpack requires a string argument of length 49

现在很奇怪,因为!HH6sQQc8sIIIcc的长度是49…


谢谢大家的帮助!

struct设计用于处理C结构。这就是为什么它只有股票C型。"6字节整数"不是标准的C类型——你不能写像struct s { int6 timestamp; }这样的东西来立即得到一个可用的整数。这就是为什么这里也不能像这样工作。

那么,你如何用C语言解决这个问题呢?你可能会

  • write unsigned char ts_data[6];
  • 将值复制到其他地方
  • 填充它和
  • 将结果解释为整数

现在,我们要做的就是用Python表达同样的东西:

>>> struct.pack('q',1324)
',x05x00x00x00x00x00x00'    #my arch is big-endian
>>> struct.unpack('q',',x05x00x00x00x00')
error: unpack requires a string argument of length 8
>>> struct.unpack('6s',',x05x00x00x00x00')
(',x05x00x00x00x00',)
>>> s=_[0]
>>> struct.unpack('q',s+'x00'*2)    #check byte order to find out which side to pad from
(1324,)

NASDAQ TotalView-ITCH 5.0

[S0099123456Q]________________________________ wireline SEQ _____________
 | | |     ||
 | | |     |+---------------[[ Event Code   ]]
 | | |     +----------------[[ Timestamp ns ]]
 | | +----------------------[[ Tracking NUM ]]
 | +------------------------[[ Stock Locate ]]
 +--------------------------[[ Message Type ]]
Name           | Offset | Length | Value   | Notes
---------------|--------|--------|---------|-------------------------------------------------------------
Message Type   | 0      | 1      | “S”     | System Event Message.
Stock Locate   | 1      | 2      | Integer | == 0 Always
Tracking Number| 3      | 2      | Integer | NASDAQ OMX internal tracking number
Timestamp      | 5      | 6      | Integer | Nanoseconds since midnight.
Event Code     |11      | 1      | Alpha   | == { 0 | S | Q | M | E | C } See System Event Codes below.

为了得到NASDAQ_Timestamp
在掩码中使用 6B 抓取6- uchar -s或 char[] 6s
并且仅在消费者端需要时才将它们后处理为int()
其中延迟解除阻塞去流引擎电缆性能

+也享受Python 7.3.2.1。关于使用"!"掩码前缀

的BigEndian/network排序的注意事项
>>> struct.pack(   ">c2H6sc", "S", 0, 99, "123456", "Q" )
'Sx00x00x00c123456Q'
>>> struct.unpack( ">c2H6sc", "Sx00x00x00c123456Q" )
('S', 0, 99, '123456', 'Q')
  |   |   |        |    |
  |   |   |        |    +---------------[[ Event Code   ]]
  |   |   |        +--------------------[[ Timestamp ns ]]
  |   |   +-----------------------------[[ Tracking NUM ]]
  |   +---------------------------------[[ Stock Locate ]]
  +-------------------------------------[[ Message Type ]]

UPDATE2

"!HH6sQQc8sIIIcc"                              _
 +||-||||-||||||----------------------------1 |_|_ "!"   a "Network"-Byte-order
  +|-||||-||||||----------------------------2 |_|   H as a 2 Byte unsigned short
   | |||| ||||||                            3 |_|_ 
   +-||||-||||||----------------------------4-|_|   H as a 2 Byte unsigned short                                
     |||| ||||||                            5 |_|_ 
     +|||-||||||----------------------------6-|_|  6s as a 6 Byte char[]
      ||| ||||||                            7 |_|  
      ||| ||||||                            8 |_|  
      ||| ||||||                            9 |_|  
      ||| ||||||                           10 |_|  
      ||| ||||||                            1 |_|_ 
      +||-||||||--------------------------- 2-|_|   Q as a 8 Byte unsigned long long
       || ||||||                            3 |_|  
       || ||||||                            4 |_|  
       || ||||||                            5 |_|  
       || ||||||                            6 |_|  
       || ||||||                            7 |_|  
       || ||||||                            8 |_|  
       || ||||||                            9 |_|_ 
       +|-||||||---------------------------20-|_|   Q as a 8 Byte unsigned long long
        | ||||||                            1 |_|  
        | ||||||                            2 |_|  
        | ||||||                            3 |_|  
        | ||||||                            4 |_|  
        | ||||||                            5 |_|  
        | ||||||                            6 |_|  
        | ||||||                            7 |_|_ 
        +-||||||----------------------------8-|_|_  c as a 1 Byte char
          +|||||----------------------------9-|_|  8s as a 8 Byte char[]
           |||||                           30 |_|  
           |||||                            1 |_|  
           |||||                            2 |_|  
           |||||                            3 |_|  
           |||||                            4 |_|  
           |||||                            5 |_|  
           |||||                            6 |_|_ 
           +||||----------------------------7-|_|   I as a 4 Byte unsigned int
            ||||                            8 |_|                                     
            ||||                            9 |_|                                     
            ||||                           40 |_|_ 
            +|||----------------------------1-|_|   I as a 4 Byte unsigned int
             |||                            2 |_|                             
             |||                            3 |_|                             
             |||                            4 |_|_ 
             +||----------------------------5-|_|   I as a 4 Byte unsigned int
              ||                            6 |_|                             
              ||                            7 |_|                             
              ||                            8 |_|_ 
              +|----------------------------9-|_|_  c as a 1 Byte char
               +---------------------------50-|_|_  c as a 1 Byte char

地点:

Format  | C-type               | Python-type        | Standard size
========|======================|====================|===============
     x  | pad byte             | no value           |     
     c  | char                 | string of length 1 | 1 
     b  | signed char          | integer            | 1 
     B  | unsigned char        | integer            | 1 
     ?  | _Bool                | bool               | 1 
     h  | short                | integer            | 2 
     H  | unsigned short       | integer            | 2 
     i  | int                  | integer            | 4 
     I  | unsigned int         | integer            | 4 
     l  | long                 | integer            | 4 
     L  | unsigned long        | integer            | 4 
     q  | long long            | integer            | 8 
     Q  | unsigned long long   | integer            | 8 
     f  | float                | float              | 4 
     d  | double               | float              | 8 
     s  | char[]               | string             | 
     p  | char[]               | string             |  
     P  | void *               | integer            |       

最新更新