-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path_var_dump.py
More file actions
135 lines (113 loc) · 3.38 KB
/
_var_dump.py
File metadata and controls
135 lines (113 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from __future__ import print_function
import sys
try:
from enum import Enum
except ImportError:
Enum = type(str)
try:
from types import NoneType
except ImportError:
NoneType = type(None)
if sys.version_info > (3,):
long = int
unicode = str
__author__ = "Shamim Hasnath"
__copyright__ = "Copyright 2013, Shamim Hasnath"
__license__ = "BSD License"
__version__ = "1.2.0"
TAB_SIZE = 4
def display(o, space, num, key, typ, proret):
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, Enum):
st += "Enum(%s)"
l.append(str(o))
elif isinstance(o, object):
st += "object(%s) (%s)"
nameValueString = None
try:
tmp = o.value
if tmp is not None:
tmp = str(tmp)
nameValueString = tmp
except:
# o.value does not exist or can not be converted to string
pass
if nameValueString is None or nameValueString == "":
nameValueString = o.__class__.__name__
else:
nameValueString = o.__class__.__name__ + "(" + nameValueString + ")"
l.append(nameValueString)
try:
l.append(len(o.__dict__))
except AttributeError:
l.append(str(o))
if proret:
print(st % tuple(l))
return st % tuple(l)
def dump(o, space, num, key, typ, proret):
r = ''
if type(o) in (str, int, float, long, bool, NoneType, unicode, Enum):
r += display(o, space, num, key, typ, proret)
elif isinstance(o, Enum):
r += display(o, space, num, key, typ, proret)
elif isinstance(o, object):
r += display(o, space, num, key, typ, proret)
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):
try:
o = o.__dict__
except AttributeError:
return r
typ = object
for i in o:
space += TAB_SIZE
if type(o) is dict:
r += dump(o[i], space, num, i, typ, proret)
else:
r += dump(i, space, num, '', typ, proret)
num += 1
space -= TAB_SIZE
return r
def var_dump(*obs):
"""
shows structured information of a object, list, tuple etc
"""
i = 0
for x in obs:
dump(x, 0, i, '', object, True)
i += 1
def var_export(*obs):
"""
returns output as as string
"""
r = ''
i = 0
for x in obs:
r += dump(x, 0, i, '', object, False)
i += 1
return r