Skip to main content

Binary search binary file for fast random access

Project description

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)

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.2.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

binarysearchfile-0.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for binarysearchfile-0.2.0.tar.gz
Algorithm Hash digest
SHA256 04cbaefc12a04482b5fde3000a9975f37df00a8ceda42b64a13a518e91a5147b
MD5 22c875547abd8bbdfecb66868493a9db
BLAKE2b-256 62ea9e8b2b6fb83daaad42c9ff9a26aa18b973bbd8806886b4cf08cc704a8f54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for binarysearchfile-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08280c7a7cf7a0a32de25511451f6db9d1a171de08d5bbdbc2e2000283276b5b
MD5 af03a2640f478df5eb967c03ef48b89e
BLAKE2b-256 28dd72f96fb8e6a8f0a23c15f3873e943397701a1ebd8950e1d5cd34e596e8d5

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