Patrix
Patrix provides a RadixTree class
(aka trie, compressed prefix tree, or compact prefix tree)
that behaves like a python dictionary
(abc.MutableMapping subclass)
but provides quick and efficient completion suggestions for partial word entries.
This is useful for building autocomplete systems of long lists of known strings that share common prefixes. This is typical for hierarchical naming systems like file paths, IP addresses, or domain names, but it is not limited to those examples.
Usage
Autocomplete System
from patrix import RadixTree
words = [
"python",
"programming",
"program",
"project",
"package",
]
autocomplete = RadixTree(words)
Query for possible completions of a partial word.
Each result is a Completion: the completed prefix, whether that prefix is a stored word (is_word), and whether the tree continues past it (does_continue).
completions returns None when the query matches no entry.
# Example usage
>>> autocomplete.completions()
{Completion(prefix='p', is_word=False, does_continue=True)}
>>> autocomplete.completions("p")
{Completion(prefix='package', is_word=True, does_continue=False), Completion(prefix='pro', is_word=False, does_continue=True), Completion(prefix='python', is_word=True, does_continue=False)}
>>> autocomplete.completions("pro")
{Completion(prefix='program', is_word=True, does_continue=True), Completion(prefix='project', is_word=True, does_continue=False)}
>>> autocomplete.completions("program")
{Completion(prefix='program', is_word=True, does_continue=False), Completion(prefix='programming', is_word=True, does_continue=False)}
>>> autocomplete.completions("programming")
{Completion(prefix='programming', is_word=True, does_continue=False)}
>>> autocomplete.completions("a") is None
True
Save and load a tree
>>> from patrix import RadixTree
>>> # Entries can be a list of strings or key-value tuples
>>> r = RadixTree(("computer", "compute", ("computing", 1)))
>>> # Save to disk as JSON
>>> r.asdict()
{'comput': {'e': {'': {}, 'r': {}}, 'ing': {"__value__": 1}}}
>>> # Load using from_dict
>>> s = RadixTree.from_dict({'comput': {'e': {'': {}, 'r': {}}, 'ing': {"__value__": 1}}})
>>> s.asdict()
{'comput': {'e': {'': {}, 'r': {}}, 'ing': {"__value__": 1}}}
Compression rate for this tree:
>>> r.total_chars
11
>>> len("computer" + "computing" + "compute")
24
>>> 1 - 11 / 24 # 54% compression rate
0.5416666666666667
>>> r.size # nodes in the tree
6
Use it like a regular dictionary
RadixTree behaves like a regular python dictionary,
but the insertion order is not preserved.
>>> r = RadixTree()
>>> r["computer"] = 1
>>> r["compute"] = 2
>>> "computer" in r
True
>>> r["computer"]
1
>>> r.pop("compute")
2
>>> "compute" in r
False
>>> s = r | {"computing": 3}
>>> "computing" in s
True
>>> "computing" in r
False
>>> del s["computing"]
>>> r == s
True
Metadata
Release files for patrix 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| patrix-0.4.0.tar.gz | 10.7 kB | Without provenance | Details for patrix-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| patrix-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for patrix-0.4.0-py3-none-any.whl |
Total release size: 21.0 kB
Release files / patrix-0.4.0.tar.gz
| Download URL | patrix-0.4.0.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d9984d637d77a5eea5c8a14ad5290d94d6a9fdf7df2eefa30707f20ac369ebe1
|
|
BLAKE2b-256 checksum How to use checksums |
95485e6ea0122bed2d178d214cce4aa56c6758bea66c53fb5442161875c6750a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / patrix-0.4.0-py3-none-any.whl
| Download URL | patrix-0.4.0-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
419ed9379ddfd895bb60eebc6dedc82de0a4c7f68c891b6adb8db8158b520aab
|
|
BLAKE2b-256 checksum How to use checksums |
8fbbc5bbba300c9f0ca2339149bc54ce5490904373ccd81f8c7eb2b94b5e99d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for patrix 0.4.0
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.