Skip to main content

Python BinField implementation for binary data manipulation

Project description

binfield

https://travis-ci.org/penguinolog/binfield.svg?branch=master https://coveralls.io/repos/github/penguinolog/binfield/badge.svg?branch=master Documentation Status https://img.shields.io/pypi/v/binfield.svg https://img.shields.io/pypi/pyversions/binfield.svg https://img.shields.io/pypi/status/binfield.svg https://img.shields.io/github/license/penguinolog/binfield.svg

Python binfield implementation for binary data manipulation.

Why? Python supports binary data manipulation via binary operations out of the box and it’s fast, but it’s hard to read and painful during prototyping, especially for complex (nested) structures.

This library is designed to fix this issue: it allows to operate with binary data like dict with constant indexes: you just need to define structure class and create an instance with start data. Now you can use indexes for reading and writing data

Pros:

  • Free software: Apache license

  • Open Source: https://github.com/penguinolog/binfield

  • Self-documented code: docstrings with types in comments

  • Tested: see badges on top

  • Support multiple Python versions:

Python 2.7
Python 3.4
Python 3.5
Python 3.6
Python 3.7
PyPy
PyPy3
Jyton 2.7

Usage

Not mapped objects can be created simply from BinField class:

bf = BinField(42)

Data with fixed size should be created as a new class (type): Example on real data (ZigBee frame control field):

# Describe
class ZBFrameControl(binfield.BinField):
    _size_ = 16  # Optional, used as source for mask, if mask is not defined
    _mask_ = 0xFF7F  # Optional, used as source for size, if size is not defined
    FrameType = [0, 3]  # Enum
    Security = 3
    FramePending = 4
    AckRequest = 5
    PAN_ID_Compression = 6
    SecurityNumberSuppress = 8
    InformationPresent = 9
    DstAddrMode = [10, 12]
    FrameVersion =  [12, 14]
    SrcAddrMode = [14, 16]

# Construct from frame
# (limitation: endian conversion is not supported, make it using another tools)
frame = frame = ZBFrameControl(0x0803)  # Beacon request

>>> print(frame)
<2051 == 0x0803 == (0b0000100000000011 & 0b1111111111111111)
  FrameType             = <3 == 0x03 == (0b011 & 0b111)>
  Security               = <0 == 0x00 == (0b0 & 0b1)>
  FramePending           = <0 == 0x00 == (0b0 & 0b1)>
  AckRequest             = <0 == 0x00 == (0b0 & 0b1)>
  PAN_ID_Compression     = <0 == 0x00 == (0b0 & 0b1)>
  SecurityNumberSuppress = <0 == 0x00 == (0b0 & 0b1)>
  InformationPresent     = <0 == 0x00 == (0b0 & 0b1)>
  DstAddrMode            = <2 == 0x02 == (0b10 & 0b11)>
  FrameVersion           = <0 == 0x00 == (0b00 & 0b11)>
  SrcAddrMode            = <0 == 0x00 == (0b00 & 0b11)>

>>> repr(frame)
'ZBFrameControl(x=0x0803, base=16)'

>>> print(frame.FrameType)
<3 == 0x03 == (0b011 & 0b111)>  # Get nested structure: current is flat, so we have single value

# We can use slice to get bits from value: result type is always subclass of BinField
>>> repr(frame.FrameType[: 2])
'<FrameType_slice_0_2(x=0x03, base=16) at 0x7FD0ACA57408>'

>>> frame.FrameType == 3  # Transparent comparision with integers
True

>>> int(frame.FrameType)  # Painless conversion to int
3

>>> bool(frame.AckRequest)  # And bool
False

>>> print(frame[1: 5])  # Ignore indexes and just get few bits using slice
<1 == 0x01 == (0b0001 & 0b1111)>

>>> print(ZBFrameControl.AckRequest)  # Request indexes from created data type
5

>>> print(ZBFrameControl.DstAddrMode)  # Multiple bits too
slice(10, 12, None)

# Modification of nested data (if no type conversion was used) changes original object:
>>> frame.AckRequest = 1
>>> print(frame)
<2083 == 0x0823 == (0b0000100000100011 & 0b1111111101111111)
  FrameType              = <3 == 0x03 == (0b011 & 0b111)>
  Security               = <0 == 0x00 == (0b0 & 0b1)>
  FramePending           = <0 == 0x00 == (0b0 & 0b1)>
  AckRequest             = <1 == 0x01 == (0b1 & 0b1)>
  PAN_ID_Compression     = <0 == 0x00 == (0b0 & 0b1)>
  SecurityNumberSuppress = <0 == 0x00 == (0b0 & 0b1)>
  InformationPresent     = <0 == 0x00 == (0b0 & 0b1)>
  DstAddrMode            = <2 == 0x02 == (0b10 & 0b11)>
  FrameVersion           = <0 == 0x00 == (0b00 & 0b11)>
  SrcAddrMode            = <0 == 0x00 == (0b00 & 0b11)>
>

# But remember, that nested blocks has it's own classes
>>> repr(frame.DstAddrMode)
'<DstAddrMode(x=0x02, base=16) at 0x7FD0AD139548>'

>>> fr2 = ZBFrameControl(0xFFFF)
>>> repr(fr2)
'ZBFrameControl(x=0xFF7F, base=16)'  # Mask if applied, if defined

# Fields can be set only from integers
>>> frame.SrcAddrMode = fr2.SrcAddrMode
Traceback (most recent call last):
...
TypeError: BinField value could be set only as int

>>> repr(frame['FramePending'])  # __getitem__ and __setitem__ is supported
'<FramePending(x=0x00, base=16) at 0x7FD0ACAD3188>'

Nested structures are supported, if required. Definition example (not aligned with any real data):

class NestedMappedBinField(BinField):
    test_index = 0
    nested_block = {
        '_index_': (1, 6),
        'single_bit': 0,
        'multiple': (1, 3)
    }

>>> bf = NestedMappedBinField(0xFF)
# No _size_ and no _mask_ -> size is not limited,
# but indexes can not be changed after class creation
>>> print(bf)
<255 == 0xFF == (0b11111111)
  test_index   = <1 == 0x01 == (0b1 & 0b1)>
  nested_block =
    <31 == 0x1F == (0b11111 & 0b11111)
      single_bit = <1 == 0x01 == (0b1 & 0b1)>
      multiple   = <3 == 0x03 == (0b11 & 0b11)>
    >
>

# Get nested block: nested block is structured.
>>> print(bf.nested_block)
<31 == 0x1F == (0b11111 & 0b11111)
  single_bit = <1 == 0x01 == (0b1 & 0b1)>
  multiple   = <3 == 0x03 == (0b11 & 0b11)>
>

Note: negative indexes are not supported by design!

Testing

Main test mechanism for the package binfield uses tox. Test environments available:

pep8
py27
py34
py35
py36
pypy
pypy3
pylint
docs

CI systems

For code checking several CI systems are used in parallel:

  1. Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it publishes coverage on coveralls.

  2. coveralls: is used for coverage display.

CD system

Travis CI: is used for package delivery on PyPI.

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

BinField-0.9.2.tar.gz (198.7 kB view details)

Uploaded Source

Built Distributions

BinField-0.9.2-cp36-cp36m-manylinux1_x86_64.whl (727.5 kB view details)

Uploaded CPython 3.6m

BinField-0.9.2-cp36-cp36m-manylinux1_i686.whl (668.8 kB view details)

Uploaded CPython 3.6m

BinField-0.9.2-cp35-cp35m-manylinux1_x86_64.whl (697.0 kB view details)

Uploaded CPython 3.5m

BinField-0.9.2-cp35-cp35m-manylinux1_i686.whl (636.7 kB view details)

Uploaded CPython 3.5m

BinField-0.9.2-cp34-cp34m-manylinux1_x86_64.whl (717.6 kB view details)

Uploaded CPython 3.4m

BinField-0.9.2-cp34-cp34m-manylinux1_i686.whl (651.1 kB view details)

Uploaded CPython 3.4m

BinField-0.9.2-cp27-cp27mu-manylinux1_x86_64.whl (642.6 kB view details)

Uploaded CPython 2.7mu

BinField-0.9.2-cp27-cp27mu-manylinux1_i686.whl (576.7 kB view details)

Uploaded CPython 2.7mu

BinField-0.9.2-cp27-cp27m-manylinux1_x86_64.whl (642.6 kB view details)

Uploaded CPython 2.7m

BinField-0.9.2-cp27-cp27m-manylinux1_i686.whl (576.7 kB view details)

Uploaded CPython 2.7m

File details

Details for the file BinField-0.9.2.tar.gz.

File metadata

  • Download URL: BinField-0.9.2.tar.gz
  • Upload date:
  • Size: 198.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for BinField-0.9.2.tar.gz
Algorithm Hash digest
SHA256 968d77a80c9b48a2ffd014162345b2fd8c1366f48719336a98d752ee4ca75c89
MD5 f12aecf0c49057f5e6768e749dab7be4
BLAKE2b-256 6ef39c02b72b38c21b901cd6ce719fb99c3e2c18c860860703de2da6a21032f9

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 302b8082e654eafbae69fba531ce330b94e8da56ad7d04057e76e70e4e60e5d5
MD5 fc04c4b28e0b47668ca9a9e3fc395125
BLAKE2b-256 e55b939b6f79773cb1823666cc13a82db1326e5cb3d5718146e59e722e9d5a64

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 2acb813b9ad152199e3df8c14a9512cfa80ff5eade906f47ca83971e34069f64
MD5 183f69b73498a1a6fe8a470bed4f6dce
BLAKE2b-256 ef903126e0d24b9488c5aa5c332b96f8414cd51b7c58040e8f06eff7c94ac24d

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 649d8e36a5357266372e812f6f4bfa9d7e860d7f43b060cf0a30850c571c28cc
MD5 cb40421b6b04d3c13f52b70c3871d9d7
BLAKE2b-256 f0a7c2f48d95ae69ce961672649324b60d2a6cc875324faa2ee0845a9395b4f5

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 77ee7d13bc536269eb2308f84f712aaa6c61ba0c6a3f1f1e7395699ff47e1d99
MD5 719124938b19095516a6b322724788a0
BLAKE2b-256 193b820d0fb80332a66d319ee261f69b5379a3c5fce6422b13118674f8864a6e

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e23782ca790068198b6ff69086c3db1ead95e158dd5a51805611194d0871b57d
MD5 5da9a2804e496a779718bc2f0a265e98
BLAKE2b-256 ca71522f28787de209020711e8a44e33e78591c17480fd3dbd645e680f70ac64

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 40505981dae7b36ed142f7850af60322971a4bc81c8d9a4d80867d135f3ea432
MD5 4ce8f260906754fc38c8a510098dd279
BLAKE2b-256 c5fa7ae63128f61b7a1ce369bbf73baabcda30784c62376dfcbc2536746382f2

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b1ef2e20af6a5998c7a9dbb6fa105cfa9eadb573e7d97c37aed3955384a90c37
MD5 a7d058c9c44fc8a58a018e8a53583629
BLAKE2b-256 9693a4e9f680eef101191a7c26680c824c1d28754b8fd6b1e083a5988738b49e

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ee55bbe5e8cd4c10b3b713e72fa112bd5b3e3d4e1464de414cd03a5aec482b0c
MD5 ac154fe6e026865b56a40c04c73a9218
BLAKE2b-256 0942f1878651879673c358e65ff61d88a9541ef452be5d722904f7ea3b519ba1

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7e50159a8c76ec355658d359c76ec331d180585fc538ed16371f07ae9210c701
MD5 91c1eb444c3dee12dffa031d9a96869d
BLAKE2b-256 68a4ef1f29d4a19a83dfa6fcd549c0b4653342f01ca9ec4aa739dd7658ad2307

See more details on using hashes here.

File details

Details for the file BinField-0.9.2-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for BinField-0.9.2-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 71c385209b90bb9b17b74fdd98fbf885589f7936b890a44fd6ccf9f5b9a456a2
MD5 94e1764d725932be0e8a48a0f8e7f2a9
BLAKE2b-256 f1f991cf4746b6d33aaa83463897dba195bc9e124dc02d844d0a24c05f3cbec7

See more details on using hashes here.

Supported by

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