基树实现

py-radix的Python项目详细描述


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

py基数实现了用于存储和 检索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 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

许可证

PY基数是根据ISC/BSD许可证授予的。基本基树 从MRTD中获取(并修改)实施方案,实施期限为4年 BSD许可证。有关详细信息,请参见许可文件。

贡献

请通过github在https://github.com/mjschultz/py-radix/issues报告错误。 代码更改可以通过github上的pull请求提供,也可以通过电子邮件发送 直接发给我<;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.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用McClickListener单击了什么元素   Java时间戳在Oracle时间戳中不同情况下存储12 PM的奇怪行为   java无法使用事件总线对运行在不同机器上的垂直体进行通信   java Mockserver:收到请求后进行回调   java无法将Json字符串转换为Map<string,Object>   java如何按升序排列输出?   java视图行,带有oracle键。jbo。在SrCategoryParentIterator中找不到键[300100120394155]   javafxmysql连接示例   java正在等待加载完成   java是否可以将同一个有状态会话bean实例注入多个其他会话bean?   java无法让万向节检测离开或进入区域   使用JavaCV和OpenCV的java提供了dyld:lazy符号绑定失败:找不到符号:__sincos_stret   xml解析无法使用Java读取xml文档   java无法更改工具栏的颜色   javaapachesshd和JSCH   java无法在firebase存储中检索图像url   java问题与executeUpdate   同一应用程序中不同活动之间的java SharedReference