def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
python类zip_longest()的实例源码
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def assert_iterators_equal(self, xs, ys, test_id, limit=None):
# check that an iterator xs matches the expected results ys,
# up to a given limit.
if limit is not None:
xs = itertools.islice(xs, limit)
ys = itertools.islice(ys, limit)
sentinel = object()
pairs = itertools.zip_longest(xs, ys, fillvalue=sentinel)
for i, (x, y) in enumerate(pairs):
if x == y:
continue
elif x == sentinel:
self.fail('{}: iterator ended unexpectedly '
'at position {}; expected {}'.format(test_id, i, y))
elif y == sentinel:
self.fail('{}: unexpected excess element {} at '
'position {}'.format(test_id, x, i))
else:
self.fail('{}: wrong element at position {};'
'expected {}, got {}'.format(test_id, i, y, x))
def _create_argument_values_that_must_be_files_or_dirs(params):
"""
Loop over test_params and create temporary files for training/validation sources/targets.
"""
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
params = params.split()
regular_files_params = {'-vs', '-vt', '-t', '-s', '--source', '--target',
'--validation-source', '--validation-target'}
folder_params = {'--prepared-data', '-d'}
to_unlink = set()
for arg, val in grouper(params, 2):
if arg in regular_files_params and not os.path.isfile(val):
open(val, 'w').close()
to_unlink.add(val)
if arg in folder_params:
os.mkdir(val)
to_unlink.add(val)
return to_unlink
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def signature(function):
"""Return the signature of the given function (possibly without variable
positional and keyword arguments) as a string.
If available, the result should be determined by function
:func:`~inspect.signature` of module :mod:`inspect` (Python 3).
If something wrents wrong, a less general costum made string is
returned (Python 2).
"""
try:
return str(inspect.signature(function))
except BaseException:
argspec = inspect.getargspec(function)
args = argspec.args if argspec.args else []
defaults = argspec.defaults if argspec.defaults else []
strings = []
for arg, default in zip_longest(reversed(args), reversed(defaults)):
if default is None:
strings.insert(0, arg)
else:
strings.insert(0, '%s=%s' % (arg, default))
return '(%s)' % ', '.join(strings)
def grouper(iterable, n):
"""Collect data into fixed-length chunks or blocks.
If len(iterable) % n != 0, the last chunk is simply cut.
Example:
grouper('ABCDEFG', 3) -> ABC DEF G
Args:
iterable: Any iterable object.
n: The length of the chunks.
Returns:
An iterator that returns the chunks.
"""
args = [iter(iterable)] * n
groups = zip_longest(*args, fillvalue=None)
return (filter(lambda el: el is not None, group) for group in groups)
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def __eq__(self, other):
'''
>>> viewabledict({}) == viewabledict({})
True
>>> viewabledict({}) != viewabledict({1:1})
True
>>> viewabledict({1:1,3:3}) != viewabledict({1:1,2:2})
True
>>> viewabledict({1:1,2:2}) + viewabledict({3:3}) == viewabledict({1:1,2:2,3:3})
True
>>> viewabledict({}) + viewabledict({3:3}) == viewabledict({3:3})
True
'''
if not isinstance(other, Mapping):
return False
return all(a == b for a, b in zip_longest(
self.iteritems(), other.iteritems(), fillvalue=_NO_VAL))
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def next(self):
"""
Yields the next row from the source files.
"""
for self._filename in self._filenames:
self._open()
for row in self._csv_reader:
self._row_number += 1
yield dict(zip_longest(self._fields, row, fillvalue=''))
self._close()
self._row_number = -1
self._filename = None
raise StopIteration
# ------------------------------------------------------------------------------------------------------------------
def from_array(cls, array, xalign=0.5, yalign=0.5, padding=0, bg=0):
"""Create an image from an array of images."""
if not non_string_iterable(xalign): xalign = [xalign] * max(len(r) for r in array)
if not non_string_iterable(yalign): yalign = [yalign] * len(array)
align = [[Alignment((xalign[c], yalign[r])) for c,_ in enumerate(row)] for r,row in enumerate(array)]
padding = Padding(padding)
heights = [max(img.height if img is not None else 0 for img in row) + padding.y for row in array]
widths = [max(img.width if img is not None else 0 for img in column) + padding.x for column in zip_longest(*array)]
aimg = Image.new("RGBA", (sum(widths), sum(heights)), bg)
for r,row in enumerate(array):
for c,img in enumerate(row):
if img is None: continue
x = sum(widths[0:c]) + padding.l + int(align[r][c].x * (widths[c] - (img.width + padding.x)))
y = sum(heights[0:r]) + padding.u + int(align[r][c].y * (heights[r] - (img.height + padding.y)))
aimg.overlay(img, (x,y))
return aimg
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes
def tabulate(vals):
# From pfmoore on GitHub:
# https://github.com/pypa/pip/issues/3651#issuecomment-216932564
assert len(vals) > 0
sizes = [0] * max(len(x) for x in vals)
for row in vals:
sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
result = []
for row in vals:
display = " ".join([str(c).ljust(s) if c is not None else ''
for s, c in zip_longest(sizes, row)])
result.append(display)
return result, sizes