]>
git.wh0rd.org - home.git/blob - .ipython/profile_default/startup/50-dump.py
e1910eae62059286bf136ff198338a8db967d73f
2 # Released into the public domain.
3 # Written by Mike Frysinger <vapier>.
5 """Module for diving into python objects."""
7 from __future__
import print_function
13 from cStringIO
import StringIO
15 from io
import StringIO
25 # Objects that hold multiple objects (can be looped over).
32 # Objects that we shouldn't really probe.
38 # Simple objects we don't decode further.
53 # Objects that are dictionary based.
59 # Standard python objects we don't normally expand.
68 _C
= lambda x
: '\033[%im' % x
70 return '%s%s%s' % (c
, m
, self
.NORMAL
)
84 def __init__(self
, obj
, depth
=None, out
=None, linelim
=150,
85 show_internal
=False, show_all
=False, show_std
=False):
88 #out = self.sys.stdout
91 self
.depth
= depth
if depth
else self
.MAX_DEPTH
92 self
.line_limit
= linelim
93 self
.show_internal
= show_internal
94 self
.show_all
= show_all
95 self
.show_std
= show_std
100 if hasattr(out
, 'getvalue'):
101 self
.pydoc
.pager(out
.getvalue())
106 """Truncate |s| length to self.line_limit bytes"""
107 if len(s
) > self
.line_limit
:
108 s
= '%s %s' % (s
[0:self
.line_limit
],
109 self
.C('<truncated>', self
.RED
))
112 def dump(self
, obj
, depth
=0, name
=None):
113 """Dump |obj| with |name|"""
115 def w(msg
, indent
=indent
, color
=None):
116 for line
in msg
.splitlines():
118 line
= self
.C(line
, color
)
119 self
.out
.write('%s%s\n' % (indent
, line
))
120 def d(obj
, **kwargs
):
122 self
.dump(obj
, depth
=depth
+ 1, **kwargs
)
123 except Exception as e
:
124 w(' <error probing>: %s' % (e
,), color
=self
.RED
)
127 if ((not self
.show_std
and isinstance(obj
, self
.TYPES_STANDARD
)) or
128 isinstance(obj
, self
.TYPES_SCALAR
)):
129 w('%s: %s' % (self
.C(name
, self
.BOLD
), obj
), indent
=indent
[:-2])
134 w('%s: %s' % (self
.C(name
, self
.BOLD
),
135 self
.C('<loop>', self
.RED
)), indent
=indent
[:-2])
142 w('%s' % (name
,), color
=self
.BOLD
, indent
=indent
[:-2])
147 w('%s: %s' % (type(obj
), self
.trunc(objs
),))
149 w('Object.type: %s' % (type(obj
),))
150 w(' .repr: %s' % (self
.trunc(objr
),))
151 w(' .str : %s' % (self
.trunc(objs
),))
153 if depth
> self
.depth
:
154 w('<stop; depth limit hit at %i>' % (depth
,), color
=self
.RED
)
157 if isinstance(obj
, self
.TYPES_ITERABLES
):
159 for i
, o
in enumerate(obj
):
160 d(o
, name
='[%i]' % (i
,))
161 elif isinstance(obj
, self
.TYPES_SCALAR
):
162 # Scalar types; already shown above.
164 elif isinstance(obj
, self
.TYPES_DICT
):
166 for k
, v
in obj
.items():
167 d(v
, name
='{%r}' % (k
,))
168 elif isinstance(obj
, self
.TYPES_STANDARD
):
169 # Standard types; already shown above.
171 elif isinstance(obj
, self
.TYPES_CALLABLE
):
172 # Callable functions.
173 doc
= getattr(obj
, 'func_doc', None)
175 w('"""%s"""' % (doc
.strip(),), indent
=indent
+ ' ',
178 # Unknown type; probe it!
179 doc
= getattr(obj
, '__doc__', None)
181 w('"""%s"""' % (doc
.strip(),), indent
=indent
+ ' ',
184 for k
, v
in getattr(obj
, '__dict__', {}).items():
185 d(v
, name
='{%r}' % (k
,))
189 s
= self
.trunc(repr(getattr(obj
, k
)))
190 except Exception as e
:
191 w(' %s: %s' % (self
.C(k
, self
.BOLD
),
192 self
.C('<error> %s' % str(e
), self
.RED
)))
195 if k
.startswith('__'):
196 if self
.show_all
or not k
.endswith('__'):
197 w(' %s (skipping): %s' % (self
.C(k
, self
.BOLD
), s
))
198 elif self
.show_internal
or not k
.startswith('_'):
199 d(getattr(obj
, k
), name
=k
)
202 def dump(*args
, **kwargs
):
203 d
= Dump(*args
, **kwargs
)