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,打印对象内容的更多相关文章
- 优雅的重载toString方法,打印对象内容而不是打印内存地址的方法
如果直接在日志或者System.out.println中打印java对象,会打印这个对象的内存地址,而不是具体内容. 为了便于调试,一般的做法有2种: 1.重写toStrong方法 2.将对象传入JS ...
- Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用
Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...
- python函数参数是值传递还是引用传递(以及变量间复制后是否保持一致):取决于对象内容可变不可变
函数参数传递本质上和变量整体复制一样,只是两个变量分别为形参a和实参b.那么,a=b后,a变了,b值是否跟着变呢?这取决于对象内容可变不可变 首先解释一下,什么是python对象的内容可变不可变? p ...
- 将Python打印的内容进行高亮的输出
将打印的内容进行高亮的显示 内容: 格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: "\033[41;36m something he ...
- Python笔记day20-面向对象
目录 面向对象 1 装饰器 1.1 装饰器是什么? 1.2 装饰器 2 面向对象 (Object Oriented) 简称OO 2.1 面向对象相关术语 2.2 类和对象 2.3 类和对象的实现和书写 ...
- 全面了解python中的类,对象,方法,属性
全面了解python中的类,对象,方法,属性 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...
- Python学习day25-面向对象之组合,多态和封装
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python即时网络爬虫项目: 内容提取器的定义(Python2.7版本)
1. 项目背景 在Python即时网络爬虫项目启动说明中我们讨论一个数字:程序员浪费在调测内容提取规则上的时间太多了(见上图),从而我们发起了这个项目,把程序员从繁琐的调测规则中解放出来,投入到更高端 ...
- Python中的可变对象和不可变对象
Python中的可变对象和不可变对象 什么是可变/不可变对象 不可变对象,该对象所指向的内存中的值不能被改变.当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一 ...
随机推荐
- webstorm快捷键 webstorm keymap内置快捷键英文翻译、中英对照说明
20160114参考网络上的快捷键,整理自己常用的: 查找/代替shift+shift 快速搜索所有文件,简便ctrl+shift+N 通过文件名快速查找工程内的文件(必记)ctrl+shift+al ...
- ubuntu 14.04 pytorch安装
一. pytorch官网上有安装说明: 但是在安装过程中,由于pip版本为1.5.4,需要先对pip版本进行升级才行,升级步骤如下: 1. sudo apt-get remove python-pip ...
- 使用GoAccess构建简单实时日志分析系统
很早就知道Nginx日志分析工具GoAccess,但之前由于只能静态分析,感觉不太强大.最近发现它能够实时显示报表而且报表也比之前强大很多能做趋势分析.因此果断下载安装.以下是基于CentOS的安装配 ...
- [docker]通过阿里云源安装docker && flannel不通问题解决(try this guy out)
docker清理容器 # 容器停止后就自动删除: docker run --rm centos /bin/echo "One"; # 杀死所有正在运行的容器: docker kil ...
- RxSwift之路 1#Swift语法知识准备
RxSwift之路 1#Swift语法知识准备 在开始学习 RxSwift 之前,一定要对 Swift 相关语法有所了解,否则就很难理解为什么可以这样.关于 Swift 的学习其实只要看看 Swift ...
- mysql知识汇总
一.数据类型介绍 数据类型 字节长度 范围或用法 bigint 8 无符号[0,2^64-1],有符号[-2^63 ,2^63 -1] binary(M) M 类似Char的二进制存储,只包含byte ...
- 【Unity】通用的Debugger日志模块
模块代码整理自 http://gad.qq.com/lore/catalog/10007 Debugger类.提供打印日志的静态方法. using System; using System.IO; n ...
- windows下JDK环境配置与Android SDK环境配置
一.JDK环境配置1.配置变量名:JAVA_HOME变量值:jdk安装的绝对路径. 变量名:Path(在系统变量中找到并选中Path点击下面的编辑按钮,不要删除原本变量值中的任何一个字母,在这个变量值 ...
- 关于Retrofit网络请求URL中含有可变参数的处理
开题:在此默认各位看官对Retrofit.以及Okhttp已经有过一定的了解及应用,所以今天我们不谈基础入门的东西,今天我们谈在Retrofit请求接口管理类中URL参数含有动态参数的处理方式.一般我 ...
- 02Hadoop二次排序2
案例: 数据: 邮编 | 日期 |金额 ILMN,2013-12-05,97.65GOOD,2013-12-09,1078.14IBM,2013-12-09,177.46ILMN, ...