Registry / http-networking / pytricia

pytricia

JSON →
library1.3.0pypypi✓ verified 87d ago

PyTricia is an efficient IP address storage and lookup module for Python, based on the Patricia trie data structure. It supports IPv4 and IPv6, longest-prefix matching, and set operations. Current version 1.3.0 is stable with no known breaking changes. Release cadence is low (last release 2025).

pip install pytricia
INSTALL
IMPORT
SIG · PYTRICIA
P
pytricia
http-networkingpythonv1.3.0
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
build_error
glibc
py 3.103.910 runs
build_error
Code
Verified usage

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

PyTricia
import pytricia
The module is imported directly; PyTricia is the class name.

Basic usage: insert, lookup, longest-prefix match, containment check, iteration, and deletion.

import pytricia # Initialize a new trie pt = pytricia.PyTricia() # Insert a prefix pt["10.0.0.0/8"] = "internal" pt["10.1.0.0/16"] = "subnet" # Lookup an IP result = pt["10.1.0.5"] print(result) # prints 'subnet' # Longest prefix match import ipaddress net = pt.get_key("10.1.0.5") print(net) # prints '10.1.0.0/16' # Check containment print("10.0.0.0/8" in pt) # True # Iterate over prefixes for prefix in pt: print(prefix, pt[prefix]) # Delete a prefix del pt["10.1.0.0/16"]
Debug
Known issues
gotchaPyTricia expects prefix strings in CIDR notation (e.g., '10.0.0.0/8'). Do not pass IP addresses without prefix length; for host lookups, use the IP with /32 or use get_key() which returns the matching prefix.
fix
Always provide a prefix length when inserting. For single IPs, use e.g., '192.168.1.1/32'.
affects: all
gotchaPyTricia does not support non-ASCII or non-CIDR inputs; all prefixes must be valid IPv4 or IPv6 CIDR strings. Passing a malformed string raises ValueError.
fix
Validate input with ipaddress.ip_network(prefix, strict=False) before inserting.
affects: all
gotchaValues stored in the trie are not validated; they can be any Python object. However, retrieving a key that does not exist raises KeyError. Use pt.get(key, default) or check containment with 'in' first.
fix
Use 'prefix in pt' before accessing pt[prefix] to avoid KeyError.
affects: all
Errors
Common errors & fixes
KeyError: <ip_address>
Trying to look up an IP that is not contained in any inserted prefix, or using an IP without /32.
fix
Ensure the IP is covered by an inserted prefix. For exact host lookup, insert with /32. Use pt.get(ip, None) to avoid exception.
ValueError: invalid prefix
Passing a string that is not a valid CIDR prefix (e.g., missing slash, invalid address).
fix
Check input format: e.g., '10.0.0.0/8' is valid. Use ipaddress module to parse and validate.
TypeError: 'PyTricia' object does not support item assignment
Attempting to assign a value without having imported pytricia correctly or using a read-only view.
fix
Ensure you have created an instance: pt = pytricia.PyTricia() then use pt[prefix] = value.
Upgrade
Version history
1.3.0latest on PyPI · released Sep 15, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
pytricia — pip install pytricia · libregistry