#!/usr/bin/env python import re,sys class TreeNode: def __init__(self): self.label="" self.code="" self.children=[] def insert(self,code,label): branch=0 if code[0].isdigit(): branch=int(code[0]) else: branch=ord(code[0])-ord('a')+10 if len(code)>1: code=code[1:] self.children[branch].insert(code,label) else: a=TreeNode() a.label=label a.code=code self.children+=[a] def Print(self,depth): for i in range(depth): print '\t', print self.code,self.label for c in self.children: c.Print(depth+1) def getChoicesAlongPath(self,path): branch=0 if path[0].isdigit(): branch=int(path[0]) else: branch=ord(path[0])-ord('a')+10 if(len(self.children))<=branch: if branch==0: if len(path)>1: return [1]+self.getChoicesAlongPath(path[1:]) else: return [1] if len(path)>1: return [len(self.children)]+self.children[branch].getChoicesAlongPath(path[1:]) else: return [len(self.children)] def CodeToText(self,code): branch=0 if code[0]!='*': if code[0].isdigit(): branch=int(code[0]) else: branch=ord(code[0])-ord('a')+10 if(len(self.children))<=branch: if branch==0: if len(code)>1: return ["unspecified"]+self.CodeToText(code[1:]) else: return ["unspecified"] if len(code)>1: return [self.children[branch].label]+self.children[branch].CodeToText(code[1:]) else: return [self.children[branch].label] else: return [ "*" ] class Axis: def __init__(self): self.tree=TreeNode(); def Print(self): self.tree.Print(0) def getChoicesAlongPath(self,path): return self.tree.getChoicesAlongPath(path) def CodeToText(self,code): return self.tree.CodeToText(code) class Code: def __init__(self, filename): self.axes=[] ifs=open(filename,'r') lines=map(lambda line: re.sub("\n","",line), ifs.readlines()) ifs.close() lineNo=0 while lineNo