python 的var_dump
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的更多相关文章
- python的var_dump,打印对象内容
from __future__ import print_function from types import NoneType __author__ = "Shamim Hasnath&q ...
- Python与PHP通过XMLRPC进行通信
Python与PHP通过XMLRPC进行通信:服务器端用Python,客户端用PHP. 服务器端:xmlrpc_server.py #!/usr/bin/python # coding: UTF-8 ...
- python用httplib模块发送get和post请求
在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能. 1.python发送GET请求 我在本地建立一个测试环境,test.php的内容就是输出一句话: 1 e ...
- 插入排序-Python与PHP实现版
插入排序Python实现 import random a=[random.randint(1,999) for x in range(0,36)] # 直接插入排序算法 def insertionSo ...
- 选择排序-Python与PHP实现版
选择排序Python实现 import random # 生成待排序数组 a=[random.randint(1,999) for x in range(0,36)] # 选择排序 def selec ...
- 常见查找算法之php, js,python版
常用算法 >>>1. 顺序查找, 也叫线性查找, 它从第一个记录开始, 挨个进行对比, 是最基本的查找技术 javaScript 版顺序查找算法: // 顺序查找(线性查找) 只做找 ...
- ava、Python和PHP三者的区别
Java.Python和PHP三者的区别 2017年07月15日 22:09:21 书生_AABB 阅读数:18994 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- java、python与留下迷点的php hash collision
JAVA 生成java的碰撞数据比较简单 根据网上资料可知: at,bU,c6的在java中的hash值是相同的 则可以根据这三个不断做 笛卡尔积 简单明了就是做字符串拼接. 举个例子 把A当做at, ...
- ruby,python及curl post请求
#飘红部分为变量 test_url="http://test" body_hash={"value"=>100, "year"=> ...
随机推荐
- hdu 3746 Cyclic Nacklace
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 思路:KMP中Next数组的应用,求出最小的循环节,题目的意思是只能在字符串的后面上添加新的字符 ...
- [译]:Orchard入门——部件管理
原文链接:Managing Widgets 在Orchard中,部件是可以加入到当前当前主题任何位置或区域(如侧栏sidebar或底部区域footer)的UI块(如:HTML)或代码部分(如:内容部分 ...
- UWP 禁止Pivot swip 手势
以前想要禁止内置的手势动作,看了一下网上是设置 IsLocked="True". 但是拿到UWP上来,靠,设置了之后header只显示当前的那个header.这样的设计真是丑爆了. ...
- JavaScript Bind()趣味解答 包懂~~
首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...
- Listview的使用
最近一个多月忙着使用新的技术来做项目,现在项目上线了,嗯,发现android有些生疏了,所以今天特地写了这一篇博客来相信的讲解一些基础知识,同事呢,也可以让我温故知新一下.进入正题. 什么是listv ...
- 用 TWebBrowser 查找网页上的按钮,编辑框,
Form1.wb1.Navigate(Aurl); <table> <tr> <td style="text-align:right;">< ...
- 转载:Tomcat的JVM设置和连接数设置
Windows环境下修改“%TOMCAT_HOME%\bin\catalina.bat”文件,在文件开头增加如下设置:set JAVA_OPTS=-Xms256m -Xmx512m Linux环境下修 ...
- 设置R启动时自动加载常用的包或函数
在我前面的文章(http://www.cnblogs.com/homewch/p/5749850.html)中有提到R可以自定义启动环境,需要修改R安装文件中的ect文件夹下的配置文件Rprofile ...
- 初识SQL 执行顺序
SQL不同于一般的程序代码,会按照一定的顺序进行执行,他的第一个执行始终从from开始执行,虽然Select出现在第一位置但是执行顺序 确不是在第一个.有时候可能大家写了很久的代码,不一定能够很好的理 ...
- iOS UIView 动画浅谈
UIView 等会效果简单实现,哪一个登录页面的demo来举例子吧. + (void)animateWithDuration:(NSTimeInterval)duration animations:( ...