Skip to main content

Radix tree implementation

Project description

https://travis-ci.org/mjschultz/py-radix.svg?branch=master https://coveralls.io/repos/mjschultz/py-radix/badge.png?branch=master

py-radix implements the radix tree data structure for the storage and retrieval of IPv4 and IPv6 network prefixes.

The radix tree is commonly used for routing table lookups. It efficiently stores network prefixes of varying lengths and allows fast lookups of containing networks.

Installation

Installation is a breeze via pip:

pip install py-radix

Or with the standard Python distutils incantation:

python setup.py build
python setup.py install

The C extension will be built for supported python versions. If you do not want the C extension, set the environment variable RADIX_NO_EXT=1.

Tests are in the tests/ directory and can be run with python setup.py nosetests.

Usage

A simple example that demonstrates most of the features:

import radix

# Create a new tree
rtree = radix.Radix()

# Adding a node returns a RadixNode object. You can create
# arbitrary members in its 'data' dict to store your data
rnode = rtree.add("10.0.0.0/8")
rnode.data["blah"] = "whatever you want"

# You can specify nodes as CIDR addresses, or networks with
# separate mask lengths. The following three invocations are
# identical:
rnode = rtree.add("10.0.0.0/16")
rnode = rtree.add("10.0.0.0", 16)
rnode = rtree.add(network = "10.0.0.0", masklen = 16)

# It is also possible to specify nodes using binary packed
# addresses, such as those returned by the socket module
# functions. In this case, the radix module will assume that
# a four-byte address is an IPv4 address and a sixteen-byte
# address is an IPv6 address. For example:
binary_addr = inet_ntoa("172.18.22.0")
rnode = rtree.add(packed = binary_addr, masklen = 23)

# Exact search will only return prefixes you have entered
# You can use all of the above ways to specify the address
rnode = rtree.search_exact("10.0.0.0/8")
# Get your data back out
print rnode.data["blah"]
# Use a packed address
addr = socket.inet_ntoa("10.0.0.0")
rnode = rtree.search_exact(packed = addr, masklen = 8)

# Best-match search will return the longest matching prefix
# that contains the search term (routing-style lookup)
rnode = rtree.search_best("10.123.45.6")

# Worst-search will return the shortest matching prefix
# that contains the search term (inverse routing-style lookup)
rnode = rtree.search_worst("10.123.45.6")

# Covered search will return all prefixes inside the given
# search term, as a list (including the search term itself,
# if present in the tree)
rnodes = rtree.search_covered("10.123.0.0/16")

# There are a couple of implicit members of a RadixNode:
print rnode.network     # -> "10.0.0.0"
print rnode.prefix      # -> "10.0.0.0/8"
print rnode.prefixlen   # -> 8
print rnode.family      # -> socket.AF_INET
print rnode.packed      # -> '\n\x00\x00\x00'

# IPv6 prefixes are fully supported in the same tree
rnode = rtree.add("2001:DB8::/3")
rnode = rtree.add("::/0")

# Use the nodes() method to return all RadixNodes created
nodes = rtree.nodes()
for rnode in nodes:
        print rnode.prefix

# The prefixes() method will return all the prefixes (as a
# list of strings) that have been entered
prefixes = rtree.prefixes()

# You can also directly iterate over the tree itself
# this would save some memory if the tree is big
# NB. Don't modify the tree (add or delete nodes) while
# iterating otherwise you will abort the iteration and
# receive a RuntimeWarning. Changing a node's data dict
# is permitted.
for rnode in rtree:
        print rnode.prefix

License

py-radix is licensed under a ISC/BSD licence. The underlying radix tree implementation is taken (and modified) from MRTd and is subject to a 4-term BSD license. See the LICENSE file for details.

Contributing

Please report bugs via GitHub at https://github.com/mjschultz/py-radix/issues. Code changes can be contributed through a pull request on GitHub or emailed directly to me <mjschultz@gmail.com>.

The main portions of the directory tree are as follows:

.
├── radix/*.py      # Pure Python code
├── radix/_radix.c  # C extension code (compatible with pure python code)
├── radix/_radix/*  # C extension code (compatible with pure python code)
├── tests/          # Tests (regression and unit)
└── setup.py        # Standard setup.py for installation/testing/etc.

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

py-radix-0.10.0.tar.gz (21.8 kB view details)

Uploaded Source

Built Distributions

py_radix-0.10.0-cp39-cp39-manylinux2014_x86_64.whl (65.3 kB view details)

Uploaded CPython 3.9

py_radix-0.10.0-cp38-cp38-manylinux2014_x86_64.whl (66.0 kB view details)

Uploaded CPython 3.8

py_radix-0.10.0-cp37-cp37m-manylinux2014_x86_64.whl (65.7 kB view details)

Uploaded CPython 3.7m

py_radix-0.10.0-cp36-cp36m-manylinux2014_x86_64.whl (64.5 kB view details)

Uploaded CPython 3.6m

File details

Details for the file py-radix-0.10.0.tar.gz.

File metadata

  • Download URL: py-radix-0.10.0.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py-radix-0.10.0.tar.gz
Algorithm Hash digest
SHA256 b8dbd1344bb30c6a1097d4103203c7b117d92931620365985018de4bef5aede3
MD5 3f9c6e07ee0e779b5a3de551f38be5ba
BLAKE2b-256 bf4e47d9e7f4dfd0630662e19d2cc1b2f1d307ec52df11f4a66f6ed6f0cce138

See more details on using hashes here.

File details

Details for the file py_radix-0.10.0-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: py_radix-0.10.0-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 65.3 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for py_radix-0.10.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0c4956f6489bed71798a68f2e9217037a8631a2b057b42484a5833b013f2415c
MD5 65dd6ad36b814a9c4619cdf910d8afc2
BLAKE2b-256 a53230b72197869c2a1f04734835156643693defbf393debc1f379fb824c054b

See more details on using hashes here.

File details

Details for the file py_radix-0.10.0-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: py_radix-0.10.0-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 66.0 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for py_radix-0.10.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48f1d341328cf69479343ef75dc06939dd50e507b0c1d5e985ffe8fec797d1c6
MD5 ba9ce94c7da759595fe851ee3d63e090
BLAKE2b-256 d8a5148451bcbdb4e5d3c876b44213c04868888f1b363b3fd9e912ce065399c7

See more details on using hashes here.

File details

Details for the file py_radix-0.10.0-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: py_radix-0.10.0-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 65.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for py_radix-0.10.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c36ef71ec45eb586afc9431dbb51aebfe73caf5940b381bdcd96ef76d13aa787
MD5 9f4475c1cecd89822a77e668e45386aa
BLAKE2b-256 f9075259708410b58829f62024e116ad5137148ef5480f4ec611d09b151db488

See more details on using hashes here.

File details

Details for the file py_radix-0.10.0-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: py_radix-0.10.0-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 64.5 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for py_radix-0.10.0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b6c6e6100061e677bc5f6b90d9df10291c764af903268e6f8b599b530865aeae
MD5 aa2aa43dc8bbd557d317468544affac0
BLAKE2b-256 d3e3ee1cf0cf2870a2d0247cdacb2b8187496444c69614eaa36d72e4c455a1cb

See more details on using hashes here.

Supported by

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