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
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.8.3.tar.gz (177.7 kB view details)

Uploaded Source

Built Distributions

BinField-0.8.3-cp36-cp36m-manylinux1_x86_64.whl (674.1 kB view details)

Uploaded CPython 3.6m

BinField-0.8.3-cp36-cp36m-manylinux1_i686.whl (611.0 kB view details)

Uploaded CPython 3.6m

BinField-0.8.3-cp35-cp35m-manylinux1_x86_64.whl (638.8 kB view details)

Uploaded CPython 3.5m

BinField-0.8.3-cp35-cp35m-manylinux1_i686.whl (577.5 kB view details)

Uploaded CPython 3.5m

BinField-0.8.3-cp34-cp34m-manylinux1_x86_64.whl (652.7 kB view details)

Uploaded CPython 3.4m

BinField-0.8.3-cp34-cp34m-manylinux1_i686.whl (592.7 kB view details)

Uploaded CPython 3.4m

BinField-0.8.3-cp27-cp27mu-manylinux1_x86_64.whl (585.5 kB view details)

Uploaded CPython 2.7mu

BinField-0.8.3-cp27-cp27mu-manylinux1_i686.whl (536.4 kB view details)

Uploaded CPython 2.7mu

BinField-0.8.3-cp27-cp27m-manylinux1_x86_64.whl (585.4 kB view details)

Uploaded CPython 2.7m

BinField-0.8.3-cp27-cp27m-manylinux1_i686.whl (536.4 kB view details)

Uploaded CPython 2.7m

File details

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

File metadata

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

File hashes

Hashes for BinField-0.8.3.tar.gz
Algorithm Hash digest
SHA256 d94b2f83d90bdf6525f072e211be37fa10576e99f86675958e49bd8ff8713023
MD5 df02beb6c7c415d4759d9d7e31685dfe
BLAKE2b-256 e4830d409b82754ce74c4cb227a7b0f616dabeb29928666b63a44e3656c59a4e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fa7673d88b158a18231aa2f916c1b84ee4c775a479c1db4f0f117ed0fc810226
MD5 70618e1a6b37f660acf666711185281c
BLAKE2b-256 0becc085ace470869059d0dddf2e055d25d9013710feac45422e0759fab3cec5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6366289e26a259c5d8eb652772efaaabc563441628dfb5a2140c4f36333e2019
MD5 8732e2943d17a586ab5e13dd75951112
BLAKE2b-256 64f2e2928b33f211b68b54239be8d2bc974673cf589bc84c4b1ace5869a9c440

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e28bd86263ab01964fdbd92d03157fed80eb54806f0c77618bbde17398611206
MD5 f65471ee8024c7425d40b9cc63ec03d0
BLAKE2b-256 847bef67184d07970bf65f1c55508dcb3211bdac6627790f1b3cef8e547d114f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f219e95d816f709d744b794008ff0afe95442da96eff947ab21576da01709d11
MD5 aedc44f6afc69c1ca2a8c87217e7697f
BLAKE2b-256 bebd05cdd743f43471e7a943a17770f053f6eb2033b4e6352c3d63edd634594b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ece276a8fad313e8ff77ffaa461375108cff4c3b591434f62ee982c34f486d9e
MD5 30c624cef209e72ca90bb8d0a70d8a91
BLAKE2b-256 5d63f1a8538980a05ea605953dce3d5bb9bb5844aa32b42adcf39dc7b857c44a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 29066055d449fcc9d6ecad1645edceba7b02e37e0ef1e2449a94c65ca2c2b7db
MD5 3b38cccc93b493656a3c4dc44f9652fc
BLAKE2b-256 b06568fccc9aad6bc02efffd83c321e1ebbbe63e6c24924f36f74132f6f45745

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ca89331276c43760d8b3fd4687020955db8b61a49a05aec505fcd37f2cae1eb2
MD5 f090439f20bd0525572b29a8374bf948
BLAKE2b-256 d3f88dd9761a8fa5d1b59eb8bc7d208d0a566aa633507797867fc8ade85109ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4d57dc518b3f8eb2ab8f10fcfd98d6021fefcc204d85a7178d8ae5787e987f15
MD5 6e13c33d968bce6ec89e252993f33204
BLAKE2b-256 4fb0c3833a2bd121b5c25d1fb4795582b0839926b37c245a0b6882f331aeb04f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5925629ff283f063ad3d7aac151d2c2e52ca6ff1658788930c1c21cbaa954412
MD5 a4410f35e972d675503f756be2914b1e
BLAKE2b-256 5e0deeb866669b1e7db925fb922b661c6801bf25d473f82a4e55fee978457a34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.8.3-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 db3e9edc8bdf30443f3ce05063b31c51ca64769e864eb758f58e213ca02d324a
MD5 11775b72ddb584e206086b9af77e00b1
BLAKE2b-256 d3c04935b17ccc1a1ec895e5201445b552b5280209f138a486ee541508961148

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