Python struct.pack_into() Examples
The following are 30
code examples of struct.pack_into().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
struct
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: test_struct.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_pack_into_fn(self): test_string = b'Reykjavik rocks, eow!' writable_buf = array.array('b', b' '*100) fmt = '21s' pack_into = lambda *args: struct.pack_into(fmt, *args) # Test without offset. pack_into(writable_buf, 0, test_string) from_buf = writable_buf.tobytes()[:len(test_string)] self.assertEqual(from_buf, test_string) # Test with offset. pack_into(writable_buf, 10, test_string) from_buf = writable_buf.tobytes()[:len(test_string)+10] self.assertEqual(from_buf, test_string[:10] + test_string) # Go beyond boundaries. small_buf = array.array('b', b' '*10) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0, test_string) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2, test_string)
Example #2
Source File: test_struct.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_pack_into_fn(self): test_string = 'Reykjavik rocks, eow!' writable_buf = array.array('c', ' '*100) fmt = '21s' pack_into = lambda *args: struct.pack_into(fmt, *args) # Test without offset. pack_into(writable_buf, 0, test_string) from_buf = writable_buf.tostring()[:len(test_string)] self.assertEqual(from_buf, test_string) # Test with offset. pack_into(writable_buf, 10, test_string) from_buf = writable_buf.tostring()[:len(test_string)+10] self.assertEqual(from_buf, test_string[:10] + test_string) # Go beyond boundaries. small_buf = array.array('c', ' '*10) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0, test_string) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2, test_string)
Example #3
Source File: info.py From aerospike-admin with Apache License 2.0 | 6 votes |
def _authenticate(sock, user, password, password_field_id): user = str_to_bytes(user) password = str_to_bytes(password) sz = len(user) + len(password) + 34 # 2 * 5 + 24 send_buf = _admin_write_header(sz, _AUTHENTICATE, 2) fmt_str = "! I B %ds I B %ds" % (len(user), len(password)) struct.pack_into(fmt_str, send_buf, _HEADER_SIZE, len(user) + 1, _USER_FIELD_ID, user, len(password) + 1, password_field_id, password) try: # OpenSSL wrapper doesn't support ctypes send_buf = _buffer_to_string(send_buf) sock.sendall(send_buf) recv_buff = _receivedata(sock, _HEADER_SIZE) rv = _admin_parse_header(recv_buff) return rv[2] except Exception as ex: raise IOError("Error: %s" % str(ex))
Example #4
Source File: test_icmp.py From ryu with Apache License 2.0 | 6 votes |
def setUp_with_echo(self): self.echo_id = 13379 self.echo_seq = 1 self.echo_data = b'\x30\x0e\x09\x00\x00\x00\x00\x00' \ + b'\x10\x11\x12\x13\x14\x15\x16\x17' \ + b'\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' \ + b'\x20\x21\x22\x23\x24\x25\x26\x27' \ + b'\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f' \ + b'\x30\x31\x32\x33\x34\x35\x36\x37' self.data = icmp.echo( id_=self.echo_id, seq=self.echo_seq, data=self.echo_data) self.type_ = icmp.ICMP_ECHO_REQUEST self.code = 0 self.ic = icmp.icmp(self.type_, self.code, self.csum, self.data) self.buf = bytearray(struct.pack( icmp.icmp._PACK_STR, self.type_, self.code, self.csum)) self.buf += self.data.serialize() self.csum_calc = packet_utils.checksum(self.buf) struct.pack_into('!H', self.buf, 2, self.csum_calc)
Example #5
Source File: default_records.py From aiokafka with Apache License 2.0 | 6 votes |
def write_header(self, use_compression_type=True): batch_len = len(self._buffer) self.HEADER_STRUCT.pack_into( self._buffer, 0, 0, # BaseOffset, set by broker batch_len - self.AFTER_LEN_OFFSET, # Size from here to end self.NO_PARTITION_LEADER_EPOCH, self._magic, 0, # CRC will be set below, as we need a filled buffer for it self._get_attributes(use_compression_type), self._last_offset, self._first_timestamp or 0, self._max_timestamp or 0, self._producer_id, self._producer_epoch, self._base_sequence, self._num_records ) crc = calc_crc32c(self._buffer[self.ATTRIBUTES_OFFSET:]) struct.pack_into(">I", self._buffer, self.CRC_OFFSET, crc)
Example #6
Source File: test_legacy.py From aiokafka with Apache License 2.0 | 6 votes |
def test_read_log_append_time_v1(): buffer = _make_compressed_batch(1) # As Builder does not support creating data with `timestamp_type==1` we # patch the result manually buffer[ATTRIBUTES_OFFSET] |= TIMESTAMP_TYPE_MASK expected_timestamp = 10000000 struct.pack_into(">q", buffer, TIMESTAMP_OFFSET, expected_timestamp) batch = LegacyRecordBatch(buffer, 1) msgs = list(batch) for offset, msg in enumerate(msgs): assert msg.offset == offset assert msg.timestamp == expected_timestamp assert msg.timestamp_type == 1
Example #7
Source File: test_struct.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_trailing_counter(self): store = array.array('b', b' '*100) # format lists containing only count spec should result in an error self.assertRaises(struct.error, struct.pack, '12345') self.assertRaises(struct.error, struct.unpack, '12345', '') self.assertRaises(struct.error, struct.pack_into, '12345', store, 0) self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0) # Format lists with trailing count spec should result in an error self.assertRaises(struct.error, struct.pack, 'c12345', 'x') self.assertRaises(struct.error, struct.unpack, 'c12345', 'x') self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0, 'x') self.assertRaises(struct.error, struct.unpack_from, 'c12345', store, 0) # Mixed format tests self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs') self.assertRaises(struct.error, struct.unpack, '14s42', 'spam and eggs') self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0, 'spam and eggs') self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
Example #8
Source File: test_struct.py From BinderFilter with MIT License | 6 votes |
def test_pack_into_fn(self): test_string = 'Reykjavik rocks, eow!' writable_buf = array.array('c', ' '*100) fmt = '21s' pack_into = lambda *args: struct.pack_into(fmt, *args) # Test without offset. pack_into(writable_buf, 0, test_string) from_buf = writable_buf.tostring()[:len(test_string)] self.assertEqual(from_buf, test_string) # Test with offset. pack_into(writable_buf, 10, test_string) from_buf = writable_buf.tostring()[:len(test_string)+10] self.assertEqual(from_buf, test_string[:10] + test_string) # Go beyond boundaries. small_buf = array.array('c', ' '*10) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0, test_string) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2, test_string)
Example #9
Source File: test_struct.py From oss-ftp with MIT License | 6 votes |
def test_pack_into_fn(self): test_string = 'Reykjavik rocks, eow!' writable_buf = array.array('c', ' '*100) fmt = '21s' pack_into = lambda *args: struct.pack_into(fmt, *args) # Test without offset. pack_into(writable_buf, 0, test_string) from_buf = writable_buf.tostring()[:len(test_string)] self.assertEqual(from_buf, test_string) # Test with offset. pack_into(writable_buf, 10, test_string) from_buf = writable_buf.tostring()[:len(test_string)+10] self.assertEqual(from_buf, test_string[:10] + test_string) # Go beyond boundaries. small_buf = array.array('c', ' '*10) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0, test_string) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2, test_string)
Example #10
Source File: test_icmp.py From ryu with Apache License 2.0 | 6 votes |
def setUp_with_dest_unreach(self): self.unreach_mtu = 10 self.unreach_data = b'abc' self.unreach_data_len = len(self.unreach_data) self.data = icmp.dest_unreach( data_len=self.unreach_data_len, mtu=self.unreach_mtu, data=self.unreach_data) self.type_ = icmp.ICMP_DEST_UNREACH self.code = icmp.ICMP_HOST_UNREACH_CODE self.ic = icmp.icmp(self.type_, self.code, self.csum, self.data) self.buf = bytearray(struct.pack( icmp.icmp._PACK_STR, self.type_, self.code, self.csum)) self.buf += self.data.serialize() self.csum_calc = packet_utils.checksum(self.buf) struct.pack_into('!H', self.buf, 2, self.csum_calc)
Example #11
Source File: test_struct.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_trailing_counter(self): store = array.array('b', b' '*100) # format lists containing only count spec should result in an error self.assertRaises(struct.error, struct.pack, '12345') self.assertRaises(struct.error, struct.unpack, '12345', '') self.assertRaises(struct.error, struct.pack_into, '12345', store, 0) self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0) # Format lists with trailing count spec should result in an error self.assertRaises(struct.error, struct.pack, 'c12345', 'x') self.assertRaises(struct.error, struct.unpack, 'c12345', 'x') self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0, 'x') self.assertRaises(struct.error, struct.unpack_from, 'c12345', store, 0) # Mixed format tests self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs') self.assertRaises(struct.error, struct.unpack, '14s42', 'spam and eggs') self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0, 'spam and eggs') self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
Example #12
Source File: memory.py From cle with BSD 2-Clause "Simplified" License | 6 votes |
def pack(self, addr, fmt, *data): """ Use the ``struct`` module to pack `data` into memory at address `addr` with the format `fmt`. """ try: start, backer = next(self.backers(addr)) except StopIteration: raise KeyError(addr) if start > addr: raise KeyError(addr) try: return struct.pack_into(fmt, backer, addr - start, *data) except struct.error as e: if len(backer) - (addr - start) >= struct.calcsize(fmt): raise e raise KeyError(addr)
Example #13
Source File: area.py From Miyamoto with GNU General Public License v3.0 | 6 votes |
def SaveEntrances(self): """ Saves the entrances back to block 7 """ offset = 0 entstruct = struct.Struct('>HHhhBBBBBBxBHBBBBBx') buffer = bytearray(len(self.entrances) * 24) zonelist = self.zones for entrance in self.entrances: zoneID = SLib.MapPositionToZoneID(zonelist, entrance.objx, entrance.objy) try: entstruct.pack_into(buffer, offset, int(entrance.objx), int(entrance.objy), int(entrance.camerax), int(entrance.cameray), int(entrance.entid), int(entrance.destarea), int(entrance.destentrance), int(entrance.enttype), int(entrance.players), zoneID, int(entrance.playerDistance), int(entrance.entsettings), int(entrance.otherID), int(entrance.coinOrder), int(entrance.pathID), int(entrance.pathnodeindex), int(entrance.transition)) except struct.error: if zoneID < 0: raise ValueError('Entrance %d at (%d, %d) is too far from any zone\'s boundaries!\nPlease place it near a zone.' % (entrance.entid, int(entrance.objx), int(entrance.objy))) from None else: raise ValueError('SaveEntrances struct.error.') offset += 24 self.blocks[6] = bytes(buffer)
Example #14
Source File: area.py From Miyamoto with GNU General Public License v3.0 | 6 votes |
def SaveLoadedSprites(self): """ Saves the list of loaded sprites back to block 9 """ ls = [] for sprite in self.sprites: if sprite.type not in ls: ls.append(sprite.type) ls.sort() offset = 0 sprstruct = struct.Struct('>Hxx') buffer = bytearray(len(ls) * 4) for s in ls: sprstruct.pack_into(buffer, offset, int(s)) offset += 4 self.blocks[8] = bytes(buffer)
Example #15
Source File: test_struct.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_pack_into_fn(self): test_string = b'Reykjavik rocks, eow!' writable_buf = array.array('b', b' '*100) fmt = '21s' pack_into = lambda *args: struct.pack_into(fmt, *args) # Test without offset. pack_into(writable_buf, 0, test_string) from_buf = writable_buf.tobytes()[:len(test_string)] self.assertEqual(from_buf, test_string) # Test with offset. pack_into(writable_buf, 10, test_string) from_buf = writable_buf.tobytes()[:len(test_string)+10] self.assertEqual(from_buf, test_string[:10] + test_string) # Go beyond boundaries. small_buf = array.array('b', b' '*10) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0, test_string) self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2, test_string)
Example #16
Source File: adafruit_ccs811.py From Adafruit_CircuitPython_CCS811 with MIT License | 6 votes |
def set_environmental_data(self, humidity, temperature): """Set the temperature and humidity used when computing eCO2 and TVOC values. :param int humidity: The current relative humidity in percent. :param float temperature: The current temperature in Celsius.""" # Humidity is stored as an unsigned 16 bits in 1/512%RH. The default # value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61, # 0x00. humidity = int(humidity * 512) # Temperature is stored as an unsigned 16 bits integer in 1/512 degrees # there is an offset: 0 maps to -25C. The default value is 25C = 0x64, # 0x00. As an example 23.5% temperature would be 0x61, 0x00. temperature = int((temperature + 25) * 512) buf = bytearray(5) buf[0] = _ENV_DATA struct.pack_into(">HH", buf, 1, humidity, temperature) with self.i2c_device as i2c: i2c.write(buf)
Example #17
Source File: ui_cfg_fdcb.py From NXP-MCUBootUtility with Apache License 2.0 | 5 votes |
def _setMemberForFdcb( self, offset, byteNum, data ): struct.pack_into(self._convertPackFmt(byteNum), self.fdcbBuffer, offset, data)
Example #18
Source File: adafruit_bno055.py From Adafruit_CircuitPython_BNO055 with MIT License | 5 votes |
def radius_magnetometer(self, radius): data = bytearray(2) struct.pack_into("<h", data, 0, radius) self._write_register(_RADIUS_MAGNET_REGISTER, bytes(data))
Example #19
Source File: BinaryParser.py From python-ntfs with Apache License 2.0 | 5 votes |
def pack_word(self, offset, word): """ Applies the little-endian WORD (2 bytes) to the relative offset. Arguments: - `offset`: The relative offset from the start of the block. - `word`: The data to apply. """ o = self._offset + offset # TODO return struct.pack_into("<H", self._buf, o, word)
Example #20
Source File: area.py From Miyamoto with GNU General Public License v3.0 | 5 votes |
def SaveSprites(self): """ Saves the sprites back to block 8 """ offset = 0 sprstruct = struct.Struct('>HHHHIIBB2sBxxx') buffer = bytearray((len(self.sprites) * 24) + 4) f_int = int for sprite in self.sprites: zoneID = SLib.MapPositionToZoneID(self.zones, sprite.objx, sprite.objy, True) try: sprstruct.pack_into(buffer, offset, f_int(sprite.type), f_int(sprite.objx), f_int(sprite.objy), struct.unpack(">H", sprite.spritedata[:2])[0], struct.unpack(">I", sprite.spritedata[2:6])[0], struct.unpack(">I", sprite.spritedata[6:10])[0], zoneID, sprite.layer, sprite.spritedata[10:], sprite.initialState) except struct.error: # Hopefully this will solve the mysterious bug, and will # soon no longer be necessary. if zoneID < 0: raise ValueError('Sprite %d at (%d, %d) is too far from any zone\'s boundaries!\nPlease place it near a zone.' % (sprite.type, sprite.objx, sprite.objy)) from None else: raise ValueError('SaveSprites struct.error. Current sprite data dump:\n' + \ str(offset) + '\n' + \ str(sprite.type) + '\n' + \ str(sprite.objx) + '\n' + \ str(sprite.objy) + '\n' + \ str(sprite.spritedata[:10]) + '\n' + \ str(zoneID) + '\n' + \ str(sprite.layer) + '\n' + \ str(sprite.spritedata[10:]) + '\n' + \ str(sprite.initialState) + '\n', ) offset += 24 buffer[offset] = 0xFF buffer[offset + 1] = 0xFF buffer[offset + 2] = 0xFF buffer[offset + 3] = 0xFF self.blocks[7] = bytes(buffer)
Example #21
Source File: area.py From Miyamoto with GNU General Public License v3.0 | 5 votes |
def WriteNabbitPathNodes(self, buffer, offst, nodes): """ Writes the Nabbit path node data to the block 15 bytearray """ offset = int(offst) nodestruct = struct.Struct('>HHffhHBBBx') for node in nodes: nodestruct.pack_into(buffer, offset, int(node['x']), int(node['y']), 0.0, 0.0, int(node['action']), int(node['unk1']), int(node['unk2']), int(node['unk3']), int(node['unk4'])) offset += 20
Example #22
Source File: test_icmp.py From ryu with Apache License 2.0 | 5 votes |
def setUp(self): self.type_ = icmp.ICMP_ECHO_REQUEST self.code = 0 self.csum = 0 self.data = None self.ic = icmp.icmp(self.type_, self.code, self.csum, self.data) self.buf = bytearray(struct.pack( icmp.icmp._PACK_STR, self.type_, self.code, self.csum)) self.csum_calc = packet_utils.checksum(self.buf) struct.pack_into('!H', self.buf, 2, self.csum_calc)
Example #23
Source File: area.py From Miyamoto with GNU General Public License v3.0 | 5 votes |
def SaveLayer(self, idx): """ Saves an object layer to a bytes object """ layer = self.layers[idx] if not layer: return None layer.sort(key=lambda obj: obj.zValue()) offset = 0 objstruct = struct.Struct('>HhhHHB') buffer = bytearray((len(layer) * 16) + 2) f_int = int for obj in layer: objstruct.pack_into(buffer, offset, f_int((obj.tileset << 12) | obj.type), f_int(obj.objx), f_int(obj.objy), f_int(obj.width), f_int(obj.height), f_int(obj.data)) offset += 16 buffer[offset] = 0xFF buffer[offset + 1] = 0xFF return bytes(buffer)
Example #24
Source File: area.py From Miyamoto with GNU General Public License v3.0 | 5 votes |
def SaveOptions(self): """ Saves block 2, the general options """ wrapByte = 1 if self.wrapFlag else 0 if self.unkFlag1: wrapByte |= 8 unk1 = 100 if self.unkFlag2 else 0 unk2 = 100 if self.unkFlag3 else 0 unk3 = 100 if self.unkFlag4 else 0 optstruct = struct.Struct('>IIHHxBBBBxxBHH') buffer = bytearray(0x18) optstruct.pack_into(buffer, 0, self.eventBits32, self.eventBits64, wrapByte, self.timelimit, unk1, unk2, unk3, self.startEntrance, self.startEntranceCoinBoost, self.timelimit2, self.timelimit3) self.blocks[1] = bytes(buffer)
Example #25
Source File: voice_client.py From discord.py with MIT License | 5 votes |
def _get_voice_packet(self, data): header = bytearray(12) # Formulate rtp header header[0] = 0x80 header[1] = 0x78 struct.pack_into('>H', header, 2, self.sequence) struct.pack_into('>I', header, 4, self.timestamp) struct.pack_into('>I', header, 8, self.ssrc) encrypt_packet = getattr(self, '_encrypt_' + self.mode) return encrypt_packet(header, data)
Example #26
Source File: ui_cfg_lut.py From NXP-MCUBootUtility with Apache License 2.0 | 5 votes |
def _setMemberForFdcb( self, offset, byteNum, data ): struct.pack_into(self._convertPackFmt(byteNum), self.fdcbBuffer, offset, data)
Example #27
Source File: io_file.py From cle with BSD 2-Clause "Simplified" License | 5 votes |
def value(self): val = bytearray(self.size) struct.pack_into(self.owner.arch.struct_fmt(size=4), val, io_file_data_for_arch(self.owner.arch)['fd'], self.fd) struct.pack_into(self.owner.arch.struct_fmt(size=4), val, 0, 0xFBAD2088) return bytes(val)
Example #28
Source File: hidtransport.py From pyu2f with Apache License 2.0 | 5 votes |
def ToWireFormat(self): """Serializes the packet.""" ret = bytearray(64) ret[0:4] = self.cid ret[4] = self.cmd struct.pack_into('>H', ret, 5, self.size) ret[7:7 + len(self.payload)] = self.payload return list(map(int, ret))
Example #29
Source File: xpediteData.py From Xpedite with Apache License 2.0 | 5 votes |
def commit(self): """Commits accumulated data to the xpedite data file""" offset = 0 layout = {} for key in self.dataTable: if not isinstance(self.dataTable[key].data, str): isMarshalled = True self.dataTable[key].binData = pickle.dumps(self.dataTable[key].data, pickle.HIGHEST_PROTOCOL) else: isMarshalled = False self.dataTable[key].binData = self.dataTable[key].data dataSize = len(self.dataTable[key].binData) layout[key] = LayoutEntry(offset, isMarshalled, dataSize) offset += dataSize with open(self.dataFile, 'wb') as binFile: pTable = pickle.dumps(layout, pickle.HIGHEST_PROTOCOL) pTableSize = len(pTable) #convert to bytes binBuffer = create_string_buffer(8) struct.pack_into('i', binBuffer, 0, pTableSize) binFile.write(binBuffer) binFile.write(pTable) for key in self.dataTable: binFile.write(self.dataTable[key].binData)
Example #30
Source File: adafruit_bno055.py From Adafruit_CircuitPython_BNO055 with MIT License | 5 votes |
def offsets_gyroscope(self, offsets): data = bytearray(6) struct.pack_into("<hhh", data, 0, *offsets) self._write_register(_OFFSET_GYRO_REGISTER, bytes(data))