Skip to content

Instantly share code, notes, and snippets.

@snowleung
Created January 8, 2014 02:15
Show Gist options
  • Save snowleung/8310592 to your computer and use it in GitHub Desktop.
Save snowleung/8310592 to your computer and use it in GitHub Desktop.
origin :unknown
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