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. 优雅的重载toString方法,打印对象内容而不是打印内存地址的方法

    如果直接在日志或者System.out.println中打印java对象,会打印这个对象的内存地址,而不是具体内容. 为了便于调试,一般的做法有2种: 1.重写toStrong方法 2.将对象传入JS ...

  2. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  3. python函数参数是值传递还是引用传递(以及变量间复制后是否保持一致):取决于对象内容可变不可变

    函数参数传递本质上和变量整体复制一样,只是两个变量分别为形参a和实参b.那么,a=b后,a变了,b值是否跟着变呢?这取决于对象内容可变不可变 首先解释一下,什么是python对象的内容可变不可变? p ...

  4. 将Python打印的内容进行高亮的输出

    将打印的内容进行高亮的显示 内容: 格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: "\033[41;36m something he ...

  5. Python笔记day20-面向对象

    目录 面向对象 1 装饰器 1.1 装饰器是什么? 1.2 装饰器 2 面向对象 (Object Oriented) 简称OO 2.1 面向对象相关术语 2.2 类和对象 2.3 类和对象的实现和书写 ...

  6. 全面了解python中的类,对象,方法,属性

    全面了解python中的类,对象,方法,属性 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...

  7. Python学习day25-面向对象之组合,多态和封装

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  8. Python即时网络爬虫项目: 内容提取器的定义(Python2.7版本)

    1. 项目背景 在Python即时网络爬虫项目启动说明中我们讨论一个数字:程序员浪费在调测内容提取规则上的时间太多了(见上图),从而我们发起了这个项目,把程序员从繁琐的调测规则中解放出来,投入到更高端 ...

  9. Python中的可变对象和不可变对象

    Python中的可变对象和不可变对象 什么是可变/不可变对象 不可变对象,该对象所指向的内存中的值不能被改变.当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一 ...

随机推荐

  1. DES加密解密算法C语言代码实现

    代码: #include<stdio.h> #include<string.h> #include<stdlib.h> /*-------------------- ...

  2. 全面理解Java内存模型(JMM)及volatile关键字(转载)

    关联文章: 深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoad ...

  3. appium 报错

    2018-11-27 18:05:56:313 - [Logcat] Logcat terminated with code 0, signal null 2018-11-27 18:05:56:31 ...

  4. Mybatis(二) 全局配置文件详解

    这节来说说全局配置文件的东西,非常简单.看一遍就懂了. --WH 一.全部配置内容 SqlMapConfig.xml的配置内容和顺序如下,顺序不能乱.现在来对这些属性的意思一一进行讲解. 二.prop ...

  5. 以太坊客户端Geth命令用法-参数详解【转载】

    原文链接:http://www.cnblogs.com/tinyxiong/p/7918706.html Geth在以太坊智能合约开发中最常用的工具(必备开发工具),一个多用途的命令行工具.熟悉Get ...

  6. 34对MyBatis的博客的整理心得

    对本博客的mybatis重新读一下,做一个整理.如下: 1:为什么会有mybatis,因为原生的jdbc方式有很大问题: (1)数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭 ...

  7. windows下自动删除过期文件的脚本

    windows下自动删除过期文件的脚本 前言: 比如日志文件每天都产生,时间长了就会有很大的一堆垃圾.整理一下 定时删除文件的方法. 正文: Windows: 定时删除tomcat日志和缓存.可以保留 ...

  8. 导出jqgrid表格数据为EXCEL文件,通过tableExport.js插件。

    今天公司项目需要做个导出功能,将jqgrid查询出的数据导出为EXCEL表格文件,期间遇到两个问题: 1.导出报错 uncaught exception: INVALID_CHARACTER_ERR: ...

  9. express.js graphql express-graphql

    文档 创建应用 const l = console.log; var express = require("express"); var graphqlHTTP = require ...

  10. Windows 环境下 wampserver 与 phpStudy 的环境配置

    一. wamperserver 1.下载好安装到本地指定目录,官网下载地址  http://www.wampserver.com 2.根据自己实际的安装路径,D:\pc\wampserver\wamp ...