Skip to main content

This python module implements a container for data blocks.

Project description

jk_bincontainer

Introduction

This python module implements a container for data blocks.

Information about this module can be found here:

Why this module?

Sometimes it is more convenient to store a single file instead of multiple ones. For purposes of reuse of such kind of functionality this module has been created. It implements a container that allows adding binary data blocks, store the whole set ob blocks in a single file, and load everything again later.

This module is heavily inspired by the internal structure of PNG files.

Limitations of this module

Every data block must have a unique key so that it can be identified within the container. This key must consist of exactly 4 ASCII characters.

All data handling is done in memory. Therefore it is recommended to not use this container for huge amounts of data. (This container is not intended to meet such requirements.)

How to use this module

Import this module

Please include this module into your application using the following code:

import jk_bincontainer

Create a data container

To create an empty container simply construct it:

bc = BinContainer()

The container is now ready for use.

Add a data block

You can add binary data blocks like this:

bc.addBinaryBlock("abcd", b"\x01\x02\x03")

This data block will be added as a binary data block. In the current implementation there are only binary data blocks. In the future more blocks of different kind might be supported. Please note that because of this reason a data block is associated with a type identifier internally that is used implicitely if addBinaryBlock() (or other methods in the future) are called.

Retrieve the block again

Of course, you can find the block again later:

blockType, rawData = bc.getBlockByKeyE("abcd")

As stated above data blocks are associated with a type identifier. For now only binary data blocks are supported, therefore blockType will always receive "bin" currently.

All named data retrieval methods return such a tuple. If you retrieve by index you will receive a triple:

blockKey, blockType, rawData = bc.getBlockByIndexE(0)

Again blockType will contain "bin" as right now only binary data blocks are supported.

In general the following methods are available:

Method Argument(s) Return Value(s)
getBlockByIndexE int index str blockKey, str blockType, data
getBlockByIndex int index str blockKey, str blockType, data
getBlockByKeyE str key str blockType, data
getBlockByKey str key str blockType, data

Serialize the data container

The data container can be serialized like this:

rawBytes = bytes(bc)

Or:

rawBytes = bc.toBytes()

Both approaches will construct a bytes object with data currently stored in the container.

Additionally you could construct a bytearray object if you are interested in bytearray instead of bytes:

rawByteArray = bc.toByteArray()

Write the data container to disk

The data container can be serialized and written to disk. Example:

bc.writeToFile("/path/to/myfile")

Loading the data from disk

If you have written the data to disk you can load them again. Example:

bc.loadFromFile("/path/to/myfile")

Deserialize data

Alternatively you might already have binary data and want to load directly from it. Example:

bc.loadFromData(binData)

Contact Information

This work is Open Source. This enables you to use this work for free.

Please have in mind this also enables you to contribute. We, the subspecies of software developers, can create great things. But the more collaborate, the more fantastic these things can become. Therefore Feel free to contact the author(s) listed below, either for giving feedback, providing comments, hints, indicate possible collaborations, ideas, improvements. Or maybe for "only" reporting some bugs:

License

This software is provided under the following license:

  • Apache Software License 2.0

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

jk_bincontainer-0.2021.10.30.tar.gz (8.6 kB view details)

Uploaded Source

File details

Details for the file jk_bincontainer-0.2021.10.30.tar.gz.

File metadata

  • Download URL: jk_bincontainer-0.2021.10.30.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.10

File hashes

Hashes for jk_bincontainer-0.2021.10.30.tar.gz
Algorithm Hash digest
SHA256 4484c2f6c29783c2aedba904bbc1adea939805973c5cc5170cf469446db48b8f
MD5 f0862c1fe9dbdf53030340074ec34b21
BLAKE2b-256 cb5008e942cf3d0fd465ab665f6cf75a62692d8ed08c81a05fe4402f4b05d2ba

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