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

Uploaded Source

Built Distributions

BinField-0.9.0-cp36-cp36m-manylinux1_x86_64.whl (698.8 kB view details)

Uploaded CPython 3.6m

BinField-0.9.0-cp36-cp36m-manylinux1_i686.whl (640.4 kB view details)

Uploaded CPython 3.6m

BinField-0.9.0-cp35-cp35m-manylinux1_x86_64.whl (668.1 kB view details)

Uploaded CPython 3.5m

BinField-0.9.0-cp35-cp35m-manylinux1_i686.whl (605.4 kB view details)

Uploaded CPython 3.5m

BinField-0.9.0-cp34-cp34m-manylinux1_x86_64.whl (688.9 kB view details)

Uploaded CPython 3.4m

BinField-0.9.0-cp34-cp34m-manylinux1_i686.whl (620.9 kB view details)

Uploaded CPython 3.4m

BinField-0.9.0-cp27-cp27mu-manylinux1_x86_64.whl (614.4 kB view details)

Uploaded CPython 2.7mu

BinField-0.9.0-cp27-cp27mu-manylinux1_i686.whl (560.8 kB view details)

Uploaded CPython 2.7mu

BinField-0.9.0-cp27-cp27m-manylinux1_x86_64.whl (614.4 kB view details)

Uploaded CPython 2.7m

BinField-0.9.0-cp27-cp27m-manylinux1_i686.whl (560.8 kB view details)

Uploaded CPython 2.7m

File details

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

File metadata

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

File hashes

Hashes for BinField-0.9.0.tar.gz
Algorithm Hash digest
SHA256 612b615cc46a6057d165a9b16e4ea8a3722145b6656fa4144f417597c0260a62
MD5 9a58eca0f325d69fc822190500a3c26b
BLAKE2b-256 e4e0eba3ad91ea40087006ef2258e3794ab73fef055d5f7f2c928a3fb6821759

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f7916e1a6fcb8e9a14082688495518b45b172862dee75f879e11b3a98a42ff9f
MD5 1702562837812b2d9f91909c33699529
BLAKE2b-256 91e38ce1c3880b0ef6df35c422ce2311f4aec32388e8650d3f6444d473dbd81b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b865932d9cfb76efbea69f6b33758fe4cd6e65052b8ea5d6181e2daac9e81699
MD5 111ae5207f2fbb567ec300811a7103ef
BLAKE2b-256 b461617cf709de95dad77fad25c98671e356246949390e80409245e5f46882bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 00642cc4f31c4ff5b8a69f64913b6e08d9ec055609c4aec51bfab86b40ee6464
MD5 07ed6063f3cdd984140f2efd078790b6
BLAKE2b-256 d0bf569b3e38dbc9c4d39bfcb832bfb9fcbc52a07c2ddde44631e2ae31fd762d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 899b658c3e2e5b65b70fe3dc801c7b0fb22faca7b4bbf4f93d3e3b781f3ec715
MD5 c871b8b42f158a642d4702dec7616e39
BLAKE2b-256 f30ebe15315cd08b8ec37695d577184a3e4b322fe6f3da14ad199c45c910cbc0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 19cfd5933d1f5fb00670ca72510c3b0636a8d8ebf6bcf7de23e94f55087049b1
MD5 77d26551a3341b7617b32abbd11f8cbf
BLAKE2b-256 85956fd113058403385afec1649460ba2ff85bf4444cc5a447c7011b7c000bbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f790869c9f62323ee038e46e14e39b9a73932bdaba426c55564d3e9309496990
MD5 849c09c3713787b851fb464e1bee398a
BLAKE2b-256 b6339cfe155d9712236fc8ece15c709b905fd583a2b73d54735d9a583c84e427

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 385949e735aefa633db0bb61ee293ac8f6bad1373e201a4458520a09dd41e4ea
MD5 a9deb2a760d5722d3cdeea7bc801e93c
BLAKE2b-256 2e9bfe6d504b83daf1ec3ae579964599fa1cd4d26057a79992aa7dec70207fb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3d7cec3952428f71e971ffb7a72a9d08e9a00ad2cb0c6734ca13a9695279461b
MD5 e35565110843ec19e4e0cb8bcd1e0006
BLAKE2b-256 35765a09e0892acb544c2835d0e23fe992d3955a6f4d7ffe009e335699a692e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 63de0673305b2c554efd4807290b1180e3071bf1489abaa09cc2a134601828f2
MD5 f0594666d7f920d69250c621d14d6367
BLAKE2b-256 c094fe6d6db94c7918bdb2623c12a40656df8c36092f54faf60472c82769d768

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for BinField-0.9.0-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 cae98172a2a09dfe14cea78331fad5596df0f5e26111a44c286f55878dd2358f
MD5 8e96787a03b122702fb24ad3acbbeca7
BLAKE2b-256 005495f25c6bd682329592ff7994b06a71f42b5abb8a531406ee821af24d8e8b

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