5
Convert a string produced by python's repr() into the
6
corresponding data structure, without calling eval().
8
return Builder().build(getObj(s))
12
return compiler.parse(s).getChildren()[1].getChildren()[0].getChildren()[1]
14
class UnknownType(Exception):
20
m = getattr(self, 'build_'+o.__class__.__name__, None)
22
raise UnknownType(o.__class__.__name__)
25
def build_List(self, o):
26
return map(self.build, o.getChildren())
28
def build_Const(self, o):
31
def build_Dict(self, o):
33
i = iter(map(self.build, o.getChildren()))
38
def build_Tuple(self, o):
39
return tuple(self.build_List(o))
41
def build_Name(self, o):
44
raise UnknownType('Name')
46
def build_Add(self, o):
47
real, imag = map(self.build_Const, o.getChildren())
51
raise UnknownType('Add')
52
if not isinstance(imag, complex) or imag.real != 0.0:
53
raise UnknownType('Add')