pygtrie is a pure Python library implementing a trie data structure. It provides `Trie`, `CharTrie`, and `StringTrie` classes, each implementing a mutable mapping (dictionary-like) interface. Its strengths lie in prefix-based operations, such as iterating over or deleting subtries, prefix checking, and shortest/longest prefix look-ups. It also includes a `PrefixSet` for managing sets of prefixes. The current version is 2.5.0, with releases occurring periodically to introduce features and address compatibility.
pip install pygtrieVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of `Trie` for key-value storage and `StringTrie` for path-like keys, including prefix-based retrieval.
Update imports from `from pytrie import trie` to `import pygtrie as trie` or directly import classes like `from pygtrie import Trie`.
If sorted iteration is required, use `trie.enable_sorting()` after initialization or when needed.
Access the `key` and `value` properties directly from the returned `_Step` or `_NoneStep` object (e.g., `result.key`, `result.value`).
For string keys, prefer using `pygtrie.CharTrie` or `pygtrie.StringTrie`, which are designed to handle string keys and return them as strings.
Understand the semantics of `PrefixSet.add()`: it ensures that only the shortest unique prefixes are stored. Review existing entries before adding to avoid unintended deletions or no-ops.
To check if a key has an associated value, use `if key in trie:`. To retrieve a value safely, use `trie.get(key, default_value)` to provide a fallback, or ensure a value is set for the specific key before accessing it directly.
Ensure that the `separator` argument passed to `pygtrie.StringTrie` is a non-empty string, such as `pygtrie.StringTrie(separator='/')`.
If you intend to work with string keys and receive strings back, use `pygtrie.CharTrie` (for single-character components) or `pygtrie.StringTrie` (for keys split by a custom separator). For example, `t = pygtrie.CharTrie()` or `t = pygtrie.StringTrie()`.
For exceptionally deep tries, consider increasing Python's recursion limit temporarily using `sys.setrecursionlimit(new_limit)` (use with caution), or explore alternative (potentially iterative) methods for processing deep structures if available for your specific use case.
No dependency data recorded yet.