基数树实现
项目描述
这是一个SEKOIA.io分支的py-radix库的PyPI包。版本1.0.0post1是git版本682a1b96fb5b9599bc4baf11c0ab08b2be4fe783。
原始代码由Raphael Cohen分支,以减少C实现的内存占用,并进行了其他一些修复。这个特定的PyPI包由Sasha Romijn发布。
以下属性已被转换为属性,将在查询时计算
网络
前缀
data对象现在默认为None,而不是空字典。它可以用来存储任何类型的数据。
py-radix实现了用于存储和检索IPv4和IPv6网络前缀的基数树数据结构。
基数树通常用于路由表查找。它有效地存储了不同长度的网络前缀,并允许快速查找包含的网络。
安装
通过pip轻松安装
pip install py-radix
或使用标准的Python distutils咒语
python setup.py build python setup.py install
C扩展将针对支持的Python版本构建。如果您不想构建C扩展,请设置环境变量RADIX_NO_EXT=1。
测试位于tests/目录中,可以使用python setup.py nosetests运行。
用法
这是一个展示大部分功能的简单示例
import socket 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 = socket.inet_aton("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_aton("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)
许可
py-radix 采用 ISC/BSD 许可。底层的基数树实现是从 MRTd(经过修改)获得的,并受 4 项 BSD 许可的约束。有关详细信息,请参阅 LICENSE 文件。
贡献
请通过 GitHub 报告错误,网址为 https://github.com/mjschultz/py-radix/issues。代码更改可以通过 GitHub 上的拉取请求或直接通过电子邮件发送给我 <mjschultz@gmail.com> 进行贡献。
目录树的主要部分如下
. ├── 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.
项目详情
关闭
py-radix-sr-1.0.0.post1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e0c0f922380856bbdf785c701f67661f1b5c5cb6779308532ce3c27a6204cd7d |
|
MD5 | db415f5b48d07d4c5361c0f174f6bde5 |
|
BLAKE2b-256 | af1785f23218057fa5f4080798617c8c15aaf243ef65304615b8f2d3dec26b44 |