Skip to content

Commit 2081048

Browse files
committed
CLN: add formats/printing.py, strip from common.py
1 parent 3b027af commit 2081048

File tree

4 files changed

+403
-384
lines changed

4 files changed

+403
-384
lines changed

pandas/core/common.py

-253
Original file line numberDiff line numberDiff line change
@@ -1261,65 +1261,6 @@ def _try_sort(iterable):
12611261
def _count_not_none(*args):
12621262
return sum(x is not None for x in args)
12631263

1264-
# -----------------------------------------------------------------------------
1265-
# miscellaneous python tools
1266-
1267-
1268-
def adjoin(space, *lists, **kwargs):
1269-
"""
1270-
Glues together two sets of strings using the amount of space requested.
1271-
The idea is to prettify.
1272-
1273-
----------
1274-
space : int
1275-
number of spaces for padding
1276-
lists : str
1277-
list of str which being joined
1278-
strlen : callable
1279-
function used to calculate the length of each str. Needed for unicode
1280-
handling.
1281-
justfunc : callable
1282-
function used to justify str. Needed for unicode handling.
1283-
"""
1284-
strlen = kwargs.pop('strlen', len)
1285-
justfunc = kwargs.pop('justfunc', _justify)
1286-
1287-
out_lines = []
1288-
newLists = []
1289-
lengths = [max(map(strlen, x)) + space for x in lists[:-1]]
1290-
# not the last one
1291-
lengths.append(max(map(len, lists[-1])))
1292-
maxLen = max(map(len, lists))
1293-
for i, lst in enumerate(lists):
1294-
nl = justfunc(lst, lengths[i], mode='left')
1295-
nl.extend([' ' * lengths[i]] * (maxLen - len(lst)))
1296-
newLists.append(nl)
1297-
toJoin = zip(*newLists)
1298-
for lines in toJoin:
1299-
out_lines.append(_join_unicode(lines))
1300-
return _join_unicode(out_lines, sep='\n')
1301-
1302-
1303-
def _justify(texts, max_len, mode='right'):
1304-
"""
1305-
Perform ljust, center, rjust against string or list-like
1306-
"""
1307-
if mode == 'left':
1308-
return [x.ljust(max_len) for x in texts]
1309-
elif mode == 'center':
1310-
return [x.center(max_len) for x in texts]
1311-
else:
1312-
return [x.rjust(max_len) for x in texts]
1313-
1314-
1315-
def _join_unicode(lines, sep=''):
1316-
try:
1317-
return sep.join(lines)
1318-
except UnicodeDecodeError:
1319-
sep = compat.text_type(sep)
1320-
return sep.join([x.decode('utf-8') if isinstance(x, str) else x
1321-
for x in lines])
1322-
13231264

