当前位置: 首页>>代码示例>>Python>>正文


Python OIBTree.maxKey方法代码示例

本文整理汇总了Python中BTrees.OIBTree.OIBTree.maxKey方法的典型用法代码示例。如果您正苦于以下问题:Python OIBTree.maxKey方法的具体用法?Python OIBTree.maxKey怎么用?Python OIBTree.maxKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BTrees.OIBTree.OIBTree的用法示例。


在下文中一共展示了OIBTree.maxKey方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: IPTree

# 需要导入模块: from BTrees.OIBTree import OIBTree [as 别名]
# 或者: from BTrees.OIBTree.OIBTree import maxKey [as 别名]
class IPTree(object):
    """Data structure for storing sets of IP addresses"""
    def __init__(self, l=None):
        self._tree = OIBTree()
        self._tree[[-1,255,255,255]] = 0
        self._tree[[256,0,0,1]] = 0

    def add(self, s):
        ip4 = IP4(s)
        t = self._tree
        #print "adding %r" % ip4
        inbetween = list(t.items(min=ip4.begin, max=ip4.end,
                                 excludemin=True,
                                 excludemax=True))

        ma = t.minKey(ip4.end)   # returns smallest key >= ip4.end
        mi = t.maxKey(ip4.begin) # returns largest key <= ip4.begin

        if ma == ip4.end:
            if t[ma] == 1:
                del t[ma]
        else:
            if not ip4.end in self:
                t[ip4.end] = 0

        if mi == ip4.begin:
            if t[mi] == 0:
                del t[mi]
        else:
            if not ip4.begin in self:
                t[ip4.begin] = 1

        for node, val in inbetween:
            del self._tree[node]

    def remove(self, s):
        ip4 = IP4(s)
        t = self._tree
        #print "removing %r" % ip4
        inbetween = list(t.items(min=ip4.begin, max=ip4.end,
                                 excludemin=True,
                                 excludemax=True))

        ma = t.minKey(ip4.end)   # returns smallest key >= ip4.end
        mi = t.maxKey(ip4.begin) # returns largest key <= ip4.begin

        if ma == ip4.end:
            if t[ma] == 0:
                del t[ma]
        else:
            if ip4.end in self:
                t[ip4.end] = 1

        if mi == ip4.begin:
            if t[mi] == 1:
                del t[mi]
        else:
            if ip4.begin in self:
                t[ip4.begin] = 0

        for node, val in inbetween:
            del self._tree[node]

    def as_iter(self):
        i = iter(self._tree)
        i.next() # discard first item
        while True:
            p0 = i.next()
            p1 = i.next()
            yield (p0, p1)

    def __contains__(self, a):
        if isinstance(a, list):
            k = self._tree.maxKey(a)
            return self._tree[k]
        raise RuntimeError("Should not happen!")

    def __repr__(self):
        return repr(list(self.as_iter()))
开发者ID:jap,项目名称:iptree,代码行数:81,代码来源:iptree.py


注:本文中的BTrees.OIBTree.OIBTree.maxKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。