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 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)
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")
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))
def test_add_mixedtrie(self): addTrie = Trie() self.assertRaises(TypeError, self.trie.__add__, addTrie)
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)