Python struct.html() Examples
The following are 30
code examples of struct.html().
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
.
Example #1
Source File: binary_reader.py From ontology-python-sdk with GNU Lesser General Public License v3.0 | 6 votes |
def read_var_int(self, max_size=sys.maxsize): """ Read a variable length integer from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max_size (int): (Optional) maximum number of bytes to read. Returns: int: """ fb = self.read_byte() if fb is 0: return fb if hex(fb) == '0xfd': value = self.read_uint16() elif hex(fb) == '0xfe': value = self.read_uint32() elif hex(fb) == '0xff': value = self.read_uint64() else: value = fb if value > max_size: raise SDKException(ErrorCode.param_err('Invalid format')) return int(value)
Example #2
Source File: BinaryWriter.py From neo-python-core with MIT License | 6 votes |
def WriteVarString(self, value, encoding="utf-8"): """ Write a string value to the stream. Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: value (string): value to write to the stream. encoding (str): string encoding format. """ if type(value) is str: value = value.encode(encoding) length = len(value) ba = bytearray(value) byts = binascii.hexlify(ba) string = byts.decode(encoding) self.WriteVarInt(length) self.WriteBytes(string)
Example #3
Source File: lbroker.py From pedlar with Apache License 2.0 | 6 votes |
def handle_tick(): """Listen to incoming tick updates.""" socket = context.socket(zmq.SUB) # Set topic filter, this is a binary prefix # to check for each incoming message # set from server as uchar topic = X # We'll subsribe to only tick updates for now socket.setsockopt(zmq.SUBSCRIBE, bytes.fromhex('00')) logger.info("Connecting to ticker: %s", ARGS.ticker) socket.connect(ARGS.ticker) while True: raw = socket.recv() # unpack bytes https://docs.python.org/3/library/struct.html bid, ask = struct.unpack_from('dd', raw, 1) # offset topic logger.debug("Tick: %f %f", bid, ask) # We'll use global to pass tick data between green threads # since only 1 actually run at a time global BID, ASK # pylint: disable=global-statement BID, ASK = bid, ask # socket will be cleaned up at garbarge collection
Example #4
Source File: flask_ticker.py From pedlar with Apache License 2.0 | 6 votes |
def run(self): """Connect to ticker server and emit updates.""" socket = context.socket(zmq.SUB) # Set topic filter, this is a binary prefix # to check for each incoming message # set from server as uchar topic = X # We'll subsribe to only tick updates for now socket.setsockopt(zmq.SUBSCRIBE, bytes.fromhex('00')) with self.app.app_context(): current_app.logger.debug("Connecting to ticker: %s", current_app.config['TICKER_URL']) socket.connect(current_app.config['TICKER_URL']) while True: raw = socket.recv() # unpack bytes https://docs.python.org/3/library/struct.html bid, ask = struct.unpack_from('dd', raw, 1) # offset topic self.socketio.emit('tick', {'bid': round(bid, 5), 'ask': round(ask, 5)}) # socket will be cleaned up at garbarge collection
Example #5
Source File: agent.py From pedlar with Apache License 2.0 | 6 votes |
def remote_run(self): """Start main loop and receive updates.""" # Check connection if not self._session: self.connect() # We'll trade forever until interrupted logger.info("Starting main trading loop...") try: while True: socks = self._poller.poll(self.polltimeout) if not socks: continue raw = socks[0][0].recv() # unpack bytes https://docs.python.org/3/library/struct.html if len(raw) == 17: # We have tick data bid, ask = struct.unpack_from('dd', raw, 1) # offset topic self.on_tick(bid, ask, datetime.now()) elif len(raw) == 33: # We have bar data bo, bh, bl, bc = struct.unpack_from('dddd', raw, 1) # offset topic self.on_bar(bo, bh, bl, bc, datetime.now()) finally: logger.info("Stopping agent...") self.disconnect()
Example #6
Source File: lean_minhash.py From datasketch with MIT License | 6 votes |
def bytesize(self, byteorder='@'): '''Compute the byte size after serialization. Args: byteorder (str, optional): This is byte order of the serialized data. Use one of the `byte order characters <https://docs.python.org/3/library/struct.html#byte-order-size-and-alignment>`_: ``@``, ``=``, ``<``, ``>``, and ``!``. Default is ``@`` -- the native order. Returns: int: Size in number of bytes after serialization. ''' # Use 8 bytes to store the seed integer seed_size = struct.calcsize(byteorder+'q') # Use 4 bytes to store the number of hash values length_size = struct.calcsize(byteorder+'i') # Use 4 bytes to store each hash value as we are using the lower 32 bit hashvalue_size = struct.calcsize(byteorder+'I') return seed_size + length_size + len(self) * hashvalue_size
Example #7
Source File: binary_reader.py From neo-python with MIT License | 6 votes |
def _unpack(self, fmt, length=1) -> Any: """ Unpack the stream contents according to the specified format in `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. length (int): amount of bytes to read. Returns: variable: the result according to the specified format. """ try: values = struct.unpack(fmt, self._stream.read(length)) return values[0] except struct.error as e: raise ValueError(e)
Example #8
Source File: BinaryWriter.py From neo-python with MIT License | 6 votes |
def WriteVarString(self, value, encoding="utf-8"): """ Write a string value to the stream. Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: value (string): value to write to the stream. encoding (str): string encoding format. """ if type(value) is str: value = value.encode(encoding) length = len(value) ba = bytearray(value) byts = binascii.hexlify(ba) string = byts.decode(encoding) self.WriteVarInt(length) self.WriteBytes(string)
Example #9
Source File: BinaryReader.py From neo-python with MIT License | 6 votes |
def ReadVarBytes(self, max=sys.maxsize): """ Read a variable length of bytes from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max (int): (Optional) maximum number of bytes to read. Raises: ValueError: if the amount of bytes indicated by the variable int cannot be read Returns: bytes: """ length = self.ReadVarInt(max) return self.SafeReadBytes(length)
Example #10
Source File: digital.py From nxt-python with GNU General Public License v3.0 | 6 votes |
def _i2c_query(self, address, format): """Reads an i2c value from given address, and returns a value unpacked according to the given format. Format is the same as in the struct module. See http://docs.python.org/library/struct.html#format-strings """ n_bytes = struct.calcsize(format) msg = bytes((self.I2C_DEV, address)) now = time() if self.last_poll+self.poll_delay > now: diff = now - self.last_poll sleep(self.poll_delay - diff) self.last_poll = time() self.brick.ls_write(self.port, msg, n_bytes) try: self._ls_get_status(n_bytes) finally: #we should clear the buffer no matter what happens data = self.brick.ls_read(self.port) if len(data) < n_bytes: raise I2CError('Read failure: Not enough bytes') data = struct.unpack(format, data[-n_bytes:]) return data
Example #11
Source File: binary_reader.py From ontology-python-sdk with GNU Lesser General Public License v3.0 | 6 votes |
def unpack(self, fmt, length=1): """ Unpack the stream contents according to the specified format in `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. length (int): amount of bytes to read. Returns: variable: the result according to the specified format. """ try: info = struct.unpack(fmt, self.stream.read(length))[0] except struct.error as e: raise SDKException(ErrorCode.unpack_error(e.args[0])) return info
Example #12
Source File: binary_reader.py From neo-python with MIT License | 5 votes |
def read_var_bytes(self, max=sys.maxsize): """ Read a variable length of bytes from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max (int): (Optional) maximum number of bytes to read. Returns: bytes: """ length = self.read_var_int(max) return self.read_bytes(length)
Example #13
Source File: BinaryReader.py From neo-python-core with MIT License | 5 votes |
def ReadVarInt(self, max=sys.maxsize): """ Read a variable length integer from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max (int): (Optional) maximum number of bytes to read. Returns: int: """ fb = self.ReadByte() if fb is 0: return fb value = 0 if hex(fb) == '0xfd': value = self.ReadUInt16() elif hex(fb) == '0xfe': value = self.ReadUInt32() elif hex(fb) == '0xff': value = self.ReadUInt64() else: value = fb if value > max: raise Exception("Invalid format") return int(value)
Example #14
Source File: BinaryReader.py From neo-python-core with MIT License | 5 votes |
def unpack(self, fmt, length=1): """ Unpack the stream contents according to the specified format in `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. length (int): amount of bytes to read. Returns: variable: the result according to the specified format. """ return struct.unpack(fmt, self.stream.read(length))[0]
Example #15
Source File: binary_writer.py From ontology-python-sdk with GNU Lesser General Public License v3.0 | 5 votes |
def pack(self, fmt, data): """ Write bytes by packing them according to the provided format `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html """ return self.write_bytes(struct.pack(fmt, data))
Example #16
Source File: BinaryReader.py From neo-python-core with MIT License | 5 votes |
def ReadVarBytes(self, max=sys.maxsize): """ Read a variable length of bytes from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max (int): (Optional) maximum number of bytes to read. Returns: bytes: """ length = self.ReadVarInt(max) return self.ReadBytes(length)
Example #17
Source File: epsolar.py From terkin-datalogger with GNU Affero General Public License v3.0 | 5 votes |
def read_serial(self): string = b'\xAA\x55\xEB\x90\xEB\x90\xEB\x90\x16\xA0\x00\xB1\xA7\x7F' self.config.get('ser').write(string) payload = self.config.get('ser').readline() # 1. decode payload try: data_length = payload[8] pos_data_start = 9 pos_data_stop = pos_data_start + data_length data = payload[pos_data_start:pos_data_stop] except Exception as exc: # print traceback.format_exc() print(exc) # self.output(exc) serial_read = False return False, serial_read # 2. decode data # http://docs.python.org/2/library/struct.html#format-characters try: serial_data = list(struct.unpack('hhhhhh???c????bh', data)) serial_read = True # 15 Battery temp., there is 30 difference in value with the real value (spec) serial_data[14] -= 35 # 35 seems to be the right value self.serial_data = serial_data return serial_data, serial_read except Exception as exc: serial_read = False return False, serial_read
Example #18
Source File: lean_minhash.py From datasketch with MIT License | 5 votes |
def deserialize(cls, buf, byteorder='@'): ''' Deserialize a lean MinHash from a buffer. Args: buf (buffer): `buf` must implement the `buffer`_ interface. One such example is the built-in `bytearray`_ class. byteorder (str. optional): This is byte order of the serialized data. Use one of the `byte order characters <https://docs.python.org/3/library/struct.html#byte-order-size-and-alignment>`_: ``@``, ``=``, ``<``, ``>``, and ``!``. Default is ``@`` -- the native order. Return: datasketch.LeanMinHash: The deserialized lean MinHash Example: To deserialize a lean MinHash from a buffer. .. code-block:: python lean_minhash = LeanMinHash.deserialize(buf) ''' fmt_seed_size = "%sqi" % byteorder fmt_hash = byteorder + "%dI" try: seed, num_perm = struct.unpack_from(fmt_seed_size, buf, 0) except TypeError: seed, num_perm = struct.unpack_from(fmt_seed_size, buffer(buf), 0) offset = struct.calcsize(fmt_seed_size) try: hashvalues = struct.unpack_from(fmt_hash % num_perm, buf, offset) except TypeError: hashvalues = struct.unpack_from(fmt_hash % num_perm, buffer(buf), offset) lmh = object.__new__(LeanMinHash) lmh._initialize_slots(seed, hashvalues) return lmh
Example #19
Source File: BinaryWriter.py From neo-python-core with MIT License | 5 votes |
def pack(self, fmt, data): """ Write bytes by packing them according to the provided format `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. data (object): the data to write to the raw stream. Returns: int: the number of bytes written. """ return self.WriteBytes(struct.pack(fmt, data), unhex=False)
Example #20
Source File: state.py From naz with MIT License | 5 votes |
def _find_data_coding(encoding): # NB: # We cant use all python standard encodings[1] # We can only use the ones defined in SMPP spec[2]; # # 1. https://docs.python.org/3/library/codecs.html#standard-encodings # 2. section 5.2.19 of smpp ver 3.4 spec document. try: return SmppDataCoding.__dict__[encoding] except Exception as e: raise ValueError( "That encoding: `{0}` is not a recognised SMPP encoding.".format(encoding) ) from e
Example #21
Source File: BinaryWriter.py From neo-python-core with MIT License | 5 votes |
def WriteVarInt(self, value, endian="<"): """ Write an integer value in a space saving way to the stream. Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: value (int): endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian. Raises: TypeError: if `value` is not of type int. Exception: if `value` is < 0. Returns: int: the number of bytes written. """ if not isinstance(value, int): raise TypeError('%s not int type.' % value) if value < 0: raise Exception('%d too small.' % value) elif value < 0xfd: return self.WriteByte(value) elif value <= 0xffff: self.WriteByte(0xfd) return self.WriteUInt16(value, endian) elif value <= 0xFFFFFFFF: self.WriteByte(0xfe) return self.WriteUInt32(value, endian) else: self.WriteByte(0xff) return self.WriteUInt64(value, endian)
Example #22
Source File: binary_writer.py From neo-python with MIT License | 5 votes |
def write_var_int(self, value: int, endian: str = "<") -> int: """ Write an integer value in a space saving way to the stream. Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: value: endian: specify the endianness. (Default) Little endian ('<'). Use '>' for big endian. Raises: {TypeError}: if ``value`` is not of type int. ValueError: if `value` is < 0. Returns: int: the number of bytes written. """ if not isinstance(value, int): raise TypeError('%s not int type.' % value) if value < 0: raise ValueError('%d too small.' % value) elif value < 0xfd: return self.write_bytes(bytes([value])) elif value <= 0xffff: self.write_bytes(bytes([0xfd])) return self.write_uint16(value, endian) elif value <= 0xFFFFFFFF: self.write_bytes(bytes([0xfe])) return self.write_uint32(value, endian) else: self.write_bytes(bytes([0xff])) return self.write_uint64(value, endian)
Example #23
Source File: binary_writer.py From neo-python with MIT License | 5 votes |
def _pack(self, fmt, data) -> int: """ Write bytes by packing them according to the provided format `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. data (object): the data to write to the raw stream. Returns: int: the number of bytes written. """ return self.write_bytes(struct.pack(fmt, data), unhex=False)
Example #24
Source File: binary_reader.py From neo-python with MIT License | 5 votes |
def read_var_int(self, max=sys.maxsize) -> int: """ Read a variable length integer from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max: (Optional) maximum number of bytes to read. Returns: int: """ fb = int.from_bytes(self.read_byte(), 'little') if fb is 0: return fb if fb == 0xfd: value = self.read_uint16() elif fb == 0xfe: value = self.read_uint32() elif fb == 0xff: value = self.read_uint64() else: value = fb if value > max: raise ValueError("Invalid format") return value
Example #25
Source File: BitLogic.py From basil with BSD 3-Clause "New" or "Revised" License | 5 votes |
def from_value(cls, value, size=None, fmt='Q', **kwargs): ''' Factory method For format characters see: https://docs.python.org/2/library/struct.html ''' bl = cls(**kwargs) # size is 0 by default bl.fromvalue(value=value, size=size, fmt=fmt) return bl
Example #26
Source File: BinaryWriter.py From neo-python with MIT License | 5 votes |
def WriteVarInt(self, value, endian="<"): """ Write an integer value in a space saving way to the stream. Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: value (int): endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian. Returns: int: the number of bytes written. Raises: TypeError: if `value` is not of type int. ValueError: if `value` is < 0. """ if not isinstance(value, int): raise TypeError(f'{value} not int type.') if value < 0: raise ValueError(f'{value} too small.') elif value < 0xfd: return self.WriteByte(value) elif value <= 0xffff: self.WriteByte(0xfd) return self.WriteUInt16(value, endian) elif value <= 0xFFFFFFFF: self.WriteByte(0xfe) return self.WriteUInt32(value, endian) else: self.WriteByte(0xff) return self.WriteUInt64(value, endian)
Example #27
Source File: BinaryWriter.py From neo-python with MIT License | 5 votes |
def pack(self, fmt, data): """ Write bytes by packing them according to the provided format `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. data (object): the data to write to the raw stream. Returns: int: the number of bytes written. """ return self.WriteBytes(struct.pack(fmt, data), unhex=False)
Example #28
Source File: common_func.py From passbytcp with MIT License | 5 votes |
def _start(self): # memoryview act as an recv buffer # refer https://docs.python.org/3/library/stdtypes.html#memoryview buff = memoryview(bytearray(RECV_BUFFER_SIZE)) while True: if not self.conn_rd: # sleep if there is no connections time.sleep(0.06) continue # blocks until there is socket(s) ready for .recv # notice: sockets which were closed by remote, # are also regarded as read-ready by select() r, w, e = select.select(self.conn_rd, [], [], 0.5) for s in r: # iter every read-ready or closed sockets try: # here, we use .recv_into() instead of .recv() # recv data directly into the pre-allocated buffer # to avoid many unnecessary malloc() # see https://docs.python.org/3/library/socket.html#socket.socket.recv_into rec_len = s.recv_into(buff, RECV_BUFFER_SIZE) except: # unable to read, in most cases, it's due to socket close self._rd_shutdown(s) continue if not rec_len: # read zero size, closed or shutdowned socket self._rd_shutdown(s) continue try: # send data, we use `buff[:rec_len]` slice because # only the front of buff is filled self.map[s].send(buff[:rec_len]) except: # unable to send, close connection self._rd_shutdown(s) continue
Example #29
Source File: BinaryReader.py From neo-python with MIT License | 5 votes |
def ReadVarInt(self, max=sys.maxsize): """ Read a variable length integer from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max (int): (Optional) maximum number of bytes to read. Returns: int: Raises: ValueError: if the specified `max` number of bytes is exceeded """ try: fb = self.ReadByte() except ValueError: return 0 value = 0 if fb == b'\xfd': value = self.ReadUInt16() elif fb == b'\xfe': value = self.ReadUInt32() elif fb == b'\xff': value = self.ReadUInt64() else: value = int.from_bytes(fb, "little") if value > max: raise ValueError(f"Maximum number of bytes ({max}) exceeded.") return int(value)
Example #30
Source File: BinaryReader.py From neo-python with MIT License | 5 votes |
def unpack(self, fmt, length=1): """ Unpack the stream contents according to the specified format in `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. length (int): amount of bytes to read. Returns: variable: the result according to the specified format. """ return struct.unpack(fmt, self.stream.read(length))[0]