Skip to main content

Binary search binary file for fast random access

Project description

binarysearchfile

build status codecov pypi version python version DOI

Binary search sorted binary file for fast random access

Usage

Define and use your own binary search file:

from binarysearchfile import BinarySearchFile

class MyBinarySearchFile(BinarySearchFile):
    magic = b'\xfe\xff\x01\x01'  # magic string, you can change 2nd and 4th byte
    headerstart = b'MyBinarySearchFile'  # name of the file format
    record = (10, 10)  # record structure, here two ints, first field can be searched binarily

bsf = MyBinarySearchFile('mybinarysearchfile')
data = [(10, 42), (4, 10), (5, 5)]
bsf.write(data)  # write sorted data
print(len(bsf))  # number of records
print(bsf.search(10))  # get index
print(bsf.get(10))  # get record
print(bsf)  # print file information

#Output:
#3
#2
#(10, 42)
#MyBinarySearchFile
#     fname: mybinarysearchfile
#   records: 3
#      size: 40.00 Byte
#   recsize: 2 Byte  (1, 1)

The example above defines records consisting of two integers. The first element ("key") in the record can be searched binarily. Currently, the following types can be used out of the box:

0: ascii
1: utf-8
10: int
11: signedint

The file can be read by the original class:

bsf = BinarySearchFile('mybinarysearchfile')
print(bsf.get(10))

#Output:
#(10, 42)

The file format is specified in the module's docstring.

Defining your own data types

Use the following approach to define additional custom types with the DTypeDef class. Its init method takes arguments name, len, encode and decode. len, the byte length of an object, is usually a function of the object, but can be an integer for a fixed length. Register custom types only with keys greater than 99.

from binarysearchfile import BinarySearchFile, DTypeDef

class MyBinarySearchFile(BinarySearchFile):
    DTYPE = BinarySearchFile.DTYPE.copy()
    DTYPE[100] = DTypeDef(
        'fixedlenint', 5,
        encode=lambda v, s: v.to_bytes(s),
        decode=lambda v: int.from_bytes(v)
        )
    # definitions of other class properties follow

Use binary sequential file

We provide a BinarySequentialFile class that uses the same file layout and can be used for sequential reading and writing.

from binarysearchfile import BinarySequentialFile
with BinarySequentialFile('mybinarysearchfile') as bseqf:
    print(bseqf[2])
#Output:
#(10, 42)

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

binarysearchfile-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

binarysearchfile-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: binarysearchfile-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for binarysearchfile-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8b348ddab5cf1807dd9104013aeb643c00dcb8612904da33f3576783df20db31
MD5 7e5fb4a82eaaebd6cda8d8d22c81f18e
BLAKE2b-256 cd459d83ac223f5dba9d5b4f7aac3f13da1f2e3cb045810d59a5b9059c30bf08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for binarysearchfile-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ad383c13d8d4060e39c8737d58cf7c51319c66e658521a998132be2f12d6e60
MD5 e0a4e9760f713e10403b03b9c4e9b480
BLAKE2b-256 05722c0c3f483ed2c65651b68e7f18c8ad2f24a8583c17c9f13373b00dd20495

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