Skip to main content

binarysearchfile

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 = (50, 50)  # 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: binary adding whitepace
10: ascii adding whitespace
20: utf-8 adding whitespace
50: int
51: 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)

Metadata

Release files for binarysearchfile 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binarysearchfile 0.2.0
File Size Uploaded
binarysearchfile-0.2.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for binarysearchfile 0.2.0
File Interpreter ABI Platform
binarysearchfile-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / binarysearchfile-0.2.0.tar.gz

Download URL binarysearchfile-0.2.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
04cbaefc12a04482b5fde3000a9975f37df00a8ceda42b64a13a518e91a5147b
BLAKE2b-256 checksum
How to use checksums
62ea9e8b2b6fb83daaad42c9ff9a26aa18b973bbd8806886b4cf08cc704a8f54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Release files / binarysearchfile-0.2.0-py3-none-any.whl

Download URL binarysearchfile-0.2.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08280c7a7cf7a0a32de25511451f6db9d1a171de08d5bbdbc2e2000283276b5b
BLAKE2b-256 checksum
How to use checksums
28dd72f96fb8e6a8f0a23c15f3873e943397701a1ebd8950e1d5cd34e596e8d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page