Exemplo n.º 1
0
class UniqDomainContainer:
    '''
    A Container allow insert generic domain into it. it will auto resovle 
    conflict cases
    e.g.
    >>> uniq = UniqDomainContainer()
    >>> uniq.insert("*.baidu.com")
    >>> uniq.insert("*.img.baidu.com")
    >>> uniq.insert("*.video.baidu.com")
    >>> uniq.items()
    ['*.baidu.com']
    '''
    def __init__(self):
        self.tree = RRadixTree(split=lambda x: x.split("."), join=".".join)

    def insert(self, generic_domain):
        base = generic_domain[2:]

        for tail in tails(base.split(".")):
            superdomain = "*." + ".".join(tail)
            if superdomain in self.tree:
                return

        for domain in self.tree.like(base):
            del self.tree[domain]

        self.tree.insert(generic_domain)

    def batch_insert(self, domains):
        for domain in domains:
            self.insert(domain)

    def items(self):
        return self.tree.items()
Exemplo n.º 2
0
 def __init__(self):
     self.tree = RRadixTree(split=lambda x: x.split("."), join=".".join)