]>
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
22 # Objects that hold multiple objects (can be looped over).
29 # Objects that we shouldn't really probe.
35 # Simple objects we don't decode further.
50 # Objects that are dictionary based.
56 # Standard python objects we don't normally expand.
65 _C
= lambda x
: '\033[%im' % x
67 return '%s%s%s' % (c
, m
, self
.NORMAL
)
81 def __init__(self
, obj
, depth
=None, out
=None, linelim
=150,
82 show_internal
=False, show_all
=False, show_std
=False):
84 out
= self
.cStringIO
.StringIO()
85 #out = self.sys.stdout
88 self
.depth
= depth
if depth
else self
.MAX_DEPTH
89 self
.line_limit
= linelim
90 self
.show_internal
= show_internal
91 self
.show_all
= show_all
92 self
.show_std
= show_std
97 if hasattr(out
, 'getvalue'):
98 self
.pydoc
.pager(out
.getvalue())
103 """Truncate |s| length to self.line_limit bytes"""
104 if len(s
) > self
.line_limit
:
105 s
= '%s %s' % (s
[0:self
.line_limit
],
106 self
.C('<truncated>', self
.RED
))
109 def dump(self
, obj
, depth
=0, name
=None):
110 """Dump |obj| with |name|"""
112 def w(msg
, indent
=indent
, color
=None):
113 for line
in msg
.splitlines():
115 line
= self
.C(line
, color
)
116 self
.out
.write('%s%s\n' % (indent
, line
))
117 def d(obj
, **kwargs
):
119 self
.dump(obj
, depth
=depth
+ 1, **kwargs
)
120 except Exception as e
:
121 w(' <error probing>: %s' % (e
,), color
=self
.RED
)
124 if ((not self
.show_std
and isinstance(obj
, self
.TYPES_STANDARD
)) or
125 isinstance(obj
, self
.TYPES_SCALAR
)):
126 w('%s: %s' % (self
.C(name
, self
.BOLD
), obj
), indent
=indent
[:-2])
131 w('%s: %s' % (self
.C(name
, self
.BOLD
),
132 self
.C('<loop>', self
.RED
)), indent
=indent
[:-2])
139 w('%s' % (name
,), color
=self
.BOLD
, indent
=indent
[:-2])
144 w('%s: %s' % (type(obj
), self
.trunc(objs
),))
146 w('Object.type: %s' % (type(obj
),))
147 w(' .repr: %s' % (self
.trunc(objr
),))
148 w(' .str : %s' % (self
.trunc(objs
),))
150 if depth
> self
.depth
:
151 w('<stop; depth limit hit at %i>' % (depth
,), color
=self
.RED
)
154 if isinstance(obj
, self
.TYPES_ITERABLES
):
156 for i
, o
in enumerate(obj
):
157 d(o
, name
='[%i]' % (i
,))
158 elif isinstance(obj
, self
.TYPES_SCALAR
):
159 # Scalar types; already shown above.
161 elif isinstance(obj
, self
.TYPES_DICT
):
163 for k
, v
in obj
.items():
164 d(v
, name
='{%r}' % (k
,))
165 elif isinstance(obj
, self
.TYPES_STANDARD
):
166 # Standard types; already shown above.
168 elif isinstance(obj
, self
.TYPES_CALLABLE
):
169 # Callable functions.
170 doc
= getattr(obj
, 'func_doc', None)
172 w('"""%s"""' % (doc
.strip(),), indent
=indent
+ ' ',
175 # Unknown type; probe it!
176 doc
= getattr(obj
, '__doc__', None)
178 w('"""%s"""' % (doc
.strip(),), indent
=indent
+ ' ',
181 for k
, v
in getattr(obj
, '__dict__', {}).items():
182 d(v
, name
='{%r}' % (k
,))
186 s
= self
.trunc(repr(getattr(obj
, k
)))
187 except Exception as e
:
188 w(' %s: %s' % (self
.C(k
, self
.BOLD
),
189 self
.C('<error> %s' % str(e
), self
.RED
)))
192 if k
.startswith('__'):
193 if self
.show_all
or not k
.endswith('__'):
194 w(' %s (skipping): %s' % (self
.C(k
, self
.BOLD
), s
))
195 elif self
.show_internal
or not k
.startswith('_'):
196 d(getattr(obj
, k
), name
=k
)
199 def dump(*args
, **kwargs
):
200 d
= Dump(*args
, **kwargs
)