Skip to main content

Generalized trie implementation

Project description

gen-tries

Name

gen-tries

Description

A generalized trie implementation for python 3.10 or later that provides classes and functions to create and manipulate a generalized trie data structure.

Unlike many Trie implementations which only support strings as keys and token match only at the character level, it is agnostic as to the types of tokens used to key it and thus far more general purpose.

It requires only that the indexed tokens be hashable (this means that the have eq and hash methods). This is verified at runtime using the gentrie.Hashable protocol.

Tokens in a key do NOT have to all be the same type as long as they can be compared for equality.

Note that objects of user-defined classes are Hashable by default, but this may not work as naively expected unless they are immutable.

It can handle Sequences of Hashable conforming objects as keys for the trie out of the box.

As long as the tokens returned by a sequence are hashable, it largely 'just works'.

You can 'mix and match' types of objects used as token in a key as long as they all conform to the Hashable protocol.

Usage

Example 1:

from gentrie import GeneralizedTrie, TrieEntry

trie = GeneralizedTrie()
trie.add(['ape', 'green', 'apple'])
trie.add(['ape', 'green'])
matches: set[TrieEntry] = trie.prefixes(['ape', 'green'])
print(matches)

Example 1 Output:

{TrieEntry(ident=2, key=['ape', 'green'])}}

Example 2:

from gentrie import GeneralizedTrie, TrieEntry

# Create a trie to store website URLs
url_trie = GeneralizedTrie()

# Add some URLs with different components (protocol, domain, path)
url_trie.add(["https", "com", "example", "www", "/", "products", "clothing"])
url_trie.add(["http", "org", "example", "blog", "/", "2023", "10", "best-laptops"])
url_trie.add(["ftp", "net", "example", "ftp", "/", "data", "images"])

# Find all https URLs with "example.com" domain
suffixes: set[TrieEntry] = url_trie.suffixes(["https", "com", "example"])
print(suffixes)

Example 2 Output:

{TrieEntry(ident=1, key=['https', 'com', 'example', 'www', '/', 'products', 'clothing'])}

Example 3:

from gentrie import GeneralizedTrie, TrieEntry

trie = GeneralizedTrie()
trie.add('abcdef')
trie.add('abc')
trie.add('qrf')
matches: set[TrieEntry] = trie.suffixes('ab')
print(matches)

Example 3 Output:

{TrieEntry(ident=2, key='abc'), TrieEntry(ident=1, key='abcdef')}

Authors and acknowledgment

  • Jerilyn Franz

Copyright

Copyright 2025 Jerilyn Franz

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

gen_tries-0.1.4.tar.gz (2.8 MB view details)

Uploaded Source

Built Distribution

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

gen_tries-0.1.4-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file gen_tries-0.1.4.tar.gz.

File metadata

  • Download URL: gen_tries-0.1.4.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for gen_tries-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6ca6d79cb32cf59ef8649ae1c09a06c230c28c5efb629e8b719de30d543790f4
MD5 a4b19eafd1f6face11f491977adcda44
BLAKE2b-256 99e8db6a5bdd8f0e608413443f7fefa1dfc78d476e0156f1f0e2d37795fc2975

See more details on using hashes here.

File details

Details for the file gen_tries-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: gen_tries-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for gen_tries-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9d6b8f6129ecf12a8b207e00c82a9b2ddb16bbcc12e9131fca9d79f254dedbdc
MD5 6533a192977964b5fdccc1fbe0ed54e3
BLAKE2b-256 98f4ffa26c68c8b5baa2785b5c0d61cbe1e28e1dadf006a384e40877bb303c0c

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