from __future__ import print_function
from types import NoneType __author__ = "Shamim Hasnath"
__copyright__ = "Copyright 2013, Shamim Hasnath"
__license__ = "BSD License"
__version__ = "1.0.1" TAB_SIZE = 4 infs = [] def display(o, space, num, key, typ):
st = ""
l = []
if key:
if typ is dict:
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) elif isinstance(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)) def display_s(o, space, num, key, typ):
st = ""
l = []
if key:
if typ is dict:
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) elif isinstance(o, object):
st += "%s"
l.append(o.__class__.__name__)
# l.append(len(getattr(o, '__dict__', {}))) #print(st % tuple(l))
infs.append(st % tuple(l)) def dump(o, space, num, key, typ): if type(o) in (str, int, float, long, bool, NoneType, unicode):
display(o, space, num, key, typ) elif isinstance(o, object):
display(o, space, num, key, typ)
num = 0
if type(o) in (tuple, list, dict):
typ = type(o) # type of the container of str, int, long, float etc
elif isinstance(o, object):
o = getattr(o, '__dict__', {})
typ = object
for i in o:
space += TAB_SIZE
if type(o) is dict:
dump(o[i], space, num, i, typ)
else:
dump(i, space, num, '', typ)
num += 1
space -= TAB_SIZE def dump_s(o, space, num, key, typ): if type(o) in (str, int, float, long, bool, NoneType, unicode):
display_s(o, space, num, key, typ) elif isinstance(o, object):
display_s(o, space, num, key, typ)
num = 0
if type(o) in (tuple, list, dict):
typ = type(o) # type of the container of str, int, long, float etc
elif isinstance(o, object):
o = getattr(o, '__dict__', {})
typ = object
for i in o:
space += TAB_SIZE
if type(o) is dict:
dump_s(o[i], space, num, i, typ)
else:
dump_s(i, space, num, '', typ)
num += 1
space -= TAB_SIZE def _get_space_num(s):
i = 0
for c in s:
if c == ' ':
i+=1
else:
break
s = s[i:]
return i,s def var_dump(*obs):
"""
shows structured information of a object, list, tuple etc
"""
global infs
infs = []
i = 0
for x in obs:
dump(x, 0, i, '', object)
i += 1
for inf in infs:
print(inf) def var_dump_s(*obs):
"""
shows structured information of a object, list, tuple etc
"""
global infs
infs = []
i = 0
for x in obs:
dump_s(x, 0, i, '', object)
i += 1
strs = []
bsn = 0
for inf in infs:
sn, s = _get_space_num(inf)
if sn > bsn:
strs.append('{')
if sn < bsn:
strs.append('}, ')
if sn == bsn and sn != 0:
strs.append(', ')
strs.append(s)
bsn = sn
while bsn > 0:
strs.append('}')
bsn = bsn - TAB_SIZE return ''.join(strs)

测试例子:

from var_dump import *

class A:
def __init__(self,aa,bb):
self.a = aa
self.b = bb def pa(self):
print(self.a,self.b) class B:
def __init__(self):
self.y = 13423
self.g = 'sdsdsds'
self.ob = A(223,454)
a = A(3,4) a = B()
var_dump(a)
print('---------------------')
s = var_dump_s(a)
print(s)

输出:

# object(B) ()
y => int()
ob => object(A) ()
a => int()
b => int()
g => str() "sdsdsds"
---------------------
B{y=>, ob=>A{a=>, b=>}, g=>"sdsdsds"}

python 的var_dump的更多相关文章

  1. python的var_dump,打印对象内容

    from __future__ import print_function from types import NoneType __author__ = "Shamim Hasnath&q ...

  2. Python与PHP通过XMLRPC进行通信

    Python与PHP通过XMLRPC进行通信:服务器端用Python,客户端用PHP. 服务器端:xmlrpc_server.py #!/usr/bin/python # coding: UTF-8 ...

  3. python用httplib模块发送get和post请求

    在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能. 1.python发送GET请求 我在本地建立一个测试环境,test.php的内容就是输出一句话: 1 e ...

  4. 插入排序-Python与PHP实现版

    插入排序Python实现 import random a=[random.randint(1,999) for x in range(0,36)] # 直接插入排序算法 def insertionSo ...

  5. 选择排序-Python与PHP实现版

    选择排序Python实现 import random # 生成待排序数组 a=[random.randint(1,999) for x in range(0,36)] # 选择排序 def selec ...

  6. 常见查找算法之php, js,python版

    常用算法 >>>1. 顺序查找, 也叫线性查找, 它从第一个记录开始, 挨个进行对比, 是最基本的查找技术 javaScript 版顺序查找算法: // 顺序查找(线性查找) 只做找 ...

  7. ava、Python和PHP三者的区别

    Java.Python和PHP三者的区别 2017年07月15日 22:09:21 书生_AABB 阅读数:18994   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  8. java、python与留下迷点的php hash collision

    JAVA 生成java的碰撞数据比较简单 根据网上资料可知: at,bU,c6的在java中的hash值是相同的 则可以根据这三个不断做 笛卡尔积 简单明了就是做字符串拼接. 举个例子 把A当做at, ...

  9. ruby,python及curl post请求

    #飘红部分为变量 test_url="http://test" body_hash={"value"=>100, "year"=> ...

随机推荐

  1. 如何创建 Code Snippet

    比如有一行自定义代码段: @property (nonatomic,copy) NSString *<#string#>; 需要添加到 Code Snippet 上,以帮助开发人员开发更便 ...

  2. php总结二篇

    PHP的网站主要攻击方式: 1.命令注入(Command Injection) 2.eval注入(Eval Injection) 3.客户端脚本攻击(Script Insertion) 4.跨网站脚本 ...

  3. javaScript的function

    一.函数的声明方式 1.普通的函数声明 function box(num1,num2){ return num1+num2; } 2.使用变量初始化函数 var box=function(num1,n ...

  4. js 数据类型问题

    1. alert(type of 变量名) console.log(type of 变量名); 可以答应数据类型 2.var cost_price=parseFloat(parseFloat($(&q ...

  5. new 等于 malloc加构造函数

    1.new 是c++中的操作符,malloc是c 中的一个函数 2.new 不止是分配内存,而且会调用类的构造函数,同理delete会调用类的析构函数,而malloc则只分配内存,不会进行初始化类成员 ...

  6. JS中对象排序

    详细代码如下: var s=[{name:"abc",value:10},{name:"dbc",value:5},{name:"acc", ...

  7. nodejs review-04

    79 Secure your projects with HTTPS Express 生成SSL证书 openssl genrsa -out privkey.pem 1023 openssl req ...

  8. 如何將ViewData裡包含的Html輸出(MVC)

    如何將ViewData裡包含的Html輸出(MVC) 默認輸入ViewData裡的Htm系統會自動把標籤轉換而達不到預覽的效果, 我們如果要呈現解析後的HTML則要調用@MvcHtmlString.C ...

  9. HDU 3308 LCIS(线段树)

    题目链接 模板题吧,忘了好多,终于A了... #include <cstring> #include <cstdio> #include <string> #inc ...

  10. 为川师大女生支招 15年如何还200W

    就在昨儿一条新闻火遍全网,川师大21岁女生樊师贝发帖称,希望有人借她200万,为父母在城里买房15年还清,至于利息“可以用后半生来陪伴你”.她说,六旬父亲要负担家用,哥哥啃老,而她目前一分钱都还没挣到 ...