本文整理匯總了Python中struct.Struct方法的典型用法代碼示例。如果您正苦於以下問題:Python struct.Struct方法的具體用法?Python struct.Struct怎麽用?Python struct.Struct使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類struct
的用法示例。
在下文中一共展示了struct.Struct方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: go
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def go(fhs):
fmt = None
with open(opt_vocab, 'w') as vocab_out:
with open(opt_output, 'w') as vecs_out:
for lines in izip(*fhs):
parts = [line.split() for line in lines]
token = parts[0][0]
if any(part[0] != token for part in parts[1:]):
raise IOError('vector files must be aligned')
print >> vocab_out, token
vec = [sum(float(x) for x in xs) for xs in zip(*parts)[1:]]
if not fmt:
fmt = struct.Struct('%df' % len(vec))
vecs_out.write(fmt.pack(*vec))
示例2: parse
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def parse(cls, buff, offset):
"""
Given a buffer and offset, returns the parsed value and new offset.
Parses the ``size_primitive`` first to determine how many more bytes to
consume to extract the value.
"""
size, offset = cls.size_primitive.parse(buff, offset)
if size == -1:
return None, offset
var_struct = struct.Struct("!%ds" % size)
value = var_struct.unpack_from(buff, offset)[0]
value = cls.parse_value(value)
offset += var_struct.size
return value, offset
示例3: __init__
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def __init__(self):
"""
A state machine that handles the functionality needed to create and transmit a message
"""
# Constants
self.MAX_PAYLOAD_LENGTH = 40 # TBD
self.MAX_MSG_DATA_LENGTH = 38 # TDB
# States
self.STATE_IDLE = 0
self.STATE_INIT = 1
self.STATE_FRAGMENT = 2
self.STATE_TX = 3
# Frame Types
self.MSG_START = 255
self.MSG_DATA = 254
self.MSG_END = 253
# Variables
self.list_packets = []
# Frame Definitions
self.pkt_datagram_frame = struct.Struct('1B 40s') # Fixed
self.pkt_start = struct.Struct('9s 3B') # Fixed
self.pkt_data = struct.Struct('2B 38s') # Variable Data Length
self.pkt_end = struct.Struct('1B') # Fixed
示例4: __init__
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def __init__(self):
"""
A state machine that handles the functionality needed to create and transmit a message
"""
#Constants
self.MAX_PAYLOAD_LENGTH = 20 #TBD
self.MAX_MSG_DATA_LENGTH = 18 #TDB
#States
self.STATE_IDLE = 0
self.STATE_INIT = 1
self.STATE_FRAGMENT = 2
self.STATE_TX = 3
#Frame Types
self.MSG_START = 255
self.MSG_DATA = 254
self.MSG_END = 253
#Variables
self.list_packets = []
#Frame Definitions
self.pkt_datagram_frame = struct.Struct('1B 19s')
self.pkt_start = struct.Struct('9s 3B')
self.pkt_data = struct.Struct('2B 18s')
self.pkt_end = struct.Struct('1B')
示例5: create_local_telem_update_packet
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def create_local_telem_update_packet():
"""
A predefined command packet generator that provides a payload for the local telemetry update command.
:Return: A completed packet
.. todo:: This should be deprecated into a single "dummy" payload function
"""
packet_struct = struct.Struct('>B')
packet = packet_struct.pack(255)
return packet
##############
## Command = Send RF Data Now
##############
示例6: create_rf_telem_update_packet
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def create_rf_telem_update_packet():
"""
A predefined command packet generator that provides a payload for the RF telemetry update command.
:Return: A completed packet
.. todo:: This should be deprecated into a single "dummy" payload function
"""
packet_struct = struct.Struct('>B')
packet = packet_struct.pack(255)
return packet
##############
## Command = Update RF frequency
##############
示例7: create_update_rf_frequency_packet
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def create_update_rf_frequency_packet(frequency_mhz):
"""
This function generates the command packet that updates the Faraday CC430 radio frequency.
:param frequency_mhz: The frequency in MHz as an Integer or Float.
:Return: A completed packet (string of bytes)
"""
freq_list = create_freq_list(float(frequency_mhz))
packet_struct = struct.Struct('3B')
packet = packet_struct.pack(freq_list[0], freq_list[1], freq_list[2])
return packet
##############
## Command = Change CC430 PATable Settings
## ucharPATable_Setting = Byte that is places into the PA Table
##############
示例8: create_update_rf_patable_packet
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def create_update_rf_patable_packet(ucharPATable_Setting):
"""
This function generates the command packet that updates the Faraday CC430 RF power setting.
.. note:: A setting of 152 is the maximum output power, any number higher than 152 will be sent as a value of 152.
:param ucharPATable_Setting: The RF power table register setting byte for the CC430.
:Return: A completed packet (string of bytes)
"""
packet_struct = struct.Struct('1B')
packet = packet_struct.pack(ucharPATable_Setting)
return packet
##############
## Command = RESET device debug Flash
## Note: Not payload packet needed, returing 1 byte of 0 just for placeholder
##############
示例9: CreatePacketMsgExperimental
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def CreatePacketMsgExperimental(msg_cmd, dest_callsign, dest_device_id, data_len, data):
"""
This function creates the command packet expected by the command application routine that forwards a supplied payload over RF to a remote Faraday device. This is a very useful yet simple and un-optimized method of sending abritray data from one unit to another.
:param dest_callsign: The callsign of the target Faraday unit
:param dest_device_id: The device ID number of the target Faraday unit
:param data_len: The length in bytes of the data payload supplied
:param data: The data payload to be sent to the remote Faraday device over RF
:Return: A completed packet (string of bytes)
.. note: The data payload length cannot be larger than the maximum transmissible unit. Also, data only needs to be a string of bytes so both strings and packed binary data are acceptable.
:Example: Creates an experiement message packet that sends "Testing" to the remote unit.
>>> CreatePacketMsgExperimental(0, "KB1LQD", 1, 7, "Testing")
"""
packet_struct = struct.Struct('1B9s2B42s')
packet = packet_struct.pack(msg_cmd, str(dest_callsign).upper(), dest_device_id, data_len, data)
return packet
示例10: __init__
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def __init__(self, input_channel, serial_physical_obj):
#Initialize class variables
self.rx_packet_queue = Queue.Queue()
self.rx_data_payload_queue = Queue.Queue()
self.logic_startbyte_received = False
self.logic_escapebyte_received = False
self.logic_stopbyte_received = False
self.receiver_class = Receiver_Datalink_Device_State_Parser_Class(input_channel, serial_physical_obj)
self.enable_flag = True
self.max_payload_size = 6
self.datalink_packet_format = 'c' + str(self.max_payload_size) + 'c' + 'c'
self.reconstruct_data = ''
self.timer_interval = 5
self.timer = threading.Timer(self.timer_interval, self.timer_trip)
self.datalink_packet_struct = struct.Struct('BBB125s')
#Start
threading.Thread.__init__(self)
self.start() #Starts the run() function and thread
示例11: __init__
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def __init__(self, base, config, layered = False, **kwargs):
addrspace.BaseAddressSpace.__init__(self, base, config, **kwargs)
self.as_assert(base == None or layered, 'Must be first Address Space')
self.as_assert(config.LOCATION.startswith("file://"), 'Location is not of file scheme')
path = urllib.url2pathname(config.LOCATION[7:])
self.as_assert(os.path.exists(path), 'Filename must be specified and exist')
self.name = os.path.abspath(path)
self.fname = self.name
self.mode = 'rb'
if config.WRITE:
self.mode += '+'
self.fhandle = open(self.fname, self.mode)
self.fhandle.seek(0, 2)
self.fsize = self.fhandle.tell()
self._long_struct = struct.Struct("=I")
# Abstract Classes cannot register options, and since this checks config.WRITE in __init__, we define the option here
示例12: push_vm_param
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def push_vm_param(self, param):
if isinstance(param, bytearray) or isinstance(param, bytes):
self.push_bytearray(param)
elif isinstance(param, str):
self.push_bytearray(bytes(param.encode()))
elif isinstance(param, bool):
self.push_bool(param)
elif isinstance(param, int):
self.push_int(param)
elif isinstance(param, dict):
self.push_map(param)
elif isinstance(param, list):
self.create_code_params_script_builder(param)
self.push_int(len(param))
self.emit(PACK)
elif isinstance(param, Struct):
self.push_struct(param)
elif isinstance(param, Address):
self.push_bytearray(param.to_bytes())
elif isinstance(param, Account):
self.push_bytearray(param.get_address().to_bytes())
else:
raise SDKException(ErrorCode.other_error('parameter type is error'))
示例13: _85encode
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False):
# Helper function for a85encode and b85encode
if not isinstance(b, bytes_types):
b = memoryview(b).tobytes()
padding = (-len(b)) % 4
if padding:
b = b + b'\0' * padding
words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b)
chunks = [b'z' if foldnuls and not word else
b'y' if foldspaces and word == 0x20202020 else
(chars2[word // 614125] +
chars2[word // 85 % 7225] +
chars[word % 85])
for word in words]
if padding and not pad:
if chunks[-1] == b'z':
chunks[-1] = chars[0] * 5
chunks[-1] = chunks[-1][:-padding]
return b''.join(chunks)
示例14: send_dbman_msg
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def send_dbman_msg(self, opcode, msg):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.target_ip, self.dbman_port))
encodedMsg = encoder.encode(msg, defMode=True)
msgLen = len(encodedMsg)
values = (opcode, msgLen, encodedMsg)
s = struct.Struct(">ii%ds" % msgLen)
packed_data = s.pack(*values)
sock.send(packed_data)
res = sock.recv(1024)
if res is not None:
print "Received 10002 response..."
sock.close()
示例15: get_option_flags
# 需要導入模塊: import struct [as 別名]
# 或者: from struct import Struct [as 別名]
def get_option_flags(option_num):
"""
Get Critical, UnSafe, NoCacheKey flags from the option number
as per RFC 7252, section 5.4.6
:param option_num: option number
:return: option flags
:rtype: 3-tuple (critical, unsafe, no-cache)
"""
opt_bytes = array.array('B', '\0\0')
if option_num < 256:
s = struct.Struct("!B")
s.pack_into(opt_bytes, 0, option_num)
else:
s = struct.Struct("H")
s.pack_into(opt_bytes, 0, option_num)
critical = (opt_bytes[0] & 0x01) > 0
unsafe = (opt_bytes[0] & 0x02) > 0
nocache = ((opt_bytes[0] & 0x1e) == 0x1c)
return (critical, unsafe, nocache)