class TrieItems(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = [ 'com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub' ] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_len(self): self.assertTrue(len(self.trie) == 4, "Trie::__len__") self.assertTrue(len(list(self.trie.items())) == 4, "Trie::items") def test_items(self): items = {} for item in self.trie.items(): items[item[0]] = item[1] for key in self.keys: self.assertTrue(key in items, "Trie::items")
def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED) self.keys = [ 'com.example', 'com.baz', 'com.example.sub', 'org.example' ] for key in self.keys: self.trie.add(key, 1)
class TrieItems(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_len(self): self.assertTrue(len(self.trie) == 4, "Trie::__len__") self.assertTrue(len(list(self.trie.items())) == 4, "Trie::items") def test_items(self): items = {} for item in self.trie.items(): items[item[0]] = item[1] for key in self.keys: self.assertTrue(key in items, "Trie::items")
def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = [ 'com.example', 'com.blah', 'com.example.sub1', 'com.example.sub2' ] for key in self.keys: self.trie.add(key, 1, atAllSubPaths=True)
def setUp(self): self.trie = Trie() self.keys = [ 'bar', 'baz', 'barbell', 'foo', 'food', 'bar', 'bazbuzz', 'bazbuzz' ] for key in self.keys: self.trie.add(key, 1)
class TrieOpAdd(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.example.sub', 'org.example'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_add_dict(self): addDict = { 'com.example': 1, 'com.example.sub2': 1, 'org.other': 1, 'org.example.sub': 1, 'net.example': 1 } nt = self.trie + addDict self.assertTrue(len(nt) == 7, "Trie::__add__(dict) length") for key in addDict.keys(): self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") for key in self.keys: self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") def test_add_mixedtrie(self): addTrie = Trie() self.assertRaises(TypeError, self.trie.__add__, addTrie) def test_add_trie(self): addDict = { 'com.example': 1, 'com.example.sub2': 1, 'org.other': 1, 'org.example.sub': 1, 'net.example': 1 } addTrie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) + addDict nt = self.trie + addTrie self.assertTrue(len(nt) == 7, "Trie::__add__(dict) length") for key in addDict.keys(): self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") for key in self.keys: self.assertTrue(nt.has(key), "Trie::__add__(dict) contents")
class TrieOpAdd(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = ['com.example', 'com.example.sub', 'org.example'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_add_dict(self): addDict = { 'com.example': 1, 'com.example.sub2': 1, 'org.other': 1, 'org.example.sub': 1, 'net.example': 1 } nt = self.trie + addDict self.assertTrue(len(nt) == 7, "Trie::__add__(dict) length") for key in addDict.keys(): self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") for key in self.keys: self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") def test_add_mixedtrie(self): addTrie = Trie() self.assertRaises(TypeError, self.trie.__add__, addTrie) def test_add_trie(self): addDict = { 'com.example': 1, 'com.example.sub2': 1, 'org.other': 1, 'org.example.sub': 1, 'net.example': 1 } addTrie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) + addDict nt = self.trie + addTrie self.assertTrue(len(nt) == 7, "Trie::__add__(dict) length") for key in addDict.keys(): self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") for key in self.keys: self.assertTrue(nt.has(key), "Trie::__add__(dict) contents")
class KeyAsValueTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example'] for key in self.keys: self.trie.add(key, key) def test_values(self): for k in self.keys: self.assertTrue(self.trie.get(k) == k, "Trie::get")
class KeyAsValueTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED) self.keys = [ 'com.example', 'com.baz', 'com.example.sub', 'org.example' ] for key in self.keys: self.trie.add(key, key) def test_values(self): for k in self.keys: self.assertTrue(self.trie.get(k) == k, "Trie::get")
class DottedKeyTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_paths(self): for path in self.trie.paths(): self.assertTrue(path in self.keys, "Trie::paths") paths = list(self.trie.paths()) self.assertTrue(len(paths) == 4, "Trie::paths length") def test_allpathvalues(self): values = self.trie.getAllPathValues('com.example.sub') self.assertTrue(len(values) == 2, "Trie::getAllPathValues") def test_subpaths(self): subpaths = self.trie.getSubPaths('com.example.sub') self.assertTrue(len(subpaths) == 2, "Trie::getSubPaths") self.assertTrue(subpaths[0] == 'com.example', "Trie::getSubPaths") self.assertTrue(subpaths[1] == 'com.example.sub', "Trie::getSubPaths")
class DottedKeyTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED) self.keys = [ 'com.example', 'com.baz', 'com.example.sub', 'org.example' ] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_paths(self): for path in self.trie.paths(): self.assertTrue(path in self.keys, "Trie::paths") paths = list(self.trie.paths()) self.assertTrue(len(paths) == 4, "Trie::paths length") def test_allpathvalues(self): values = self.trie.getAllPathValues('com.example.sub') self.assertTrue(len(values) == 2, "Trie::getAllPathValues") def test_subpaths(self): subpaths = self.trie.getSubPaths('com.example.sub') self.assertTrue(len(subpaths) == 2, "Trie::getSubPaths") self.assertTrue(subpaths[0] == 'com.example', "Trie::getSubPaths") self.assertTrue(subpaths[1] == 'com.example.sub', "Trie::getSubPaths")
class TriePathsTest(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.example.sub', 'org.example', 'com.other', 'com.other.sub', 'net.example', 'com.example.sub2'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_paths(self): comPaths = list(self.trie.paths(prefix = 'com')) examplePaths = list(self.trie.paths(prefix = 'com.example')) self.assertTrue(len(comPaths) == 5, "Trie::paths(prefix)") self.assertTrue(len(examplePaths) == 3, "Trie::paths(prefix)")
class TrieLenTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_len(self): self.assertTrue(len(self.trie) == 4, "Trie::__len__") def test_removes(self): self.trie.remove('org.example') self.trie.remove('com.example') self.assertTrue(len(self.trie) == 3, "Trie::__len__")
class TrieLenTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = [ 'com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub' ] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_len(self): self.assertTrue(len(self.trie) == 4, "Trie::__len__") def test_removes(self): self.trie.remove('org.example') self.trie.remove('com.example') self.assertTrue(len(self.trie) == 3, "Trie::__len__")
class StoreCountTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = [ 'com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub' ] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_paths(self): for path in self.trie.paths(): self.assertTrue(path in self.keys, "Trie::paths") paths = list(self.trie.paths()) self.assertTrue(len(paths) == 4, "Trie::paths length") def test_counts(self): countParts = {'com.example': 2, 'com.baz': 1, 'com.example.sub': 3} for countPart in countParts.items(): val = self.trie.get(countPart[0]) self.assertTrue(val == countPart[1], "Trie::get with STORE_COUNT") def test_removes(self): self.trie.remove('org.example') self.trie.remove('com.example') self.assertTrue( self.trie.get('org.example') is None, "remove single path") self.assertTrue( self.trie.get('com.example') == 1, "remove double path")
class StoreCountTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_paths(self): for path in self.trie.paths(): self.assertTrue(path in self.keys, "Trie::paths") paths = list(self.trie.paths()) self.assertTrue(len(paths) == 4, "Trie::paths length") def test_counts(self): countParts = { 'com.example': 2, 'com.baz': 1, 'com.example.sub': 3 } for countPart in countParts.items(): val = self.trie.get(countPart[0]) self.assertTrue(val == countPart[1], "Trie::get with STORE_COUNT") def test_removes(self): self.trie.remove('org.example') self.trie.remove('com.example') self.assertTrue(self.trie.get('org.example') is None, "remove single path") self.assertTrue(self.trie.get('com.example') == 1, "remove double path")
def test_add_trie(self): addDict = { 'com.example': 1, 'com.example.sub2': 1, 'org.other': 1, 'org.example.sub': 1, 'net.example': 1 } addTrie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) + addDict nt = self.trie + addTrie self.assertTrue(len(nt) == 7, "Trie::__add__(dict) length") for key in addDict.keys(): self.assertTrue(nt.has(key), "Trie::__add__(dict) contents") for key in self.keys: self.assertTrue(nt.has(key), "Trie::__add__(dict) contents")
class SubPathCountTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = [ 'com.example', 'com.blah', 'com.example.sub1', 'com.example.sub2' ] for key in self.keys: self.trie.add(key, 1, atAllSubPaths=True) def test_paths(self): for k in self.keys: self.assertTrue(k in self.trie, "Trie::paths has with atAllSubPaths") def test_counts(self): countParts = { 'com': 4, 'com.example': 3, 'com.blah': 1, 'com.example.sub1': 1, 'com.example.sub2': 1 } for countPart in countParts.items(): val = self.trie.get(countPart[0]) self.assertTrue(val == countPart[1], "Trie::get with atAllSubPaths") def test_removes(self): self.trie.remove('com.example.sub2', atAllSubPaths=True) countParts = { 'com': 3, 'com.example': 2, 'com.blah': 1, 'com.example.sub1': 1 } for countPart in countParts.items(): val = self.trie.get(countPart[0]) self.assertTrue(val == countPart[1], "Trie::remove with atAllSubPaths")
class SubPathCountTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.blah', 'com.example.sub1', 'com.example.sub2'] for key in self.keys: self.trie.add(key, 1, atAllSubPaths = True) def test_paths(self): for k in self.keys: self.assertTrue(k in self.trie, "Trie::paths has with atAllSubPaths") def test_counts(self): countParts = { 'com': 4, 'com.example': 3, 'com.blah': 1, 'com.example.sub1': 1, 'com.example.sub2': 1 } for countPart in countParts.items(): val = self.trie.get(countPart[0]) self.assertTrue(val == countPart[1], "Trie::get with atAllSubPaths") def test_removes(self): self.trie.remove('com.example.sub2', atAllSubPaths = True) countParts = { 'com': 3, 'com.example': 2, 'com.blah': 1, 'com.example.sub1': 1 } for countPart in countParts.items(): val = self.trie.get(countPart[0]) self.assertTrue(val == countPart[1], "Trie::remove with atAllSubPaths")
def setUp(self): self.trie = Trie() self.keys = ['bar', 'baz', 'barbell', 'foo', 'food', 'bar', 'bazbuzz', 'bazbuzz'] for key in self.keys: self.trie.add(key, 1)
class StringKeyTests(unittest.TestCase): def setUp(self): self.trie = Trie() self.keys = ['bar', 'baz', 'barbell', 'foo', 'food', 'bar', 'bazbuzz', 'bazbuzz'] for key in self.keys: self.trie.add(key, 1) def test_paths(self): paths = list(self.trie.paths()) self.assertTrue(len(paths) == 6, "Trie::paths") def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_in(self): for key in self.keys: self.assertTrue(key in self.trie, "Trie::__contains__") def test_get(self): for key in self.keys: self.assertTrue(self.trie.get(key) is not None, "Trie::get") def test_getItem(self): for key in self.keys: self.assertTrue(self.trie[key] is not None, "Trie::__getitem__") def test_remove(self): self.trie.remove('bar', 1) self.trie.remove('baz', 1) self.assertTrue(self.trie.get('bar') == 1, "Trie::remove") self.assertTrue(self.trie.get('baz') is None, "Trie::remove") def test_removeAll(self): self.trie.removeAll('bar') self.assertTrue(self.trie.get('bar') is None, "Trie::removeAll") self.assertTrue(self.trie.get('barbell') is not None, "Trie::removeAll retain leaves") def test_setItem(self): self.trie['blah'] = 1 self.assertTrue(self.trie.get('blah') == 1, "Trie::__setitem__") def test_missingItems(self): notIn = ['bubbles', 'barb', 'bazz'] for key in notIn: self.assertTrue(self.trie.get(key) is None, "Missing items") def test_singleItems(self): for item in ['baz', 'barbell', 'foo', 'food']: self.assertTrue(self.trie.get(item) == 1, "Trie::add non-duplicates") def test_doubleItems(self): for item in ['bar', 'bazbuzz']: self.assertTrue(self.trie.get(item) == [1, 1], "Trie::add duplicates")
def setUp(self): self.trie = Trie(keyFunction=KEY_DOTTED, storeFunction=STORE_COUNT) self.keys = ['com.example', 'com.example.sub', 'org.example'] for key in self.keys: self.trie.add(key, 1)
def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example'] for key in self.keys: self.trie.add(key, key)
def test_add_mixedtrie(self): addTrie = Trie() self.assertRaises(TypeError, self.trie.__add__, addTrie)
This example uses: * The STORE_COUNT storage function * The Trie::add(atAllSubPaths) option to track all prefix combinations """ import sys sys.path.append("../") from Trieful import Trie, STORE_COUNT import time from datetime import timedelta if __name__ == "__main__": # Setup a trie, using most of the defaults t = Trie(storeFunction=STORE_COUNT) print "Building dictionary Trie" # Read all of the words from the shared dictionary, and add them to the Trie dictfile = open('/usr/share/dict/words', 'r') # Track how long it takes to build out the Trie st = time.time() for word in dictfile: t.add(word.strip(), atAllSubPaths=True) ed = time.time() dictfile.close() print "\tBuilt Trie of %i words in %s (%0.2f words / second)" % ( len(t), str(timedelta(seconds=ed - st)), (len(t) * 1.0) / (ed - st))
This example uses: * The STORE_COUNT storage function * The Trie::add(atAllSubPaths) option to track all prefix combinations """ import sys sys.path.append("../") from Trieful import Trie, STORE_COUNT import time from datetime import timedelta if __name__ == "__main__": # Setup a trie, using most of the defaults t = Trie(storeFunction = STORE_COUNT) print "Building dictionary Trie" # Read all of the words from the shared dictionary, and add them to the Trie dictfile = open('/usr/share/dict/words', 'r') # Track how long it takes to build out the Trie st = time.time() for word in dictfile: t.add(word.strip(), atAllSubPaths = True) ed = time.time() dictfile.close() print "\tBuilt Trie of %i words in %s (%0.2f words / second)" % (len(t), str(timedelta(seconds = ed - st)), (len(t) * 1.0) / (ed - st))
class StringKeyTests(unittest.TestCase): def setUp(self): self.trie = Trie() self.keys = [ 'bar', 'baz', 'barbell', 'foo', 'food', 'bar', 'bazbuzz', 'bazbuzz' ] for key in self.keys: self.trie.add(key, 1) def test_paths(self): paths = list(self.trie.paths()) self.assertTrue(len(paths) == 6, "Trie::paths") def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_in(self): for key in self.keys: self.assertTrue(key in self.trie, "Trie::__contains__") def test_get(self): for key in self.keys: self.assertTrue(self.trie.get(key) is not None, "Trie::get") def test_getItem(self): for key in self.keys: self.assertTrue(self.trie[key] is not None, "Trie::__getitem__") def test_remove(self): self.trie.remove('bar', 1) self.trie.remove('baz', 1) self.assertTrue(self.trie.get('bar') == 1, "Trie::remove") self.assertTrue(self.trie.get('baz') is None, "Trie::remove") def test_removeAll(self): self.trie.removeAll('bar') self.assertTrue(self.trie.get('bar') is None, "Trie::removeAll") self.assertTrue( self.trie.get('barbell') is not None, "Trie::removeAll retain leaves") def test_setItem(self): self.trie['blah'] = 1 self.assertTrue(self.trie.get('blah') == 1, "Trie::__setitem__") def test_missingItems(self): notIn = ['bubbles', 'barb', 'bazz'] for key in notIn: self.assertTrue(self.trie.get(key) is None, "Missing items") def test_singleItems(self): for item in ['baz', 'barbell', 'foo', 'food']: self.assertTrue( self.trie.get(item) == 1, "Trie::add non-duplicates") def test_doubleItems(self): for item in ['bar', 'bazbuzz']: self.assertTrue( self.trie.get(item) == [1, 1], "Trie::add duplicates")
def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.blah', 'com.example.sub1', 'com.example.sub2'] for key in self.keys: self.trie.add(key, 1, atAllSubPaths = True)
def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.baz', 'com.example.sub', 'org.example', 'com.example', 'com.example.sub', 'com.example.sub'] for key in self.keys: self.trie.add(key, 1)
class TriePruneTests(unittest.TestCase): def setUp(self): self.trie = Trie(keyFunction = KEY_DOTTED, storeFunction = STORE_COUNT) self.keys = ['com.example', 'com.example.sub', 'org.example', 'com.other', 'com.other.sub', 'net.example', 'com.example.sub2'] for key in self.keys: self.trie.add(key, 1) def test_has(self): for key in self.keys: self.assertTrue(self.trie.has(key), "Trie::has") def test_outoforder_paths(self): self.trie.add("com.zanzibar") self.trie.add("com.foobar") self.trie.prune('com') comPaths = list(self.trie.paths(prefix = 'com')) self.assertTrue(len(comPaths) == 0, "Trie::paths(prefix)") self.assertTrue(len(self.trie) == 2, "Trie::__len__") def test_paths(self): self.trie.prune('com.example.sub') comPaths = list(self.trie.paths(prefix = 'com')) self.assertTrue(len(comPaths) == 4, "Trie::paths(prefix)") self.trie.prune('com') comPaths = list(self.trie.paths(prefix = 'com')) self.assertTrue(len(comPaths) == 0, "Trie::paths(prefix)") self.assertTrue(len(self.trie) == 2, "Trie::__len__")