Byte tries in Rust with Python bindings
Installation
Install from PyPI (only built for Python 3.10+ on Linux)
pip install byte-prefix-tree
Manual installation
Make sure you have Rust installed, then do the following:
git clone https://github.com/bastiscode/byte-trie.git
cd byte-trie
pip install maturin[patchelf]
maturin develop --release
Usage
Currently implemented tries:
- Patricia trie
- Adaptive radix trie
Key needs to be a bytes object, value can be anything. Make sure that the key never contains a 255 byte, as it is used as a terminator internally. For utf8-encoded text this is always the case, but for other types of data you might need to encode it in a way that ensures this.
from byte_trie import PatriciaTrie, AdaptiveRadixTrie
pt = PatriciaTrie()
# add key-value pairs
pt.insert(b"hello", 1)
pt.insert(b"world", 2)
# delete key
print(pt.delete(b"hello")) # 1
# check for keys and prefixes
print(pt.contains(b"hello")) # False
print(pt.contains(b"world")) # True
print(pt.contains(b"wor")) # False
print(pt.contains_prefix(b"hel")) # False
print(pt.contains_prefix(b"wor")) # True
# get values
print(pt.get(b"hello")) # None
print(pt.get(b"world")) # 2
# overwrite
print(pt.insert(b"world", 3)) # 2
print(pt.get(b"world")) # 3
# continuations for prefix
print(pt.continuations(b"wo")) # [(b'world', 3)]
# prefixes of some key, returns list of (prefix length, value) tuples
key = b"world cup"
prefix_matches = pt.prefix_matches(key)
print(prefix_matches) # [(5, 3)]
print(
[(key[:length].decode(), value) for length, value in prefix_matches]
) # [('world', 3)]
# same for ART
art = AdaptiveRadixTrie()
art.insert(b"hello", 1)
art.insert(b"world", 2)
print(art.delete(b"hello")) # 1
print(art.contains(b"hello")) # False
print(art.contains(b"world")) # True
print(art.contains(b"wor")) # False
print(pt.contains_prefix(b"hel")) # False
print(pt.contains_prefix(b"wor")) # True
print(art.get(b"hello")) # None
print(art.get(b"world")) # 2
print(art.insert(b"world", 3)) # 2
print(art.get(b"world")) # 3
print(art.continuations(b"wo")) # [(b'world', 3)]
key = b"world cup"
prefix_matches = art.prefix_matches(key)
print(prefix_matches) # [(5, 3)]
print(
[(key[:length].decode(), value) for length, value in prefix_matches]
) # [('world', 3)]
Metadata
Release files for byte-prefix-tree 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| byte_prefix_tree-0.3.2-cp310-abi3-manylinux_2_34_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.34+ x86-64 | Details for byte_prefix_tree-0.3.2-cp310-abi3-manylinux_2_34_x86_64.whl |
Release files / byte_prefix_tree-0.3.2-cp310-abi3-manylinux_2_34_x86_64.whl
| Download URL | byte_prefix_tree-0.3.2-cp310-abi3-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 303.0 kB |
| Tags | CPython 3.10 Linux glibc 2.34+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
a6a980b1ee2368fdb048d6c21073111b36a2427d4c9efc8561f0b4b76f2c7566
|
|
BLAKE2b-256 checksum How to use checksums |
a82ce1c64893f0085456871deea4d9ed55e765534245d01e9834bd371069f7ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.13
|
Security for byte-prefix-tree 0.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.