Registry / database / pytrie

pytrie

JSON →
library0.4.0pypypi✓ verified 86d ago

A pure Python implementation of the trie (prefix tree) data structure, version 0.4.0. No longer actively maintained; last release 2013.

pip install pytrie
INSTALL
IMPORT
SIG · PYTRIE
P
pytrie
databasepythonv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Trie
from pytrie import Trie
StringTrie
from pytrie import StringTrie

Create a trie, insert key-value pairs, and query with prefix.

from pytrie import Trie t = Trie() t['apple'] = 'fruit' t['app'] = 'prefix' print(t['apple']) # 'fruit' print(t.keys(prefix='app')) # ['app', 'apple'] print(t.items(prefix='app')) # [('app', 'prefix'), ('apple', 'fruit')]
Debug
Known issues
gotchaKeys must be strings (or hashable items for the base Trie; StringTrie enforces strings). With base Trie, keys that don't share prefixes may not be stored efficiently.
fix
Use StringTrie for string keys to ensure correct prefix semantics.
affects: all
deprecatedLibrary is in maintenance mode; no updates since 2013. Not compatible with Python 3.12+ (collections.Iterable moved to collections.abc).
fix
Pin Python version <3.12 or switch to actively maintained alternatives like 'datrie' or 'pygtrie'.
affects: 0.4.0
gotchaIteration order is not guaranteed; to get sorted keys, install sortedcontainers or use sorted(t).
fix
Install sortedcontainers: pip install sortedcontainers; or call sorted(t).
affects: all
Errors
Common errors & fixes
ImportError: cannot import name 'Iterable' from 'collections'
Python 3.12 removed collections.Iterable; pytrie uses it internally.
fix
Use Python <3.12, or monkey-patch: import collections; collections.Iterable = collections.abc.Iterable before importing pytrie.
AttributeError: 'Trie' object has no attribute 'longest_prefix'
Method not available; pytrie lacks longest_prefix().
fix
Use keys(prefix) and filter longest, or switch to pygtrie which has longest_prefix().
TypeError: 'NoneType' object is not iterable
When passing non-hashable key items to the base Trie constructor.
fix
Use StringTrie for string keys; ensure all key parts are hashable.
Upgrade
Version history
0.4.0latest on PyPI · released Mar 9, 2024
Audit
Dependencies
sortedcontainersoptionalUsed internally for sorted iteration (optional fallback)
Agent activity
12 hits · last 30 days
node
10
Resources
pytrie — pip install pytrie · libregistry