Skip to main content

A python package that uses a compressed prefix tree (radix tree) to store a dictionary of known words and suggests completions for partial entries.

Project description

Patrix

pipeline docs python license

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:

# Example usage
>>> autocomplete.completions()
{'p'}
>>> autocomplete.completions("p")
{'package', 'pro', 'python'}
>>> autocomplete.completions("pro")
{'program', 'project'}
>>> autocomplete.completions("program")
{'program', 'programming'}
>>> autocomplete.completions("programming")
set()

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patrix-0.3.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patrix-0.3.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file patrix-0.3.0.tar.gz.

File metadata

  • Download URL: patrix-0.3.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for patrix-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c5d8849bad67797623d9832290921941f8269a860cda6d52ca09ba3098755632
MD5 1eda5d632425047c9d6b143a26290265
BLAKE2b-256 13a2f185902e23e2cbe6f687929cca488b9a7e581496ee79590449551b799b94

See more details on using hashes here.

File details

Details for the file patrix-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: patrix-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for patrix-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30bc69138d7d3c1597e83694b97c98d0d21d719708cb5b2523389ea59b2cbbbb
MD5 f0f5ab9254574604d65f8a1ab0e179ca
BLAKE2b-256 7aead6ce2e9d62a1d2fc98173b5cf17d46b56eb41ffdf1880a2e82239b2e3182

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page