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. MVC+Easeyui dialog的小问题

    今天在尝试 MVC+Easyui的练习中遇到的一些,小问题. 在.net MVC 中  在_layout.cshtml中设置Easyui 环境  ,在传到子页中,发现$("#dlg" ...

  2. Mac常用终端命令

    一.基本命令 1.列出文件 ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions 参数 -w 显示中文,-l 详细信息, -a 包括 ...

  3. SQL入门语句之CREATE

    一.CREATE DATABASE语句 1.创建一个SQL数据库 CREATE DATABASE database_name 二.CREATE TABLE语句 1.只创建字段和相对应的数据类型 cre ...

  4. Android开发之---Activity生命周期

    Android开发中,有四大组件:Activity.Service.Content Provider.Broadcast Receiver,可以说,activity的使用是最频繁的了,这里来梳理一下与 ...

  5. C++基本语法

    一.static成员变量和static成员函数 1.普通成员变量每个对象有各自的一份,而静态成员变量一共就一份,为所有对象共享 2.普通成员函数必须具体作用于某个对象,而静态成员函数并不具体作用于某个 ...

  6. [Unity3D]NGUI用Sprite动画和屏幕自适应做游戏开始场景

    我们在玩任何一款手游产品时,都是先上来个logo界面,游戏欢迎界面等,这就意味着我们要做一款游戏需要多个场景,场景之间来回切换实现游戏逻辑,unity也不例外,所以从本篇开始将会介绍如何搭建多个场景, ...

  7. 使用Navicat 导入导出Mysql数据库

    1 导出 另外一种方式 2 导入,新建数据名称 3 导入,运行sql文件(步骤1中的) 推荐使用SQLyog进行导入数据比较好

  8. DevExpress的GridControl的实时加载数据解决方案(取代分页)

    http://blog.csdn.net/educast/article/details/4769457 evExpress是一套第三方控件 其中有类似DataGridView的控件 今天把针对Dev ...

  9. backbonejs使用

    backbone是一个非常好的前端MVC框架,将数据与逻辑分离出来,在稍大一点项目中,backbone都有用武之地. 个人感觉backbone最好的地方就是通过事件来管理数据改变导致的视图改变,bac ...

  10. 【水】基于ege的简单3D模拟

    我们模拟从被观察物体射出光线,在眼球焦点交汇,然后打到视网膜上成像 ——足够了吧,剩下的难度应该是普及- 只是有一些常数可以自己调一下,看着顺眼就好 #include <graphics.h&g ...