Skip to main content

A Python library for reading and writing binary sources.

Project description

Pyjak

A Python library for reading and writing binary sources. The standard library package struct has many caveats and is not very user friendly. We wrap it for you and present a slick api with sane exception handling.

Build Status

Installation

The easiest way to install is through pip:

pip3 install pyjak

Examples

Heres how you would serialize a common integer:

from pyjak import dump_int32
_bytes = dump_int32(1)
print(_bytes)

Result: (On little endian systems)

b'\x01\x00\x00\x00'

And to turn it back into an integer:

from pyjak import parse_int32
_int = parse_int32(b'\x01\x00\x00\x00')
print(_int)

Result: (On little endian systems)

1

Unsigned integers

All integer functions have unsigned versions as well. Lets say you want to fit the number 4000000000 into a 4 byte integer. You would need the unsigned version:

from pyjak import dump_uint32
_bytes = dump_uint32(4000000000)
print(_bytes)

Result: (On little endian systems)

b'\x00(k\xee'

And to turn it back into an integer:

from pyjak import parse_uint32
_int = parse_uint32(b'\x00(k\xee')
print(_int)

Result: (On little endian systems)

4000000000

Byte order

As you may have noticed, we didn't need to specify the byte order (or endianness) of the byte array. This is because all functions in pyjak defaults to the native byte order of your system.

But you can also specify which byte order you want by using the ByteOrder enumeration (except when calling int8 or uint8 functions). The available orders are:

from pyjak import ByteOrder
ByteOrder.LITTLE
ByteOrder.BIG
ByteOrder.NATIVE
print(ByteOrder.NATIVE == ByteOrder.LITTLE)

Result: (On little endian systems)

True

You can specify what byte order the serialized output should have:

from pyjak import dump_int32, ByteOrder
_bytes = dump_int32(1, ByteOrder.BIG)
print(_bytes)

Result:

b'\x00\x00\x00\x01'

Or when parsing:

from pyjak import parse_int32, ByteOrder
_int = parse_int32(b'\x00\x00\x00\x01', ByteOrder.BIG)
print(_int)

Result:

1

Booleans

You can also serialize booleans. Booleans are assumed to represented as an unsigned 1 byte integer, where 0 means False and any other value means True.

Supported data types

  • int8 (Signed 1 byte integer)
  • uint8 (Unsigned 1 byte integer)
  • int16 (Signed 2 byte integer)
  • uint16 (Unsigned 2 byte integer)
  • int32 (Signed 4 byte integer)
  • uint32 (Unsigned 4 byte integer)
  • int64 (Signed 8 byte integer)
  • uint64 (Unsigned 8 byte integer)
  • float32 (4 byte single precision float)
  • float64 (8 byte double precision float)
  • bool (unsigned 1 byte integer)

Supported versions

  • Python 3.4
  • Python 3.5
  • Python 3.6
  • Python 3.7

Issues

Report issues using the issue tracker on the github repo.

Changelog

v0.1.0

  • Initial release.

Authors

  • Max Byrde (miniwa)

License

MIT (see LICENSE)

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

pyjak-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjak-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pyjak-0.1.1.tar.gz.

File metadata

  • Download URL: pyjak-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

File hashes

Hashes for pyjak-0.1.1.tar.gz
Algorithm Hash digest
SHA256 72b2a82c589eaf4969b1d106644918cec7d65b9c3bef6a53d4dca0cca8100fd6
MD5 772a1eeb0c2f2bb6d6ee11cb995b92b2
BLAKE2b-256 abe394eb1c9105a6fb28c70809df9f3059619e8568c7110e3afd43e9feba4450

See more details on using hashes here.

File details

Details for the file pyjak-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyjak-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

File hashes

Hashes for pyjak-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5669e5aa2b8527f841087561af94c201ec1044f073c676ff845e024eeea75762
MD5 8922cd5b137a5b260ac4101b2f2c74e8
BLAKE2b-256 4c3575e74c1963ba0d5358c0b15a3f6cdddc4deea6cb1dabb906f03e675c6707

See more details on using hashes here.

Supported by

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