13241265
def iterpairs(seq):
13251266
"""
@@ -1363,19 +1304,6 @@ def split_ranges(mask):
13631304
yield ranges[-1]
13641305

13651306

1366-
def indent(string, spaces=4):
1367-
dent = ' ' * spaces
1368-
return '\n'.join([dent + x for x in string.split('\n')])
1369-
1370-
1371-
def banner(message):
1372-
"""
1373-
Return 80-char width message declaration with = bars on top and bottom.
1374-
"""
1375-
bar = '=' * 80
1376-
return '%s\n%s\n%s' % (bar, message, bar)
1377-
1378-
13791307
def _long_prod(vals):
13801308
result = long(1)
13811309
for x in vals:
@@ -2181,187 +2109,6 @@ def in_ipython_frontend():
21812109

21822110
return False
21832111

2184-
# Unicode consolidation
2185-
# ---------------------
2186-
#
2187-
# pprinting utility functions for generating Unicode text or
2188-
# bytes(3.x)/str(2.x) representations of objects.
2189-
# Try to use these as much as possible rather then rolling your own.
2190-
#
2191-
# When to use
2192-
# -----------
2193-
#
2194-
# 1) If you're writing code internal to pandas (no I/O directly involved),
2195-
# use pprint_thing().
2196-
#
2197-
# It will always return unicode text which can handled by other
2198-
# parts of the package without breakage.
2199-
#
2200-
# 2) If you need to send something to the console, use console_encode().
2201-
#
2202-
# console_encode() should (hopefully) choose the right encoding for you
2203-
# based on the encoding set in option "display.encoding"
2204-
#
2205-
# 3) if you need to write something out to file, use
2206-
# pprint_thing_encoded(encoding).
2207-
#
2208-
# If no encoding is specified, it defaults to utf-8. Since encoding pure
2209-
# ascii with utf-8 is a no-op you can safely use the default utf-8 if you're
2210-
# working with straight ascii.
2211-
2212-
2213-
def _pprint_seq(seq, _nest_lvl=0, max_seq_items=None, **kwds):
2214-
"""
2215-
internal. pprinter for iterables. you should probably use pprint_thing()
2216-
rather then calling this directly.
2217-
2218-
bounds length of printed sequence, depending on options
2219-
"""
2220-
if isinstance(seq, set):
2221-
fmt = u("{%s}")
2222-
else:
2223-
fmt = u("[%s]") if hasattr(seq, '__setitem__') else u("(%s)")
2224-
2225-
if max_seq_items is False:
2226-
nitems = len(seq)
2227-
else:
2228-
nitems = max_seq_items or get_option("max_seq_items") or len(seq)
2229-
2230-
s = iter(seq)
2231-
r = []
2232-
for i in range(min(nitems, len(seq))): # handle sets, no slicing
2233-
r.append(pprint_thing(
2234-
next(s), _nest_lvl + 1, max_seq_items=max_seq_items, **kwds))
2235-
body = ", ".join(r)
2236-
2237-
if nitems < len(seq):
2238-
body += ", ..."
2239-
elif isinstance(seq, tuple) and len(seq) == 1:
2240-
body += ','
2241-
2242-
return fmt % body
2243-
2244-
2245-
def _pprint_dict(seq, _nest_lvl=0, max_seq_items=None, **kwds):
2246-
"""
2247-
internal. pprinter for iterables. you should probably use pprint_thing()
2248-
rather then calling this directly.
2249-
"""
2250-
fmt = u("{%s}")
2251-
pairs = []
2252-
2253-
pfmt = u("%s: %s")
2254-
2255-
if max_seq_items is False:
2256-
nitems = len(seq)
2257-
else:
2258-
nitems = max_seq_items or get_option("max_seq_items") or len(seq)
2259-
2260-
for k, v in list(seq.items())[:nitems]:
2261-
pairs.append(pfmt %
2262-
(pprint_thing(k, _nest_lvl + 1,
2263-
max_seq_items=max_seq_items, **kwds),
2264-
pprint_thing(v, _nest_lvl + 1,
2265-
max_seq_items=max_seq_items, **kwds)))
2266-
2267-
if nitems < len(seq):
2268-
return fmt % (", ".join(pairs) + ", ...")
2269-
else:
2270-
return fmt % ", ".join(pairs)
2271-
2272-
2273-
def pprint_thing(thing, _nest_lvl=0, escape_chars=None, default_escapes=False,
2274-
quote_strings=False, max_seq_items=None):
2275-
"""
2276-
This function is the sanctioned way of converting objects
2277-
to a unicode representation.
2278-
2279-
properly handles nested sequences containing unicode strings
2280-
(unicode(object) does not)
2281-
2282-
Parameters
2283-
----------
2284-
thing : anything to be formatted
2285-
_nest_lvl : internal use only. pprint_thing() is mutually-recursive
2286-
with pprint_sequence, this argument is used to keep track of the
2287-
current nesting level, and limit it.
2288-
escape_chars : list or dict, optional
2289-
Characters to escape. If a dict is passed the values are the
2290-
replacements
2291-
default_escapes : bool, default False
2292-
Whether the input escape characters replaces or adds to the defaults
2293-
max_seq_items : False, int, default None
2294-
Pass thru to other pretty printers to limit sequence printing
2295-
2296-
Returns
2297-
-------
2298-
result - unicode object on py2, str on py3. Always Unicode.
2299-
2300-
"""
2301-
2302-
def as_escaped_unicode(thing, escape_chars=escape_chars):
2303-
# Unicode is fine, else we try to decode using utf-8 and 'replace'
2304-
# if that's not it either, we have no way of knowing and the user
2305-
# should deal with it himself.
2306-
2307-
try:
2308-
result = compat.text_type(thing) # we should try this first
2309-
except UnicodeDecodeError:
2310-
# either utf-8 or we replace errors
2311-
result = str(thing).decode('utf-8', "replace")
2312-
2313-
translate = {'\t': r'\t', '\n': r'\n', '\r': r'\r', }
2314-
if isinstance(escape_chars, dict):
2315-
if default_escapes:
2316-
translate.update(escape_chars)
2317-
else:
2318-
translate = escape_chars
2319-
escape_chars = list(escape_chars.keys())
2320-
else:
2321-
escape_chars = escape_chars or tuple()
2322-
for c in escape_chars:
2323-
result = result.replace(c, translate[c])
2324-
2325-
return compat.text_type(result)
2326-
2327-
if (compat.PY3 and hasattr(thing, '__next__')) or hasattr(thing, 'next'):
2328-
return compat.text_type(thing)
2329-
elif (isinstance(thing, dict) and
2330-
_nest_lvl < get_option("display.pprint_nest_depth")):
2331-
result = _pprint_dict(thing, _nest_lvl, quote_strings=True,
2332-
max_seq_items=max_seq_items)
2333-
elif (is_sequence(thing) and
2334-
_nest_lvl < get_option("display.pprint_nest_depth")):
2335-
result = _pprint_seq(thing, _nest_lvl, escape_chars=escape_chars,
2336-
quote_strings=quote_strings,
2337-
max_seq_items=max_seq_items)
2338-
elif isinstance(thing, compat.string_types) and quote_strings:
2339-
if compat.PY3:
2340-
fmt = "'%s'"
2341-
else:
2342-
fmt = "u'%s'"
2343-
result = fmt % as_escaped_unicode(thing)
2344-
else:
2345-
result = as_escaped_unicode(thing)
2346-
2347-
return compat.text_type(result) # always unicode
2348-
2349-
2350-
def pprint_thing_encoded(object, encoding='utf-8', errors='replace', **kwds):
2351-
value = pprint_thing(object) # get unicode representation of object
2352-
return value.encode(encoding, errors, **kwds)
2353-
2354-
2355-
def console_encode(object, **kwds):
2356-
"""
2357-
this is the sanctioned way to prepare something for
2358-
sending *to the console*, it delegates to pprint_thing() to get
2359-
a unicode representation of the object relies on the global encoding
2360-
set in display.encoding. Use this everywhere
2361-
where you output to the console.
2362-
"""
2363-
return pprint_thing_encoded(object, get_option("display.encoding"))
2364-
23652112

23662113
def _maybe_match_name(a, b):
23672114
a_has = hasattr(a, 'name')

0 commit comments

Comments
 (0)