Python is starting to grow on me
Ok, so I’m busily parsing lawtext with EBNF grammars, but the resulting parse tree is somewhat cumbersome to work with. Each node is a 4-tuple, [tagname,startindex,endindex,childnodes], where childnodes is a list of similar 4-tuples:
for n in r[3]:
print "%s: %s" % (n[0],indata[n[1]:n[2])
if n[0] == 'refs':
for sn in n[3]:
print " %s: %s" % (sn[0],indata[sn[1]:sn[2]])
So, late at night I begin to ponder if it would be possible to write some sort of OO-wrapper around this structure of tuples in tuples, so that I could write somewhat more readable code:
for n in r.nodes:
print "%s: %s" % (n.tag,n.text)
if n.tag == 'refs':
for sn in n.nodes:
print " %s: %s" % (sn.tag,sn.text)
Turns out it takes less than 20 lines of code:
class NodeTree:
def __init__(self,root,data,offset=0,isRoot=True):
self.data = data
self.root = root
self.isRoot = isRoot
self.offset = offset
def __getattr__(self,name):
if name == "text":
return self.data
elif name == "tag":
return (self.isRoot and 'root' or self.root[0])
elif name == "nodes":
res = []
for p in (self.isRoot and self.root[1] or self.root[3]):
res.append(NodeTree(p,self.data[p[1]-self.offset:p[2]-self.offset],p[1],False))
return res
else:
raise AttributeError
A good python programmer could probably trim down the above to ten lines, but still. I don’t know if it’s the language, or the particular problem I’m trying to solve, but I’ve been programming a whole lot more recursively lately.
Tags: programmering, python