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 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.

Pros:

  • Free software: Apache license

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

  • Self-documented code: docstrings with types in comments

  • Tested: see bages on top

  • Support miltiple Python versions:

Python 2.7
Python 3.4
Python 3.5
Python 3.6
PyPy

Usage

Not mapped objects could be created simply from BinField class:

bf = BinField(42)

Data with fixed size should be created as new class (type):

class TwoBytes(BinField):
    _size_ = 16  # Size in bits


bf = TwoBytes(42)
2 == len(bf)  # Length is in bytes for easier conversion to bytes

Also binary mask could be attached and data will be always conform with it:

class MyBinField(BinField):
    _mask_ = 0b11
    _size_ = 8


bf = MyBinField(5)
0b001 == bf  # Mask was applied and 0b101 & 0b011 = 0b001

Mapped objects is also should be created as new class (type):

class MyBinField(BinField):
    first = 0  # Single bit
    two_bits = [1, 3]  # Also could be mapped as tuple and slice
    _mask_ = 0b1011


bf = MyBinField(0b1101)
0b1001 == bf
4 == bf._size_  # Size is generated during creation from mask
0b01 == bf.two_bits._mask_  # Mask is inherited from parent object

Nested mapping is supported:

class MyBinField(BinField):
    first = 0  # Single bit
    two_bits = [1, 3]  # Also could be mapped as tuple and slice
    nested = {
        '_index_': [3, 8],  # Index is mandatory, it should be slice, list or tuple
        'nested_bit': 0,  # In nested objects use relative indexing
        'nested_bits': [1, 3]
    }
    # Nested objects could contain less indexed area, than block size,
    # but mask will be calculated from outer level indexes only.


bf = MyBinField(0xFF)
0b00011111 == bf.nested
# Nested received (generated as all bits in range) mask from top
# and size from slice
1 == bf.nested.nested_bit  # __getitem__ and properties is available
bf.nested.nested_bit = 0  # property has setters
0b11110111 == bf  # Change on nested is returned to main object

Testing

The main test mechanism for the package binfield is using tox. Test environments available:

pep8
py27
py34
py35
pypy
pylint

CI systems

For code checking several CI systems is used in parallel:

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

  2. coveralls: is used for coverage display.

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

Uploaded Source

Built Distributions

BinField-0.1.0-py3.5.egg (8.5 kB view details)

Uploaded Source

BinField-0.1.0-py2.py3-none-any.whl (10.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for BinField-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4cae201bd8b002347be1dfaf5181df211474f6843b07103338702e6d14ad9926
MD5 c7816fc0e38881b226565bd52b31cd30
BLAKE2b-256 4ed25a92655b0aeb068cd7276b5e9f9d900cc7acc44c76fddf3a5206d24349fb

See more details on using hashes here.

File details

Details for the file BinField-0.1.0-py3.5.egg.

File metadata

File hashes

Hashes for BinField-0.1.0-py3.5.egg
Algorithm Hash digest
SHA256 78af4a7dfe96b47309867b4f72f5e2e4e37873334a68b305c34dd11a600a79ef
MD5 a99d17e18f3a287ac510bbd63c57b5c2
BLAKE2b-256 55ab9f3c446ac2c496788fa0bc4c292ec6eadffcffc919bf417436986ce565e3

See more details on using hashes here.

File details

Details for the file BinField-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for BinField-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e6ce4fff0267058542faf0c04785140c646ee4a3be95ba07bb761298c23d8757
MD5 f0c903469fcf59a5f1569fff2ed209c3
BLAKE2b-256 d9296b18bd059524facf07e6e24caf7857cf1d2525bb0a0230c182a16cffc75a

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