python dump_python的var_dump,打印对象内容

from __future__ importprint_functionfrom types importNoneType__author__ = "Shamim Hasnath"

__copyright__ = "Copyright 2013, Shamim Hasnath"

__license__ = "BSD License"

__version__ = "1.0.1"TAB_SIZE= 4infs=[]defdisplay(o, space, num, key, typ):

st= ""l=[]ifkey:if typ isdict:

st+= " " * space + "['%s'] =>"

else:

st+= " " * space + "%s =>"l.append(key)elif space >0:

st+= " " * space + "[%d] =>"l.append(num)else: #at the very start

st += "#%d"l.append(num)if type(o) in(tuple, list, dict, int, str, float, long, bool, NoneType, unicode):

st+= "%s(%s)"l.append(type(o).__name__)if type(o) in(int, float, long, bool, NoneType):

l.append(o)else:

l.append(len(o))if type(o) in(str, unicode):

st+= '"%s"'l.append(o)elifisinstance(o, object):

st+= "object(%s) (%d)"l.append(o.__class__.__name__)

l.append(len(getattr(o,'__dict__', {})))#print(st % tuple(l))

infs.append(st %tuple(l))defdisplay_s(o, space, num, key, typ):

st= ""l=[]ifkey:if typ isdict:

st+= " " * space + "['%s']=>"

else:

st+= " " * space + "%s=>"l.append(key)#elif space > 0:

#st += " " * space + "[%d] => "

#l.append(num)

#else: # at the very start

#st += "#%d "

#l.append(num)

if type(o) in(tuple, list, dict, int, str, float, long, bool, NoneType, unicode):

st+= "%s"

#l.append(type(o).__name__)

if type(o) in(int, float, long, bool, NoneType):

l.append(o)else:

l.append('')if type(o) in(str, unicode):

st+= '"%s"'l.append(o)elifisinstance(o, object):

st+= "%s"l.append(o.__class__.__name__)#l.append(len(getattr(o, '__dict__', {})))

#print(st % tuple(l))

infs.append(st %tuple(l))defdump(o, space, num, key, typ):if type(o) in(str, int, float, long, bool, NoneType, unicode):

display(o, space, num, key, typ)elifisinstance(o, object):

display(o, space, num, key, typ)

num=0if type(o) in(tuple, list, dict):

typ= type(o) #type of the container of str, int, long, float etc

elifisinstance(o, object):

o= getattr(o, '__dict__', {})

typ=objectfor i ino:

space+=TAB_SIZEif type(o) isdict:

dump(o[i], space, num, i, typ)else:

dump(i, space, num,'', typ)

num+= 1space-=TAB_SIZEdefdump_s(o, space, num, key, typ):if type(o) in(str, int, float, long, bool, NoneType, unicode):

display_s(o, space, num, key, typ)elifisinstance(o, object):

display_s(o, space, num, key, typ)

num=0if type(o) in(tuple, list, dict):

typ= type(o) #type of the container of str, int, long, float etc

elifisinstance(o, object):

o= getattr(o, '__dict__', {})

typ=objectfor i ino:

space+=TAB_SIZEif type(o) isdict:

dump_s(o[i], space, num, i, typ)else:

dump_s(i, space, num,'', typ)

num+= 1space-=TAB_SIZEdef_get_space_num(s):

i=0for c ins:if c == ' ':

i+=1

else:breaks=s[i:]returni,sdef var_dump(*obs):"""shows structured information of a object, list, tuple etc"""

globalinfs

infs=[]

i=0for x inobs:

dump(x, 0, i,'', object)

i+= 1

for inf ininfs:print(inf)def var_dump_s(*obs):"""shows structured information of a object, list, tuple etc"""

globalinfs

infs=[]

i=0for x inobs:

dump_s(x, 0, i,'', object)

i+= 1strs=[]

bsn=0for inf ininfs:

sn, s=_get_space_num(inf)if sn >bsn:

strs.append('{')if sn

strs.append('},')if sn == bsn and sn !=0:

strs.append(',')

strs.append(s)

bsn=snwhile bsn >0:

strs.append('}')

bsn= bsn -TAB_SIZEreturn ''.join(strs)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值