def in_lambda(string): return putinto(string, "lambda: %s")
def map_types(string): return putinto(string, "map(type, %s)")
def type_of(string): return putinto(string, "type(%s)")
def list_of(strings): return putinto(join(', ', strings), "[%s]")
def constructor_as_string(object, assigned_names={}): """For a given object (either a SerializedObject or a list of them) return a string representing a code that will construct it. >>> from test.helper import make_fresh_serialize >>> serialize = make_fresh_serialize() >>> m = Module(None, 'myclasses') It handles built-in types >>> constructor_as_string(serialize(123)) '123' >>> constructor_as_string(serialize('string')) "'string'" >>> constructor_as_string([serialize(1), serialize('two')]) "[1, 'two']" as well as instances of user-defined classes >>> obj = UserObject(None, Class('SomeClass', module=m)) >>> constructor_as_string(obj) 'SomeClass()' interpreting their arguments correctly >>> obj.add_call(MethodCall(Method('__init__', ['self', 'arg']), {'arg': serialize('whatever')}, serialize(None))) >>> constructor_as_string(obj) "SomeClass('whatever')" even if they're user objects themselves: >>> otherobj = UserObject(None, Class('SomeOtherClass', module=m)) >>> otherobj.add_call(MethodCall(Method('__init__', ['self', 'object']), {'object': obj}, serialize(None))) >>> constructor_as_string(otherobj) "SomeOtherClass(SomeClass('whatever'))" or they are already named: >>> s = serialize("string") >>> anotherobj = UserObject(None, Class('AnotherClass', module=m)) >>> anotherobj.add_call(MethodCall(Method('__init__', ['self', 's']), {'s': s}, serialize(None))) >>> constructor_as_string(anotherobj, {s: 's'}) 'AnotherClass(s)' Handles composite objects: >>> constructor_as_string(serialize([1, "a", None])) "[1, 'a', None]" even when they contain instances of user-defined classes: >>> constructor_as_string(SequenceObject([obj], lambda x:x)) "[SomeClass('whatever')]" or other composite objects: >>> constructor_as_string(serialize((23, [4, [5]], {'a': 'b'}))) "(23, [4, [5]], {'a': 'b'})" or already named objects: >>> seq = serialize(["a", None]) >>> astring = seq.contained_objects[0] >>> constructor_as_string(seq, {astring: 'astring'}) '[astring, None]' Empty tuples are recreated properly: >>> constructor_as_string(serialize((((42,),),))) '(((42,),),)' Recreated objects keep their import information: >>> cs = constructor_as_string(UserObject(None, Class('MyClass', module=m))) >>> cs 'MyClass()' >>> cs.imports set([('myclasses', 'MyClass')]) Library objects like xml.dom.minidom.Element are recreated properly as well: >>> from xml.dom.minidom import Element >>> constructor_as_string(serialize(Element("tag", "uri", "prefix"))) "Element('tag', 'uri', 'prefix')" """ if isinstance(object, list): return list_of(map(constructor_as_string, object)) elif assigned_names.has_key(object): return CodeString(assigned_names[object]) elif isinstance(object, UserObject): # Look for __init__ call and base the constructor on that. init_call = object.get_init_call() if init_call: cs = call_as_string_for(object.klass.name, init_call.input, init_call.definition, assigned_names) else: cs = call_as_string(object.klass.name, {}) return addimport(cs, import_for(object.klass)) elif isinstance(object, ImmutableObject): return CodeString(object.reconstructor, imports=object.imports) elif isinstance(object, (CompositeObject, LibraryObject)): arguments = join(', ', get_contained_objects_info(object, assigned_names)) return putinto(arguments, object.constructor_format, object.imports) elif isinstance(object, GeneratorObject): if object.is_activated(): cs = call_as_string_for(object.definition.name, object.args, object.definition) return addimport(cs, import_for(object.definition)) else: return todo_value('generator') elif isinstance(object, UnknownObject): return todo_value(object.partial_reconstructor) else: raise TypeError("constructor_as_string expected SerializedObject at input, not %s" % object)
def constructor_as_string(object, assigned_names={}): """For a given object (either a SerializedObject or a list of them) return a string representing a code that will construct it. >>> from test.helper import make_fresh_serialize >>> serialize = make_fresh_serialize() >>> m = Module(None, 'myclasses') It handles built-in types >>> constructor_as_string(serialize(123)) '123' >>> constructor_as_string(serialize('string')) "'string'" >>> constructor_as_string([serialize(1), serialize('two')]) "[1, 'two']" as well as instances of user-defined classes >>> obj = UserObject(None, Class('SomeClass', module=m)) >>> constructor_as_string(obj) 'SomeClass()' interpreting their arguments correctly >>> obj.add_call(MethodCall(Method('__init__', ['self', 'arg']), {'arg': serialize('whatever')}, serialize(None))) >>> constructor_as_string(obj) "SomeClass('whatever')" even if they're user objects themselves: >>> otherobj = UserObject(None, Class('SomeOtherClass', module=m)) >>> otherobj.add_call(MethodCall(Method('__init__', ['self', 'object']), {'object': obj}, serialize(None))) >>> constructor_as_string(otherobj) "SomeOtherClass(SomeClass('whatever'))" or they are already named: >>> s = serialize("string") >>> anotherobj = UserObject(None, Class('AnotherClass', module=m)) >>> anotherobj.add_call(MethodCall(Method('__init__', ['self', 's']), {'s': s}, serialize(None))) >>> constructor_as_string(anotherobj, {s: 's'}) 'AnotherClass(s)' Handles composite objects: >>> constructor_as_string(serialize([1, "a", None])) "[1, 'a', None]" even when they contain instances of user-defined classes: >>> constructor_as_string(SequenceObject([obj], lambda x:x)) "[SomeClass('whatever')]" or other composite objects: >>> constructor_as_string(serialize((23, [4, [5]], {'a': 'b'}))) "(23, [4, [5]], {'a': 'b'})" or already named objects: >>> seq = serialize(["a", None]) >>> astring = seq.contained_objects[0] >>> constructor_as_string(seq, {astring: 'astring'}) '[astring, None]' Empty tuples are recreated properly: >>> constructor_as_string(serialize((((42,),),))) '(((42,),),)' Recreated objects keep their import information: >>> cs = constructor_as_string(UserObject(None, Class('MyClass', module=m))) >>> cs 'MyClass()' >>> cs.imports set([('myclasses', 'MyClass')]) Library objects like xml.dom.minidom.Element are recreated properly as well: >>> from xml.dom.minidom import Element >>> constructor_as_string(serialize(Element("tag", "uri", "prefix"))) "Element('tag', 'uri', 'prefix')" """ if isinstance(object, list): return list_of(map(constructor_as_string, object)) elif assigned_names.has_key(object): return CodeString(assigned_names[object]) elif isinstance(object, UserObject): # Look for __init__ call and base the constructor on that. init_call = object.get_init_call() if init_call: cs = call_as_string_for(object.klass.name, init_call.input, init_call.definition, assigned_names) else: cs = call_as_string(object.klass.name, {}) return addimport(cs, import_for(object.klass)) elif isinstance(object, ImmutableObject): return CodeString(object.reconstructor, imports=object.imports) elif isinstance(object, (CompositeObject, LibraryObject)): arguments = join(', ', get_contained_objects_info(object, assigned_names)) return putinto(arguments, object.constructor_format, object.imports) elif isinstance(object, GeneratorObject): if object.is_activated(): cs = call_as_string_for(object.definition.name, object.args, object.definition) return addimport(cs, import_for(object.definition)) else: return todo_value('generator') elif isinstance(object, UnknownObject): return todo_value(object.partial_reconstructor) else: raise TypeError( "constructor_as_string expected SerializedObject at input, not %s" % object)