Skip to main content

A base class for creating binary parsing and packing classes

Project description

Dataclass for go to and from binary data

It follows dataclass pattern with typehinting as the binary format. Temperature with one unsigned byte:

>>> class Temperature(BinmapDataclass):
...     temp: unsignedchar = 0
...
>>> t = Temperature()
>>> t.temp = 22
>>> print(bytes(t))
b'\x16'

>>> t2 = Temperature(b'\x20')
>>> print(t2.temp)
32

Temperature and humidity consisting of one signed byte for temperature and one unsiged byte for humidity:

>>> class TempHum(BinmapDataclass):
...     temp: signedchar = 0
...     hum: unsignedchar = 0
...
>>> th = TempHum()
>>> th.temp = -10
>>> th.humidity = 60
>>> print(bytes(th))
b'\xfc<'

>>> th2 = TempHum(b'\xea\x41')
>>> print(th2.temp)
-22
>>> print(th2.hum)
65

Datatypes

Binmap supports all datatypes that standard library struct has. The types works as typehints in the dataclass. When giving an attribute a typehinted datatype it will be added to the binary output from the class.

Padding

Padding is a field type and datatype that is length bytes long, and will not show up as attributes in the dataclass. Trying to assign it a value will be silently ignored and reading from will raise the AttributeException exception.

>>> class PaddedData(BinmapDataclass):
...     temp: signedchar = 0
...     pad1: pad = padding(length = 5)
...
>>> pd = PaddedData()
>>> pd.temp = 14
>>> print(bytes(pd))
b'\x0e\x00\x00\x00\x00\x00'

Constant

Constant is fieldtype that always is the given value. Constant could be of any datatype.

>>> class Constant(BinmapDataclass):
...     signature: unsignedshort = constant(0x1313)
...     temp: unsignedchar = 0
...
>>> c = Constant()
>>> c.temp = 18
>>> print(bytes(c))
b'\x13\x13\x12'

>>> print(c.signature)
4883
>>> print(c.temp)
18

>>> c.signature = 10
AttributeError: signature is a constant

Enums

Enumfield maps agaings IntEnum or IntFlag so that you could set the value either as the enum or as the numeric value.

>>> class WindEnum(IntEnum):
...     North = 0
...     East = 1
...     South = 2
...     West = 3
...
>>> class Wind(BinmapDataclass):
...     speed: unsignedchar = 0
...     direction: unsignedchar = enumfield(WindEnum, default=WindEnum.East)
...
>>> w = Wind()
>>> print(w)
Wind(speed=0, direction=<WindEnum.East: 1>)
>>> w.direction = WindEnum.West
>>> print(w.direction)
<WindEnum.West: 3>
>>> w.direction = 2
>>> print(w.direction)
<WindEnum.South: 2>

Autolength

Autolenght field types counts number of bytes in the output, including the autolength field it self. You can’t set an autolenght field. Autolength can be offseted, for example to ignore it’s own length.

>>> class MyBinStruct(BinmapDataclass):
...     length: unsignedchar = autolength()
...     temp: signedchar = 0
...
>>> mb = MyBinStruct()
>>> print(mb)
MyBinStruct(length=2, temp=0)
>>> mb.length = 10
AttributeError: length is a constant

Calculated fields

Calculated fields calls a function when data is converted to binary value. The function must be declared when the field is added.

>>> class WithChecksum(BinmapDataclass):
...     temp: signedchar = 0
...     hum: unsignedchar = 0
...     def chk(self) -> unsignedchar:
...         return (self.temp + self.hum) & 0xFF
...     checksum: unsignedchar = calculatedfield(chk)
...
>>> wc = WithChecksum()
>>> wc.temp = -20
>>> wc.hum = 10
>>> print(wc)
WithChecksum(temp=-20, hum=10, checksum=246)
>>> print(bytes(wc))
b'\xec\n\xf6'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binmap-1.1.0.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file binmap-1.1.0.tar.gz.

File metadata

  • Download URL: binmap-1.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for binmap-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8cbc1b029665f64f6a0166cfdcde053669a63bbceaf6cd526017bcde8bb72570
MD5 88cf057c88064e97ae21a6be5af041e7
BLAKE2b-256 c5fd49922ed5e4185227fb5ce8ac90a9121a2d1827d31bb2c0fe64dcc63ae996

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page