python - Nested arguments not compiling -
i'm trying compile code found here: http://semprog.com/psw/chapter2/simpletriple.py
...into python3 module. runs fine when choose 'run module' in idle, receive following syntax error when try create distribution:
file "/usr/local/lib/python3.2/dist-packages/simpletriple.py", line 9 def add(self, (sub, pred, obj)): ^ syntaxerror: invalid syntax
can point out wrong syntax?
here complete code:
import csv class simplegraph: def __init__(self): self._spo = {} self._pos = {} self._osp = {} def add(self, (sub, pred, obj)): """ adds triple graph. """ self._addtoindex(self._spo, sub, pred, obj) self._addtoindex(self._pos, pred, obj, sub) self._addtoindex(self._osp, obj, sub, pred) def _addtoindex(self, index, a, b, c): """ adds triple specified index. """ if not in index: index[a] = {b:set([c])} else: if b not in index[a]: index[a][b] = set([c]) else: index[a][b].add(c) def remove(self, (sub, pred, obj)): """ remove triple pattern graph. """ triples = list(self.triples((sub, pred, obj))) (delsub, delpred, delobj) in triples: self._removefromindex(self._spo, delsub, delpred, delobj) self._removefromindex(self._pos, delpred, delobj, delsub) self._removefromindex(self._osp, delobj, delsub, delpred) def _removefromindex(self, index, a, b, c): """ removes triple index , clears empty indermediate structures. """ try: bs = index[a] cset = bs[b] cset.remove(c) if len(cset) == 0: del bs[b] if len(bs) == 0: del index[a] # keyerrors occur if term missing, means wasn't valid delete: except keyerror: pass def triples(self, (sub, pred, obj)): """ generator on triple store. returns triples match given triple pattern. """ # check terms present in order use correct index: try: if sub != none: if pred != none: # sub pred obj if obj != none: if obj in self._spo[sub][pred]: yield (sub, pred, obj) # sub pred none else: retobj in self._spo[sub][pred]: yield (sub, pred, retobj) else: # sub none obj if obj != none: retpred in self._osp[obj][sub]: yield (sub, retpred, obj) # sub none none else: retpred, objset in self._spo[sub].items(): retobj in objset: yield (sub, retpred, retobj) else: if pred != none: # none pred obj if obj != none: retsub in self._pos[pred][obj]: yield (retsub, pred, obj) # none pred none else: retobj, subset in self._pos[pred].items(): retsub in subset: yield (retsub, pred, retobj) else: # none none obj if obj != none: retsub, predset in self._osp[obj].items(): retpred in predset: yield (retsub, retpred, obj) # none none none else: retsub, predset in self._spo.items(): retpred, objset in predset.items(): retobj in objset: yield (retsub, retpred, retobj) # keyerrors occur if query term wasn't in index, yield nothing: except keyerror: pass def value(self, sub=none, pred=none, obj=none): retsub, retpred, retobj in self.triples((sub, pred, obj)): if sub none: return retsub if pred none: return retpred if obj none: return retobj break return none def load(self, filename): f = open(filename, "rb") reader = csv.reader(f) sub, pred, obj in reader: sub = unicode(sub, "utf-8") pred = unicode(pred, "utf-8") obj = unicode(obj, "utf-8") self.add((sub, pred, obj)) f.close() def save(self, filename): f = open(filename, "wb") writer = csv.writer(f) sub, pred, obj in self.triples((none, none, none)): writer.writerow([sub.encode("utf-8"), pred.encode("utf-8"), obj.encode("utf-8")]) f.close() if __name__ == "__main__": g = simplegraph() g.add(("blade_runner", "name", "blade runner")) g.add(("blade_runner", "name", "blade runner")) g.add(("blade_runner", "release_date", "june 25, 1982")) g.add(("blade_runner", "directed_by", "ridley scott")) print list(g.triples((none, none, none))) print list(g.triples(("blade_runner", none, none))) print list(g.triples(("blade_runner", "name", none))) print list(g.triples(("blade_runner", "name", "blade runner"))) print list(g.triples(("blade_runner", none, "blade runner"))) print list(g.triples((none, "name", "blade runner"))) print list(g.triples((none, none, "blade runner"))) print list(g.triples(("foo", "name", "blade runner"))) print list(g.triples(("blade_runner", "foo", "blade runner"))) print list(g.triples(("blade_runner", "name", "foo")))
pep 3113 explains why feature, "tuple parameter unpacking", removed in python 3. explains how port code uses them. in case you'd need change function like:
def add(self, (sub, pred, obj)): self._addtoindex(self._spo, sub, pred, obj) self._addtoindex(self._pos, pred, obj, sub) self._addtoindex(self._osp, obj, sub, pred)
to version passes tuple single parameter , unpacks manually:
def add(self, sub_pred_obj): sub, pred, obj = sub_pred_obj self._addtoindex(self._spo, sub, pred, obj) self._addtoindex(self._pos, pred, obj, sub) self._addtoindex(self._osp, obj, sub, pred)
Comments
Post a Comment