Skip to main content

Batch records based on size constraints

Project description

Record batcher

This is a simple record batcher that takes a list of records and batches them into lists of records. The batcher is implemented in Python and is available as a Python package.

Installation

The package can be installed using pip:

pip install record-batcher

Usage

The package provides a single class RecordBatcher which takes the following parameters:

  • max_record_size: the maximum size of a record in bytes (int, default: 1MB),
  • max_batch_size:: the maximum size of a batch in bytes (int, default: 5MB),
  • max_records_per_batch: the maximum number of records per batch (int, default: 500),
  • include_list_object_in_batch_size: bool = False: whether to include the size of the list object in the batch size (default: False),
  • include_garbage_collector_overhead: bool = False: whether to include the garbage collector overhead in any object size (default: False).

batch_records

The main method of the class is batch_records which takes a list of records and returns a list of batches. The method takes only one parameter:

  • records: a list of records to be batched (list of strings),

The method returns a list of batches where each batch is a list of records. The method batches the records in such a way that the size of each batch does not exceed the maximum batch size and the number of records in each batch does not exceed the maximum number of records per batch. The method also ensures that the size of each record does not exceed the maximum record size and if it does, the record is discarded.

generate_records

The method generate_records generates a list of records of specified size. The method takes the following parameters:

  • num_records: the number of records to generate (int, default: 1),
  • record_sizes: the sizes of the records to generate (list of ints OR None, default: None),
  • random_sizes: whether to generate random sizes for the records of max size equal to record_size (bool, default: False),
  • record_size: the size of each record in bytes (int, default: 1024). If random_sizes is set to True, this is the maximum size of the records

The method returns a list of records where each record is a string of the specified size. If record_sizes is provided, the method generates records of the specified sizes. If random_sizes is set to True, it generates records of random sizes between empty string size and record_size. If record_sizes is None and random_sizes is False, the method generates records of size record_size.

Example

This example generates 10 records of random sizes between empty string size and 64 bytes and batches them into lists of records where each batch does not exceed 256 bytes and contains at most 10 records.

from record_batcher import RecordBatcher


record_batcher = RecordBatcher(max_record_size=64, max_batch_size=160, max_records_per_batch=10)

records = record_batcher.generate_records(num_records=10, random_sizes=True, record_size=64)
batches = record_batcher.batch_records(records)

print(f"Number of batches: {len(batches)}\n")
for batch in batches:
    print(
        f"Number of records in batch: {len(batch)}\n"
        f"Records: {batch}\n"
    )

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

record_batcher-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

record_batcher-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: record_batcher-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Windows/11

File hashes

Hashes for record_batcher-0.1.0.tar.gz
Algorithm Hash digest
SHA256 78d4c6a812b9d2569173901382c082269db5f340c4cf79ea24d57c9620f33703
MD5 24eb0a4b052309e34a2261e98cf61836
BLAKE2b-256 0c896b9d697c77ddc43172129e772cf788fb7b2623ea879fd3dd6091c1f1e140

See more details on using hashes here.

File details

Details for the file record_batcher-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: record_batcher-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Windows/11

File hashes

Hashes for record_batcher-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68343fd461723fb02b66ae30dee56041e6de071cf7278ad220ca97aa4566f53b
MD5 bf7a1138c87f1846e85c6fbc8711c48f
BLAKE2b-256 5c13acabe3bd4085e5b2fc630ed2778cd8b8856a138a9a171b5059bd1a22ec70

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