Skip to main content

Facilities associated with binary data.

This module requires Python 3 and recommends Python 3.6+ because it uses abc.ABC, because a Python 2 bytes object is too weak (just a str) as also is my cs.py.bytes hack class and because the keyword based Packet initiialisation benefits from keyword argument ordering.

* Cameron Simpson <cs@cskk.id.au> 22jul2018

Class BytesesField

MRO: PacketField, abc.ABC
A field containing a list of bytes chunks.

The following attributes are defined:

  • value: the gathered data as a list of bytes instances, or None if the field was gathered with discard_data true.
  • offset: the starting offset of the data.
  • end_offset: the ending offset of the data.

The offset and end_offset values are recorded during the parse, and may become irrelevant if the field's contents are changed.

Class BytesField

MRO: PacketField, abc.ABC
A field of bytes.

Class BytesRunField

MRO: PacketField, abc.ABC
A field containing a continuous run of a single bytes value.

The following attributes are defined:

  • length: the length of the run
  • bytes_value: the repeated bytes value

The property value is computed on the fly on every reference and returns a value obeying the buffer protocol: a bytes or memoryview object.

Class EmptyPacketField

MRO: PacketField, abc.ABC
An empty data field, used as a placeholder for optional fields when they are not present.

The singleton EmptyField is a predefined instance.

Function fixed_bytes_field(length, class_name=None)

Factory for BytesField subclasses built from fixed length byte strings.

Function flatten(chunks)

Flatten chunks into an iterable of bytes instances.

This exists to allow subclass methods to easily return ASCII strings or bytes or iterables or even None, in turn allowing them to simply return their superclass' chunks iterators directly instead of having to unpack them.

Class Int16BE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '>h'.

Class Int16LE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '<h'.

Class Int32BE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '>l'.

Class Int32LE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '<l'.

Class ListField

MRO: PacketField, abc.ABC
A field which is a list of other fields.

Function multi_struct_field(struct_format, subvalue_names=None, class_name=None)

Factory for PacketField subclasses build around complex struct formats.

Parameters:

  • struct_format: the struct format string
  • subvalue_names: an optional namedtuple field name list; if supplied then the field value will be a namedtuple with these names
  • class_name: option name for the generated class

Class Packet

MRO: PacketField, abc.ABC
Base class for compound objects derived from binary data.

Class PacketField

MRO: abc.ABC
A record for an individual packet field.

Function struct_field(struct_format, class_name)

Factory for PacketField subclasses built around a single struct format.

Parameters:

  • struct_format: the struct format string, specifying a single struct field
  • class_name: the class name for the generated class

Example:

>>> UInt16BE = struct_field('>H', class_name='UInt16BE')
>>> UInt16BE.__name__
'UInt16BE'
>>> UInt16BE.format
'>H'
>>> UInt16BE.struct   #doctest: +ELLIPSIS
<Struct object at ...>
>>> field, offset = UInt16BE.from_bytes(bytes((2,3,4)))
>>> field
UInt16BE(515)
>>> offset
2
>>> field.value
515

Function structtuple(class_name, struct_format, subvalue_names)

Convenience wrapper for multi_struct_field.

Class UInt16BE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '>H'.

Class UInt16LE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '<H'.

Class UInt32BE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '>L'.

Class UInt32LE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '<L'.

Class UInt64BE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '>Q'.

Class UInt64LE

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format '<Q'.

Class UInt8

MRO: PacketField, abc.ABC
A PacketField which parses and transcribes the struct format 'B'.

Class UTF8NULField

MRO: PacketField, abc.ABC
A NUL terminated UTF-8 string.

Release files for cs-binary 20180810

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs-binary 20180810
File Size Uploaded
cs.binary-20180810.tar.gz 9.4 kB Details

Release files / cs.binary-20180810.tar.gz

Download URL cs.binary-20180810.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
91395927b8bd582d36f9338066aa982c9a0f23b452a628c36505e06784078db1
BLAKE2b-256 checksum
How to use checksums
bf570a4194bc76f74746504818cb189af7d37f2652383d0588bc9a7ae074bed0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page