Skip to main content

patricia-trie

A pure Python 2.7+ implementation of a PATRICIA trie for effcient matching of string collections on text.

Note that you probably first want to have a look at marisa-trie or its PyPi package before using this.

patricia-trie has a clean API that imitates the dict() API and works with Py3k.

Installation

pip install patricia-trie

Usage

>>> from patricia import trie
>>> T = trie(1, key='value', king='kong') # a root value and two pairs
>>> '' in T # check if the value exits (note: the [empty] root is '')
True
>>> 'kong' in T
False
>>> T['king'] # get the value for an exact key
'kong'
>>> T['kong'] # error from non-existing keys
Traceback (most recent call last):
    ...
KeyError: 'kong'
>>> len(T) # count keys ("terminals") in the tree
3
>>> sorted(T.keys()) # "traditional stuff": keys(), values(), and items()
['', 'key', 'king']
>>> # scanning a text S with key(S), value(S), and item(S):
>>> S = 'keys and kewl stuff'
>>> T.key(S) # report the (longest) key that is a prefix of S
'key'
>>> T.value(S[1:]) # remember: the empty root always matches!
1
>>> del T[''] # interlude: deleting keys
>>> T.item(S[9:]) # raise error if no key is a prefix of S
Traceback (most recent call last):
    ...
KeyError: 'k'
>>> # info: the error string above contains the matched path so far
>>> T.item(S[1:], None) # avoid the error by specifying a default
>>> # iterate all matching content with keys(S), values(S), and items(S):
>>> list(T.items(S))
[('key', 'value')]

Note that deletion is a “half-supported” operation only. The key seems “removed”, but the trie is not actually changed, only the node state is changed from terminal to non-terminal. I.e., if you frequently delete keys, the compaction will become fragmented and less efficient. To mitigate this effect, make a copy of the trie (using a copy constructor idiom):

T = trie(**T)

License

Apache License v2

Metadata

Release files for patricia-trie 2

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

Source distribution (sdist)

Source distribution for patricia-trie 2
File Size Uploaded Provenance Actions
patricia-trie-2.tar.gz 4.0 kB Without provenance Details for patricia-trie-2.tar.gz

Release files / patricia-trie-2.tar.gz

Download URL patricia-trie-2.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
467a537a4859dc717e3db592cdb525c97712122ea999c980ba219fdf7bc18f4a
BLAKE2b-256 checksum
How to use checksums
745d2216364d071f4a7e3a7aa843e4e404450af85763621b4aefffe2cc17699e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for patricia-trie 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.

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