def doCommand(year, inputURI="/dev/stdin"): import sys global sax2rdf import thing from thing import load global kb #from thing import chatty #import sax2rdf import os from thing import Literal # Load the data: progress("Data from", inputURI) kb=load(inputURI) # print "# Size of kb: ", len(kb) totals = {} for s in kb.statements: obj = s.object() if isinstance(obj, Literal) try: value = int(obj.string) except: continue tot = totals.get(s.predicate(), 0) + value totals[s.predicate()] = tot ko = kb.newFormula() for pred in totals.keys(): ko.add(subj=pred, pred=qu.total, obj=ko.newSymbol(`totals[pred]`)) print ko.close().n3String()
def doCommand(year, inputURI="/dev/stdin"): """Fin - financial summary <command> <options> <inputURIs> Totals transactions by classes to which they are known to belong This is or was http://www.w3.org/2000/10/swap/pim/fin.py """ import sys global sax2rdf import thing from thing import load global kb #from thing import chatty #import sax2rdf import os # Load the data: home = os.environ["HOME"] personalInfo = load(home + "/.personal.n3") me = personalInfo.statementsMatching(pred=contact.ssn)[0].subject() myname = (personalInfo.the(subj=me, pred=contact.givenName).string + " " + personalInfo.the(subj=me, pred=contact.familyName).string) myssn = personalInfo.the(subj=me, pred=contact.ssn).string myhome = personalInfo.the(subj=me, pred=contact.home) mystreet = personalInfo.the(subj=myhome, pred=contact.street).string mystate = personalInfo.the(subj=myhome, pred=contact.stateOrProvince).string myzip = personalInfo.the(subj=myhome, pred=contact.postalCode).string assert myname and myssn and mystreet and mystate and myzip progress("Data from", inputURI) kb=load(inputURI) # print "# Size of kb: ", len(kb) # for /devel/WWW read http://www.w3.org/ when we have persistent cache stateInfo = load("/devel/WWW/2000/10/swap/test/dbork/data/USRegionState.n3") stateCodes = [] for s in stateInfo.statementsMatching(pred=state.code): stateCodes.append(s.object().string) progress("#", len(stateCodes), "state codes") categories = kb.each(pred=rdf.type, obj=qu.Cat) progress("%i categories found" % len(categories)) maUseTaxable = kb.each(pred=rdf.type, obj=ma.useTaxable) total = 0 rows = "" for s in maUseTaxable: date = kb.any(subj=s, pred=qu.date).__str__() year = int(date[0:4]) if int(year) != int(yearInQuestion): continue payees = kb.each(subj=s, pred=qu.payee) if str(payees[0]) == "Check" and len(payees) >1: payee = payees[1] else: payee = payees[0] payee=payee.string if payee[-3:-2] == " ": sc = payee [-2:] if sc in stateCodes and sc != "MA": amount = -float(kb.the(subj=s, pred=qu.amount).__str__()) mycat = "@@@" classes = kb.each(subj=s, pred=rdf.type) for c in classes: if c in categories: mycat = kb.the(subj=c, pred=rdfs.label) progress( "# %s %40s %10s" %(date, payee, `amount`)) rows = rows + """ <tr><td>%s</td><td>%s</td><td>%s</td> <td class='amount'>%7.2f</td><td class='amount'>0</td></tr>\n""" %( date[:10], payee, mycat, amount) total = total + amount progress('<%s>\n\t<%s> "%7.2f".' % ( ma.useTaxable.uriref(), qu.total.uriref(), total)) progress( "# ie sales tax of 5%% would be $%7.2f" % (total*0.05)) values = { "name": myname, "ssn": myssn, "address": mystreet, "state": mystate, "zip": myzip, "l1": yearInQuestion, "l2": "%7.2f" % total, "l3": "%7.2f" % (total * 0.05), "l4": "0", "l5": "%7.2f" % (total * 0.05), "l6": "0", "l7": "0", "l8": "%7.2f" % (total * 0.05), "l9": "%7.2f" % total, "l10": "0", "rows": rows } print substitute(form, values)
def doCommand(year, inputURI="/dev/stdin"): """Fin - financial summary <command> <options> <inputURIs> Totals transactions by classes to which they are known to belong This is or was http://www.w3.org/2000/10/swap/pim/fin.py """ import sys global sax2rdf import thing from thing import load global kb #from thing import chatty #import sax2rdf import os # Load the data: home = os.environ["HOME"] personalInfo = load(home + "/.personal.n3") me = personalInfo.statementsMatching(pred=contact.ssn)[0].subject() myname = (personalInfo.the(subj=me, pred=contact.givenName).string + " " + personalInfo.the(subj=me, pred=contact.familyName).string) myssn = personalInfo.the(subj=me, pred=contact.ssn).string myhome = personalInfo.the(subj=me, pred=contact.home) mystreet = personalInfo.the(subj=myhome, pred=contact.street).string mystate = personalInfo.the(subj=myhome, pred=contact.stateOrProvince).string myzip = personalInfo.the(subj=myhome, pred=contact.postalCode).string assert myname and myssn and mystreet and mystate and myzip progress("Data from", inputURI) kb = load(inputURI) # print "# Size of kb: ", len(kb) # for /devel/WWW read http://www.w3.org/ when we have persistent cache stateInfo = load( "/devel/WWW/2000/10/swap/test/dbork/data/USRegionState.n3") stateCodes = [] for s in stateInfo.statementsMatching(pred=state.code): stateCodes.append(s.object().string) progress("#", len(stateCodes), "state codes") categories = kb.each(pred=rdf.type, obj=qu.Cat) progress("%i categories found" % len(categories)) maUseTaxable = kb.each(pred=rdf.type, obj=ma.useTaxable) total = 0 rows = "" for s in maUseTaxable: date = kb.any(subj=s, pred=qu.date).__str__() year = int(date[0:4]) if int(year) != int(yearInQuestion): continue payees = kb.each(subj=s, pred=qu.payee) if str(payees[0]) == "Check" and len(payees) > 1: payee = payees[1] else: payee = payees[0] payee = payee.string if payee[-3:-2] == " ": sc = payee[-2:] if sc in stateCodes and sc != "MA": amount = -float(kb.the(subj=s, pred=qu.amount).__str__()) mycat = "@@@" classes = kb.each(subj=s, pred=rdf.type) for c in classes: if c in categories: mycat = kb.the(subj=c, pred=rdfs.label) progress("# %s %40s %10s" % (date, payee, ` amount `)) rows = rows + """ <tr><td>%s</td><td>%s</td><td>%s</td> <td class='amount'>%7.2f</td><td class='amount'>0</td></tr>\n""" % ( date[:10], payee, mycat, amount) total = total + amount progress('<%s>\n\t<%s> "%7.2f".' % (ma.useTaxable.uriref(), qu.total.uriref(), total)) progress("# ie sales tax of 5%% would be $%7.2f" % (total * 0.05)) values = { "name": myname, "ssn": myssn, "address": mystreet, "state": mystate, "zip": myzip, "l1": yearInQuestion, "l2": "%7.2f" % total, "l3": "%7.2f" % (total * 0.05), "l4": "0", "l5": "%7.2f" % (total * 0.05), "l6": "0", "l7": "0", "l8": "%7.2f" % (total * 0.05), "l9": "%7.2f" % total, "l10": "0", "rows": rows } print substitute(form, values)