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

Uploaded Source

Built Distributions

BinField-0.9.1-cp36-cp36m-manylinux1_x86_64.whl (733.2 kB view details)

Uploaded CPython 3.6m

BinField-0.9.1-cp36-cp36m-manylinux1_i686.whl (667.2 kB view details)

Uploaded CPython 3.6m

BinField-0.9.1-cp35-cp35m-manylinux1_x86_64.whl (701.3 kB view details)

Uploaded CPython 3.5m

BinField-0.9.1-cp35-cp35m-manylinux1_i686.whl (632.0 kB view details)

Uploaded CPython 3.5m

BinField-0.9.1-cp34-cp34m-manylinux1_x86_64.whl (724.1 kB view details)

Uploaded CPython 3.4m

BinField-0.9.1-cp34-cp34m-manylinux1_i686.whl (648.0 kB view details)

Uploaded CPython 3.4m

BinField-0.9.1-cp27-cp27mu-manylinux1_x86_64.whl (637.3 kB view details)

Uploaded CPython 2.7mu

BinField-0.9.1-cp27-cp27mu-manylinux1_i686.whl (576.0 kB view details)

Uploaded CPython 2.7mu

BinField-0.9.1-cp27-cp27m-manylinux1_x86_64.whl (637.3 kB view details)

Uploaded CPython 2.7m

BinField-0.9.1-cp27-cp27m-manylinux1_i686.whl (576.0 kB view details)

Uploaded CPython 2.7m

File details

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

File metadata

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

File hashes

Hashes for BinField-0.9.1.tar.gz
Algorithm Hash digest
SHA256 91ef47e8e1fcc428d40a4a5d8f4e222b2439ea53a02daf3ee46e52eb79d00582
MD5 970a9bbf963599ac1e1345f8a3e6f9eb
BLAKE2b-256 1c60e5970f351c87aa10498e9cc3692ab89a5e0a11743353539d38deb587bad7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 638667fe77631517e6f8e04d31c23379eec4976186318001de278701a0b4f50e
MD5 47d826c2e3256bc4733ccc82d4127748
BLAKE2b-256 593f4e3aba2f8bd0bfdecb7e39df67770352517191dbafa5ecc1d0a0ed5cced7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8149c733827c05af583f0e7fa653eec7ab2f80dff3a1f26bc50c03ecdeabaa4b
MD5 fdc6f07725237c6e513017e69225a68d
BLAKE2b-256 c07635538c5867061fb8feaddaf8b8d5fa66e20a2299d174cb943a15be5ab86b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d1fff3cff70f81eeedef2f2950f84d11583e542ea2d01376f264b9e1bb9b6f5f
MD5 56b464ca63a00b2a911262e5acf037f5
BLAKE2b-256 ee7ce39b94d711d559783009d544fb5d3b9593f933eae2e72963b424d9a6fb5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9e1c5f7fda64f3ae0c1058f2369a5237ca538ce66b3616b89419d3d180215d56
MD5 a12f5d464a0a2d9770b0d356716ffd9e
BLAKE2b-256 84c35561b3fdce89eeaa1a57adc386ea03e187af59e37899f7e646d4b17e0ce8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 353ad9627251eb0a03b53d9e35da226d1f0546d1da5376a482adef55bebc7762
MD5 a93f88947e489a4ddc98f27f6deb71df
BLAKE2b-256 b1d79403f478f90f8d6b2ac1fe2b7fd85834a440b1f0806826c184b706369d57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 dd79dc641e148202a911eba2e21ba78f992a382a7acf97482efc2b6fe6d5b3d0
MD5 4ee997463c28cfd2d01e16f1c4352e77
BLAKE2b-256 01dcc29d8af554b5977779d9fa904a5209e2ce6465547641cd3c7a0629b23630

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a1a8b09982c6ca187aeaf87e172f5103171c7dbe50e55d63b20409434b2589f5
MD5 581748bfb5278450a6135642d2ff94c6
BLAKE2b-256 35a78e7de8a9a98b2a38453debf838e4bcb0d943b1e0ee63f20ffcfff67d264f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 abab0fa39410f40cf4ec9cf7ea6bbe83cbb658d450c560ec6940cd875e90adbf
MD5 1724ea9e982829afedc28a74316a69c2
BLAKE2b-256 f43194d4160b5a0ef2dc0592912f3fb94fcafb00e63a7a71a07fc84dffafdc9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6abc20dfb9ff5ace26c50945e33ff152795614e642d3931fc200f4d09ee2b50d
MD5 5e19306a102d3015136e8465314fa9bb
BLAKE2b-256 186f209cd0ce6b0e58988c44445ceb8b36721383277558ebdb129f9452db98c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.1-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 507ca0287ac1237f649af7632f9d88077c3078ec6d277c8acd7c0d29eac8d9d1
MD5 a0b79c4190abece35e7a774ffbee38c7
BLAKE2b-256 8ee05e7e4ba432847431b7471ecbd79335dba735b0b0a959580443076b602c17

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