return to index |
nucular project page with download links |
In an attempt to make the discussion easy to understand there is a great deal of unneeded repetition in the code examples.
create
method:
def makeArchive(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "initializing archive" archive.create() print "archive created!"In this example the directory should be empty if it exists, and if the directory doesn't exist the parent directory (in this case
../testdata
must exist.
123FROG, 456BUNNY, 789KITTEN
are the identity
strings used to uniquely identify the entries in the archive.
def addSomeDictionaries(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") D = { "name": "frog", "food": "tastes delicious, like chicken", "descr": "little green slimy things", } print "adding", D archive.indexDictionary("123FROG", D) D = { "name": "bunny rabbit", "food": "just delicious with garlic", "descr": "cute and cuddly", } print "adding", D archive.indexDictionary("456BUNNY", D) D = { "name": "kitten", "note": "not edible", "descr": "cute and cuddly", } print "adding", D archive.indexDictionary("789KITTEN", D) print "SAVING session and making new entries visible now." archive.store(lazy=False)The last step of the function
store
s the entries
with immediate visibility -- this means that the entries are
made permanent and visible to all subsequent sessions.
def cuddlyQuery(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "making a query" query = archive.Query() print "look for entries with cuddly" query.anyWord("CUDDLY") print "getting query result dictionaries" dictionaries = query.resultDictionaries() count = 0 for D in dictionaries: print "found", D count+=1 print "total found", countHere the
query
is derived from the session object
and the query.anyWord
method specifies that the entry should
contain "cuddly" as a prefix to any word in any attribute. The
result of the query is extracted as a list of dictionaries using
the query.resultDictionaries
method. When evaluated
over the archive we just created above the function prints
creating session making a query look for entries with cuddly getting query result dictionaries found {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} found {'i': '789KITTEN', 'note': 'not edible', 'name': 'kitten', 'descr': 'cute and cuddly'} total found 2Note that the identities for the entries show up in the extracted dictionary as the value associated with the key "i".
For example
def cuddlyNotDelicious(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "look for entries with cuddly but without delicious" dictionaries = archive.dictionaries("cuddly ~delicious") print "total found", len(dictionaries) for D in dictionaries: print DHere the query string
"cuddly ~delicious"
looks for all
entries containing the word cuddly anywhere but not containing the word
delicious.
When evaluated the function prints
creating session look for entries with cuddly but without delicious total found 1 {'i': '789KITTEN', 'note': 'not edible', 'name': 'kitten', 'descr': 'cute and cuddly'}The Boolean Query Specification describes the syntax and usage of boolean queries.
def cuddlyAndDeliciousQuery(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "making a query" query = archive.Query() print "look for entries with cuddly" query.anyWord("CUDDLY") print "look for entries with delicious" query.anyWord("delicious") print "getting query result dictionaries" dictionaries = query.resultDictionaries() count = 0 for D in dictionaries: print "found", D count+=1 print "total found", countWhen evaluated the function prints
creating session making a query look for entries with cuddly look for entries with delicious getting query result dictionaries found {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} total found 1The equivalent boolean query string
"cute cuddly"
will also find the
cute and cuddly entries.
def addPhoneEntries(): PHONEBOOK = [ {"i": "Sally Smithers", "c":"uses too much salt", "p": "111-3333", "g":"female"}, {"i": "Joe Smithers", "c":"can't cook", "p": "111-3333", "g":"male"}, {"i": "Sandy Waller", "c":"delicious pizza", "p": "333-2222", "g":"female"}, {"i": "Joe Blow", "c":"great at a grill", "p": "333-2222", "g":"male"}, {"i": "Lola Waller", "n":"thinks snails are delicious", "p": "333-2222", "g":"female"}, ] from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") for D in PHONEBOOK: i = D["i"] print "now indexing", i archive.indexDictionary(i, D) print "storing updates with deferred visibility" archive.store()Oops! Here we used
archive.store()
without specifying
lazy=False
, so the updates have been stored in deferred
mode and will not be visible until the archive is aggregated.
def deliciousQuery(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "making a query" query = archive.Query() print "look for entries with delicious" query.anyWord("delicious") print "getting query result dictionaries" dictionaries = query.resultDictionaries() count = 0 for D in dictionaries: print "found", D count+=1 print "total found", countprints the following
creating session making a query look for entries with delicious getting query result dictionaries found {'i': '123FROG', 'food': 'tastes delicious, like chicken', 'name': 'frog', 'descr': 'little green slimy things'} found {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} total found 2The people entries don't show up because they are not visible yet.
def partiallyAggregate(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "aggregating update operations to temporary storage" archive.aggregateRecent() print "unlinking retired files" archive.cleanUp()After executing
partiallyAggregate()
if we execute
deliciousQuery()
again the function prints
creating session making a query look for entries with delicious getting query result dictionaries found {'i': '123FROG', 'food': 'tastes delicious, like chicken', 'name': 'frog', 'descr': 'little green slimy things'} found {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} found {'i': 'Lola Waller', 'p': '333-2222', 'g': 'female', 'n': 'thinks snails are delicious'} found {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} total found 4The additional entries show up because they have become visible after the aggregation operation.
def deliciousCookQuery(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "making a query" query = archive.Query() print "look for entries with delicious in 'c'" query.attributeWord("c", "delicious") print "getting query result dictionaries" dictionaries = query.resultDictionaries() count = 0 for D in dictionaries: print "found", D count+=1 print "total found", countWhen we restrict the search to the "c" attribute the function prints
creating session making a query look for entries with delicious in 'c' getting query result dictionaries found {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} total found 1The equivalent boolean query string
"c :.. delicious"
will
also find the same result.
def phoneStarts333Query(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "making a query" query = archive.Query() print "look for entries where 'p' STARTS WITH '333'" query.prefixAttribute("p", "333") print "getting query result dictionaries" dictionaries = query.resultDictionaries() count = 0 for D in dictionaries: print "found", D count+=1 print "total found", countWhen evaluated the function prints
creating session making a query look for entries where 'p' STARTS WITH '333' getting query result dictionaries found {'i': 'Joe Blow', 'p': '333-2222', 'c': 'great at a grill', 'g': 'male'} found {'i': 'Lola Waller', 'p': '333-2222', 'g': 'female', 'n': 'thinks snails are delicious'} found {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} total found 3The equivalent boolean query string
"p: 333"
will
also find the same result.
def femaleAndPhoneStarts333Query(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "making a query" query = archive.Query() print "look for entries where 'p' STARTS WITH '333'" query.prefixAttribute("p", "333") print "look for entries where 'g' is 'female'" query.matchAttribute("g", "female") print "getting query result dictionaries" dictionaries = query.resultDictionaries() count = 0 for D in dictionaries: print "found", D count+=1 print "total found", countWhen evaluated the function prints
creating session making a query look for entries where 'p' STARTS WITH '333' look for entries where 'g' is 'female' getting query result dictionaries found {'i': 'Lola Waller', 'p': '333-2222', 'g': 'female', 'n': 'thinks snails are delicious'} found {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} total found 2The equivalent boolean query string
"p:333 g=female"
will
also find the same result.
def completelyAggregate(): from nucular import Nucular print "creating session" archive = Nucular.Nucular("../testdata/APIExamples") print "aggregating update operations to temporary storage" archive.aggregateRecent() print "aggregating temporary with permanent storage" archive.moveTransientToBase() print "unlinking retired files" archive.cleanUp()