Created
January 8, 2014 02:15
-
-
Save snowleung/8310592 to your computer and use it in GitHub Desktop.
origin :unknown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types | |
LIST_TYPE = (types.ListType, types.TupleType) | |
DICT_TYPE = (types.DictType) | |
COMPLICATE_TYPE = (types.InstanceType) | |
VALUE_TYPE = (types.IntType, types.StringType, types.FloatType, types.LongType, types.BooleanType) | |
def obj2dict(obj): | |
if isinstance(obj, LIST_TYPE): | |
return [obj2dict(o) for o in obj] | |
if isinstance(obj, VALUE_TYPE): | |
return str(obj) | |
if isinstance(obj, DICT_TYPE): | |
for key in obj: | |
if not isinstance(obj[key], VALUE_TYPE): | |
obj[key] = obj2dict(obj[key]) | |
return obj | |
if isinstance(obj, COMPLICATE_TYPE): | |
tmp = {} | |
for o in dir(obj): | |
if not callable(getattr(obj, o)) and not o.startswith('_'): | |
tmp[o] = getattr(obj, o) | |
return obj2dict(tmp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment