Пример #1
0
    def test_del(self):
        db = PrefixTrie()
        db["00"] = 1
        db["001"] = 2
        db["002"] = 3
        db["0011"] = 4
        self.assertRaises(KeyError, db.__delitem__, "b")

        self.assertEqual(db["001"], 2)
        self.assertSequenceEqual(db.longest_prefix("0015"), ("001", 2))

        del db["001"]
        self.assertRaises(KeyError, db.__getitem__, "001")
        self.assertEqual(db["0011"], 4)
        self.assertSequenceEqual(db.longest_prefix("0015"), ("00", 1))
Пример #2
0
    def test_prefix(self):
        db = PrefixTrie()
        db["an"] = 0
        db["ant"] = 1
        db["all"] = 2
        db["allot"] = 3
        db["alloy"] = 4
        db["aloe"] = 5
        db["are"] = 6
        db["be"] = 7

        self.assertSequenceEqual(db.longest_prefix("antonym"), ("ant", 1))
        self.assertSequenceEqual(db.longest_prefix("any"), ("an", 0))
        self.assertSequenceEqual(db.longest_prefix("are"), ("are", 6))
        self.assertRaises(KeyError, db.longest_prefix, "alsa")
        self.assertRaises(KeyError, db.longest_prefix, "b")
Пример #3
0
class DB(object):
    def __init__(self, default=IPv4):
        self._db = PrefixTrie()
        self._factory = default

    def __getstate__(self):
        return self._db

    def insert(self, ip, message=None):
        v = self._factory(ip)
        self._db[bin(v.network)[2:v.netmask + 1]] = (v, message)

    def __getitem__(self, ip):
        v = self._factory(ip)
        return self._db.longest_prefix(bin(v.network)[2:v.netmask + 1])[1]

    def __delitem__(self, ip):
        v = self._factory(ip)
        del self._db[bin(v.network)[2:v.netmask + 1]]

    def get(self, ip):
        return self.__getitem__(ip)
Пример #4
0
class DB(object):

    def __init__(self, default=IPv4):
        self._db = PrefixTrie()
        self._factory = default

    def __getstate__(self):
        return self._db

    def insert(self, ip, message=None):
        v = self._factory(ip)
        self._db[bin(v.network)[2:v.netmask + 1]] = (v, message)

    def __getitem__(self, ip):
        v = self._factory(ip)
        return self._db.longest_prefix(bin(v.network)[2:v.netmask + 1])[1]

    def __delitem__(self, ip):
        v = self._factory(ip)
        del self._db[bin(v.network)[2:v.netmask + 1]]

    def get(self, ip):
        return self.__getitem__(ip)