Skip to content

Commit 78f1c1e

Browse files
authored
Merge pull request #13 from faucetsdn/pytricia-1.3.0
Import pytricia 1.3.0 source
2 parents bbdf249 + 903944d commit 78f1c1e

File tree

8 files changed

+858
-519
lines changed

8 files changed

+858
-519
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ update.sh
55
MANIFEST
66
build
77
dist
8+
.vscode/
9+
venv/
10+
*.swp

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ to recommend it over related modules (including py-radix and SubnetTree):
88
2. it works in Python 3, and
99
3. there are a few nicer library features for manipulating the structure.
1010

11-
Copyright (c) 2012-2017 Joel Sommers. All rights reserved.
11+
Copyright (c) 2012-2025 Joel Sommers. All rights reserved.
1212

1313
Pytricia is released under terms of the GNU Lesser General Public License,
1414
version 3.0 and greater.
1515

16+
## Support further development of Pytricia
17+
18+
I originally wrote this code with funding from the US National Science Foundation. Development since 2016 has been on an "as I have time and motivation" basis. If you or your organization gets benefit from this software and you'd like to see further development, [please consider donating](https://www.buymeacoffee.com/joelsommers).
19+
20+
1621
# Building
1722

1823
Building pytricia is done in the standard pythonic way:
@@ -200,6 +205,18 @@ Although it is possible to store IPv4 and IPv6 subnets in the same trie, this is
200205

201206
IPv4 address `32.0.0.1` matches `2000::/8` prefix due to the first octet being the same in both. In order to avoid this, separate tries should be used for IPv4 and IPv6 prefixes. Alternatively, [IPv4 addresses can be mapped to IPv6 addresses](https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses).
202207

208+
``PyTricia`` objects can be be pickled, but you must first ``freeze()`` to reconfigure them to a more efficient representation suitable for serialization. Note that while in this more compact representation you can not modify the object. To restore the ability to modify you can use ``thaw()``.
209+
210+
>>> import pytricia
211+
>>> import pickle
212+
>>> pyt = pytricia.PyTricia()
213+
>>> pyt.freeze()
214+
>>> s = pickle.dumps(pyt)
215+
>>> pyt = pickle.loads(s)
216+
>>> pyt.thaw()
217+
218+
219+
203220
# Performance
204221

205222
For API usage, the usual Python advice applies: using indexing is the fastest method for insertion, lookup, and removal. See the ``apiperf.py`` script in the repo for some comparative numbers. For Python 3, using ``ipaddress``-module objects is the slowest. There's a price to pay for the convenience, unfortunately.

apiperf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def main():
6565
for fn in expts:
6666
t = Timer("{}()".format(fn), "from __main__ import {}".format(fn))
6767
v = t.timeit(number=iterations)
68-
v = v / float(iterations)
69-
print ("{:.08f}s: average execution time for {}".format(v, fn))
68+
v_iter = v / float(iterations)
69+
print ("{:.08f}s: (average execution time for {} total_sec={:.1f})".format(v_iter, fn, v))
7070

7171
if __name__ == '__main__':
7272
main()

0 commit comments

Comments
 (0)