Beispiel #1
0
def get_partial_reconstructor(obj):
    mapping = {
        types.FunctionType: 'function',
        types.GeneratorType: 'generator'
    }
    objtype = class_of(obj)
    default = "%s.%s" % (objtype.__module__, objtype.__name__)
    return mapping.get(objtype, default)
Beispiel #2
0
    def __init__(self, obj, serialize):
        CompositeObject.__init__(self, obj)

        self.args = map(serialize, obj.args)
        self.constructor_format = "%s(%%s)" % class_name(obj)
        self.imports = set()

        if class_of(obj) in BUILTIN_ENVIRONMENT_ERROR_TYPES and obj.filename is not None:
            self.args.append(serialize(obj.filename))
Beispiel #3
0
    def __init__(self, obj, serialize):
        CompositeObject.__init__(self, obj)

        self.args = map(serialize, obj.args)
        self.constructor_format = "%s(%%s)" % class_name(obj)
        self.imports = set()

        if class_of(obj) in BUILTIN_ENVIRONMENT_ERROR_TYPES and obj.filename is not None:
            self.args.append(serialize(obj.filename))
Beispiel #4
0
def get_human_readable_id(obj):
    # Get human readable id based on object's value,
    if obj is True:
        return 'true'
    elif obj is False:
        return 'false'

    # ... based on object's type,
    objclass = class_of(obj)
    mapping = {
        list: 'list',
        dict: 'dict',
        tuple: 'tuple',
        six.text_type: 'unicode_string',
        types.GeneratorType: 'generator'
    }
    objid = mapping.get(objclass)
    if objid:
        return objid

    # ... or based on its supertype.
    if isinstance(obj, Exception):
        return underscore(objclass.__name__)
    elif isinstance(obj, RePatternType):
        return "%s_pattern" % string2id(obj.pattern)
    elif isinstance(obj, types.FunctionType):
        if obj.__name__ == '<lambda>':
            return "function"
        return "%s_function" % obj.__name__
    else:
        # str() may raise an exception.
        try:
            string = str(obj)
        except:
            string = "<>"
        # Looks like an instance without a custom __str__ defined.
        if string.startswith("<"):
            return "%s_instance" % underscore(objclass.__name__)
        else:
            return string2id(string)
Beispiel #5
0
def get_human_readable_id(obj):
    # Get human readable id based on object's value,
    if obj is True:
        return 'true'
    elif obj is False:
        return 'false'

    # ... based on object's type,
    objclass = class_of(obj)
    mapping = {list: 'list',
               dict: 'dict',
               tuple: 'tuple',
               unicode: 'unicode_string',
               types.GeneratorType: 'generator'}
    objid = mapping.get(objclass)
    if objid:
        return objid

    # ... or based on its supertype.
    if isinstance(obj, Exception):
        return underscore(objclass.__name__)
    elif isinstance(obj, RePatternType):
        return "%s_pattern" % string2id(obj.pattern)
    elif isinstance(obj, types.FunctionType):
        if obj.func_name == '<lambda>':
            return "function"
        return "%s_function" % obj.func_name
    else:
        # str() may raise an exception.
        try:
            string = str(obj)
        except:
            string = "<>"
        # Looks like an instance without a custom __str__ defined.
        if string.startswith("<"):
            return "%s_instance" % underscore(objclass.__name__)
        else:
            return string2id(string)
Beispiel #6
0
def is_builtin_exception(obj):
    """Return True if given object is an instance of a built-in exception, like
    NameError or EOFError. Return False for instances of user-defined
    exceptions.
    """
    return class_of(obj) in BUILTIN_EXCEPTION_TYPES
Beispiel #7
0
def id_of_class_of(obj):
    klass = class_of(obj)
    return (klass.__module__, klass.__name__)
Beispiel #8
0
def get_partial_reconstructor(obj):
    mapping = {types.FunctionType: 'function',
               types.GeneratorType: 'generator'}
    objtype = class_of(obj)
    default = "%s.%s" % (objtype.__module__, objtype.__name__)
    return mapping.get(objtype, default)
Beispiel #9
0
def is_builtin_exception(obj):
    """Return True if given object is an instance of a built-in exception, like
    NameError or EOFError. Return False for instances of user-defined
    exceptions.
    """
    return class_of(obj) in BUILTIN_EXCEPTION_TYPES
Beispiel #10
0
def id_of_class_of(obj):
    klass = class_of(obj)
    return (klass.__module__, klass.__name__)