]>
git.wh0rd.org - home.git/blob - .ipython/profile_default/startup/50-dump.py
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.
52 # Objects that are dictionary based.
57 # Standard python objects we don't normally expand.
66 _C
= lambda x
: '\033[%im' % x
68 return '%s%s%s' % (c
, m
, self
.NORMAL
)
82 def __init__(self
, obj
, depth
=None, out
=None, linelim
=150,
83 show_internal
=False, show_all
=False, show_std
=False):
86 #out = self.sys.stdout
89 self
.depth
= depth
if depth
else self
.MAX_DEPTH
90 self
.line_limit
= linelim
91 self
.show_internal
= show_internal
92 self
.show_all
= show_all
93 self
.show_std
= show_std
98 if hasattr(out
, 'getvalue'):
99 self
.pydoc
.pager(out
.getvalue())
104 """Truncate |s| length to self.line_limit bytes"""
105 if len(s
) > self
.line_limit
:
106 s
= '%s %s' % (s
[0:self
.line_limit
],
107 self
.C('<truncated>', self
.RED
))
110 def dump(self
, obj
, depth
=0, name
=None):
111 """Dump |obj| with |name|.
114 obj: The object to dump.
115 depth: How deep to recursively dive.
119 def w(msg
, indent
=indent
, color
=None):
120 for line
in msg
.splitlines():
122 line
= self
.C(line
, color
)
123 self
.out
.write('%s%s\n' % (indent
, line
))
124 def d(obj
, **kwargs
):
126 self
.dump(obj
, depth
=depth
+ 1, **kwargs
)
127 except Exception as e
:
128 w(' <error probing>: %s' % (e
,), color
=self
.RED
)
131 if ((not self
.show_std
and isinstance(obj
, self
.TYPES_STANDARD
)) or
132 isinstance(obj
, self
.TYPES_SCALAR
)):
133 w('%s: %s' % (self
.C(name
, self
.BOLD
), obj
), indent
=indent
[:-2])
138 w('%s: %s' % (self
.C(name
, self
.BOLD
),
139 self
.C('<loop>', self
.RED
)), indent
=indent
[:-2])
146 w('%s' % (name
,), color
=self
.BOLD
, indent
=indent
[:-2])
151 w('%s: %s' % (type(obj
), self
.trunc(objs
),))
153 w('Object.type: %s' % (type(obj
),))
154 w(' .repr: %s' % (self
.trunc(objr
),))
155 w(' .str : %s' % (self
.trunc(objs
),))
157 if depth
> self
.depth
:
158 w('<stop; depth limit hit at %i>' % (depth
,), color
=self
.RED
)
161 if isinstance(obj
, self
.TYPES_ITERABLES
):
163 for i
, o
in enumerate(obj
):
164 d(o
, name
='[%i]' % (i
,))
165 elif isinstance(obj
, self
.TYPES_SCALAR
):
166 # Scalar types; already shown above.
168 elif isinstance(obj
, self
.TYPES_DICT
):
170 for k
, v
in obj
.items():
171 d(v
, name
='{%r}' % (k
,))
172 elif isinstance(obj
, self
.TYPES_STANDARD
):
173 # Standard types; already shown above.
175 elif isinstance(obj
, self
.TYPES_CALLABLE
):
176 # Callable functions.
177 doc
= getattr(obj
, 'func_doc', None)
179 w('"""%s"""' % (doc
.strip(),), indent
=indent
+ ' ',
182 # Unknown type; probe it!
183 doc
= getattr(obj
, '__doc__', None)
185 w('"""%s"""' % (doc
.strip(),), indent
=indent
+ ' ',
188 for k
, v
in getattr(obj
, '__dict__', {}).items():
189 d(v
, name
='{%r}' % (k
,))
193 s
= self
.trunc(repr(getattr(obj
, k
)))
194 except Exception as e
:
195 w(' %s: %s' % (self
.C(k
, self
.BOLD
),
196 self
.C('<error> %s' % str(e
), self
.RED
)))
199 if k
.startswith('__'):
200 if self
.show_all
or not k
.endswith('__'):
201 w(' %s (skipping): %s' % (self
.C(k
, self
.BOLD
), s
))
202 elif self
.show_internal
or not k
.startswith('_'):
203 d(getattr(obj
, k
), name
=k
)
206 def dump(*args
, **kwargs
):
207 d
= Dump(*args
, **kwargs
)
209 dump
.__doc
__ = Dump
.dump
.__doc
__