Python itertools.izip_longest() Examples
The following are 30
code examples of itertools.izip_longest().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
itertools
, or try the search function
.
Example #1
Source File: utils.py From praatIO with MIT License | 7 votes |
def safeZip(listOfLists, enforceLength): """ A safe version of python's zip() If two sublists are of different sizes, python's zip will truncate the output to be the smaller of the two. safeZip throws an exception if the size of the any sublist is different from the rest. """ if enforceLength is True: length = len(listOfLists[0]) assert(all([length == len(subList) for subList in listOfLists])) try: zipFunc = itertools.izip_longest # Python 2.x except AttributeError: zipFunc = itertools.zip_longest # Python 3.x return zipFunc(*listOfLists)
Example #2
Source File: logcat_monitor_test.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def assertIterEqual(self, expected_iter, actual_iter): for expected, actual in itertools.izip_longest(expected_iter, actual_iter): self.assertIsNotNone( expected, msg='actual has unexpected elements starting with %s' % str(actual)) self.assertIsNotNone( actual, msg='actual is missing elements starting with %s' % str(expected)) self.assertEqual(actual.group('proc_id'), expected[0]) self.assertEqual(actual.group('thread_id'), expected[1]) self.assertEqual(actual.group('log_level'), expected[2]) self.assertEqual(actual.group('component'), expected[3]) self.assertEqual(actual.group('message'), expected[4]) with self.assertRaises(StopIteration): next(actual_iter) with self.assertRaises(StopIteration): next(expected_iter)
Example #3
Source File: new_config.py From slpkg with GNU General Public License v3.0 | 6 votes |
def merge(self, n): """Merge new file into old """ if os.path.isfile(n[:-4]): old = self.read_file(n[:-4]).splitlines() if os.path.isfile(n): new = self.read_file(n).splitlines() with open(n[:-4], "w") as out: for l1, l2 in itertools.izip_longest(old, new): if l1 is None: l1 = "" if l2 is None: l2 = "" if l1 != l2: out.write(l2 + "\n") else: out.write(l1 + "\n") print("The file {0} merged in file {1}".format( n.split("/")[-1], n[:-4].split("/")[-1]))
Example #4
Source File: docker.py From deimos with Apache License 2.0 | 6 votes |
def run(options, image, command=[], env={}, cpus=None, mems=None, ports=[]): envs = env.items() if isinstance(env, dict) else env pairs = [("-e", "%s=%s" % (k, v)) for k, v in envs] if ports != []: # NB: Forces external call to pre-fetch image port_pairings = list(itertools.izip_longest(ports, inner_ports(image))) log.info("Port pairings (Mesos, Docker) // %r", port_pairings) for allocated, target in port_pairings: if allocated is None: log.warning("Container exposes more ports than were allocated") break options += ["-p", "%d:%d" % (allocated, target or allocated)] argv = ["run"] + options argv += ["-c", str(cpus)] if cpus else [] argv += ["-m", str(mems)] if mems else [] argv += [_ for __ in pairs for _ in __] # This is just flatten argv += [image] + command return docker(*argv)
Example #5
Source File: test_xrange.py From ironpython2 with Apache License 2.0 | 6 votes |
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.izip_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))
Example #6
Source File: conftest.py From k8s with Apache License 2.0 | 6 votes |
def _add_argument_diff(actual, expected, indent=0, acc=None): first = False if not acc: acc = [] first = True if type(actual) != type(expected): acc.append("{}{!r} {} {!r}".format(" " * indent * 2, actual, "==" if actual == expected else "!=", expected)) elif isinstance(actual, dict): for k in set(actual.keys() + expected.keys()): acc.append("{}{}:".format(" " * indent * 2, k)) a = actual.get(k) e = expected.get(k) if a != e: _add_argument_diff(a, e, indent + 1, acc) elif isinstance(actual, list): for a, e in itertools.izip_longest(actual, expected): acc.append("{}-".format(" " * indent * 2)) if a != e: _add_argument_diff(a, e, indent + 1, acc) else: acc.append("{}{!r} {} {!r}".format(" " * indent * 2, actual, "==" if actual == expected else "!=", expected)) if first: return "\n".join(acc)
Example #7
Source File: list.py From vnpy_crypto with MIT License | 6 votes |
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
Example #8
Source File: __init__.py From picklable-itertools with MIT License | 6 votes |
def test_izip_longest(): yield (verify_same, izip_longest, _zip_longest, None, [], []) yield (verify_same, izip_longest, _zip_longest, None, [], [5, 4]) yield (verify_same, izip_longest, _zip_longest, None, [2], [5, 4]) yield (verify_same, izip_longest, _zip_longest, None, [7, 9], [5, 4]) yield (verify_same, izip_longest, _zip_longest, None, [7, 9], [4], [2, 9, 3]) yield (verify_same, izip_longest, _zip_longest, None, [7, 9], [4], []) yield (verify_same, izip_longest, _zip_longest, None, [7], [4], [], [5, 9]) yield (verify_same, partial(izip_longest, fillvalue=-1), partial(_zip_longest, fillvalue=-1), None, [7], [4], [], [5, 9]) yield (verify_pickle, izip_longest, _zip_longest, 3, 2, [7, 9, 8], [1, 2]) yield (verify_pickle, izip_longest, _zip_longest, 3, 1, [7, 9, 8], [1, 2])
Example #9
Source File: list.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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
Example #10
Source File: cli.py From pychievements with MIT License | 6 votes |
def print_goal(goal, achieved=False, level=None, indent=2): """ Print a goals description with its icon. Achieved (True/False) will choose the correct icon from the goal. If a level is specified, a tracker line will be added under the icon showing the current level out of the required level for the goal. If level is > the required level, achieved will be set to true. """ from clint.textui import puts from clint.textui import indent as _indent from clint.textui.cols import columns, console_width if level is not None and level >= goal['level']: achieved = True icon = (goal['icon'].achieved() if achieved else goal['icon'].unachieved()).split('\n') maxiw = max([len(str(_)) for _ in icon]) descw = console_width({})-maxiw-(indent + 4) desc = '{0}\n{1}\n\n{2}'.format(goal['name'], '-'*len(goal['name']), columns([goal['description'], descw])).split('\n') if level is not None: if level > goal['level']: level = goal['level'] maxitw = max([len(_) for _ in icon]) icon.append(("%d/%d" % (level, goal['level'])).center(maxitw)) with _indent(indent): for i, d in _zip_longest(icon, desc): puts("{1:{0}} {2}".format(maxiw, str(i) if i is not None else "", d.strip() if d is not None else ""))
Example #11
Source File: reference.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def value(self, value): value_parts = value.split('.') # If we have too many children, cut the list down to size. # pylint: disable=attribute-defined-outside-init self._children = self._children[:len(value_parts)] # Update child nodes. for child, value_part in itertools.izip_longest( self._children, value_parts): if child: # Modify existing children. This helps preserve comments and spaces. child.children[-1].value = value_part else: # Add children as needed. token_snippets = [ snippet.TokenSnippet.Create(token.DOT, '.'), snippet.TokenSnippet.Create(token.NAME, value_part), ] self._children.append(snippet.Symbol(symbol.trailer, token_snippets))
Example #12
Source File: import_statement.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def value(self, value): value_parts = value.split('.') for value_part in value_parts: if keyword.iskeyword(value_part): raise ValueError('%s is a reserved keyword.' % value_part) # If we have too many children, cut the list down to size. # pylint: disable=attribute-defined-outside-init self._children = self._children[:len(value_parts)*2-1] # Update child nodes. for child, value_part in itertools.izip_longest( self._children[::2], value_parts): if child: # Modify existing children. This helps preserve comments and spaces. child.value = value_part else: # Add children as needed. self._children.append(snippet.TokenSnippet.Create(token.DOT, '.')) self._children.append( snippet.TokenSnippet.Create(token.NAME, value_part))
Example #13
Source File: logcat_monitor_test.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def assertIterEqual(self, expected_iter, actual_iter): for expected, actual in itertools.izip_longest(expected_iter, actual_iter): self.assertIsNotNone( expected, msg='actual has unexpected elements starting with %s' % str(actual)) self.assertIsNotNone( actual, msg='actual is missing elements starting with %s' % str(expected)) self.assertEqual(actual.group('proc_id'), expected[0]) self.assertEqual(actual.group('thread_id'), expected[1]) self.assertEqual(actual.group('log_level'), expected[2]) self.assertEqual(actual.group('component'), expected[3]) self.assertEqual(actual.group('message'), expected[4]) with self.assertRaises(StopIteration): next(actual_iter) with self.assertRaises(StopIteration): next(expected_iter)
Example #14
Source File: reference.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def value(self, value): value_parts = value.split('.') # If we have too many children, cut the list down to size. # pylint: disable=attribute-defined-outside-init self._children = self._children[:len(value_parts)] # Update child nodes. for child, value_part in itertools.izip_longest( self._children, value_parts): if child: # Modify existing children. This helps preserve comments and spaces. child.children[-1].value = value_part else: # Add children as needed. token_snippets = [ snippet.TokenSnippet.Create(token.DOT, '.'), snippet.TokenSnippet.Create(token.NAME, value_part), ] self._children.append(snippet.Symbol(symbol.trailer, token_snippets))
Example #15
Source File: logcat_monitor_test.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def assertIterEqual(self, expected_iter, actual_iter): for expected, actual in itertools.izip_longest(expected_iter, actual_iter): self.assertIsNotNone( expected, msg='actual has unexpected elements starting with %s' % str(actual)) self.assertIsNotNone( actual, msg='actual is missing elements starting with %s' % str(expected)) self.assertEqual(actual.group('proc_id'), expected[0]) self.assertEqual(actual.group('thread_id'), expected[1]) self.assertEqual(actual.group('log_level'), expected[2]) self.assertEqual(actual.group('component'), expected[3]) self.assertEqual(actual.group('message'), expected[4]) with self.assertRaises(StopIteration): next(actual_iter) with self.assertRaises(StopIteration): next(expected_iter)
Example #16
Source File: reference.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def value(self, value): value_parts = value.split('.') # If we have too many children, cut the list down to size. # pylint: disable=attribute-defined-outside-init self._children = self._children[:len(value_parts)] # Update child nodes. for child, value_part in itertools.izip_longest( self._children, value_parts): if child: # Modify existing children. This helps preserve comments and spaces. child.children[-1].value = value_part else: # Add children as needed. token_snippets = [ snippet.TokenSnippet.Create(token.DOT, '.'), snippet.TokenSnippet.Create(token.NAME, value_part), ] self._children.append(snippet.Symbol(symbol.trailer, token_snippets))
Example #17
Source File: import_statement.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
def value(self, value): value_parts = value.split('.') for value_part in value_parts: if keyword.iskeyword(value_part): raise ValueError('%s is a reserved keyword.' % value_part) # If we have too many children, cut the list down to size. # pylint: disable=attribute-defined-outside-init self._children = self._children[:len(value_parts)*2-1] # Update child nodes. for child, value_part in itertools.izip_longest( self._children[::2], value_parts): if child: # Modify existing children. This helps preserve comments and spaces. child.value = value_part else: # Add children as needed. self._children.append(snippet.TokenSnippet.Create(token.DOT, '.')) self._children.append( snippet.TokenSnippet.Create(token.NAME, value_part))
Example #18
Source File: list.py From anpr with Creative Commons Attribution 4.0 International | 6 votes |
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
Example #19
Source File: test_xrange.py From BinderFilter with MIT License | 6 votes |
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.izip_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))
Example #20
Source File: test_xrange.py From oss-ftp with MIT License | 6 votes |
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.izip_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))
Example #21
Source File: obj.py From pg with MIT License | 5 votes |
def save_obj(self, path): lines = [] v = sorted(set(self.positions)) vt = sorted(set(self.uvs)) vn = sorted(set(self.normals)) lu_v = dict((x, i + 1) for i, x in enumerate(v)) lu_vt = dict((x, i + 1) for i, x in enumerate(vt)) lu_vn = dict((x, i + 1) for i, x in enumerate(vn)) for x in v: lines.append('v %f %f %f' % x) for x in vt: lines.append('vt %f %f' % x) for x in vn: lines.append('vn %f %f %f' % x) f = [] for v, vt, vn in izip_longest(self.positions, self.uvs, self.normals): v = lu_v.get(v, '') vt = lu_vt.get(vt, '') vn = lu_vn.get(vn, '') f.append(('%s/%s/%s' % (v, vt, vn)).strip('/')) if len(f) == 3: lines.append('f %s' % ' '.join(f)) f = [] data = '\n'.join(lines) with open(path, 'w') as fp: fp.write(data)
Example #22
Source File: writers.py From plugin.program.openwizard with GNU General Public License v3.0 | 5 votes |
def write_xbm(matrix, version, out, scale=1, border=None, name='img'): """\ Serializes the matrix as `XBM <https://en.wikipedia.org/wiki/X_BitMap>`_ image. :param matrix: The matrix to serialize. :param int version: The (Micro) QR code version :param out: Filename or a file-like object supporting to write text data. :param scale: Indicates the size of a single module (default: 1 which corresponds to 1 x 1 in the provided unit per module). :param int border: Integer indicating the size of the quiet zone. If set to ``None`` (default), the recommended border size will be used (``4`` for QR Codes, ``2`` for a Micro QR Codes). :param name: Prefix for the variable names. Default: "img". The prefix is used to construct the variable names: ```#define <prefix>_width``` ```static unsigned char <prefix>_bits[]``` """ row_iter = matrix_iter(matrix, version, scale, border) border = get_border(version, border) width, height = get_symbol_size(version, scale=scale, border=border) with writable(out, 'wt') as f: write = f.write write('#define {0}_width {1}\n' '#define {0}_height {2}\n' 'static unsigned char {0}_bits[] = {{\n'.format(name, width, height)) for i, row in enumerate(row_iter, start=1): iter_ = zip_longest(*[iter(row)] * 8, fillvalue=0x0) # Reverse bits since XBM uses little endian bits = ['0x{0:02x}'.format(reduce(lambda x, y: (x << 1) + y, bits[::-1])) for bits in iter_] write(' ') write(', '.join(bits)) write(',\n' if i < height else '\n') write('};\n')
Example #23
Source File: results.py From python_api with MIT License | 5 votes |
def _items(self): if self._items_list is None: self._items_list = chain(*[zip_longest([], value, fillvalue=key) for key, value in self.response().items() if type(value) in (list, tuple)]) return self._items_list
Example #24
Source File: recfunctions.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def izip_records(seqarrays, fill_value=None, flatten=True): """ Returns an iterator of concatenated items from a sequence of arrays. Parameters ---------- seqarrays : sequence of arrays Sequence of arrays. fill_value : {None, integer} Value used to pad shorter iterables. flatten : {True, False}, Whether to """ # Should we flatten the items, or just use a nested approach if flatten: zipfunc = _izip_fields_flat else: zipfunc = _izip_fields if sys.version_info[0] >= 3: zip_longest = itertools.zip_longest else: zip_longest = itertools.izip_longest for tup in zip_longest(*seqarrays, fillvalue=fill_value): yield tuple(zipfunc(tup))
Example #25
Source File: sound.py From morse-talk with GNU General Public License v2.0 | 5 votes |
def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return zip_longest(fillvalue=fillvalue, *args)
Example #26
Source File: utils.py From next-prediction with Apache License 2.0 | 5 votes |
def grouper(lst, num): args = [iter(lst)]*num out = itertools.izip_longest(*args, fillvalue=None) out = list(out) return out
Example #27
Source File: encoder.py From PiNumberStation with MIT License | 5 votes |
def chunk(self, str, size): seq = list(str) x = [iter(seq)] * size l = zip_longest(*x, fillvalue='0') return [''.join(tups) for tups in l]
Example #28
Source File: train.py From DoNotSnap with GNU General Public License v3.0 | 5 votes |
def get_images(limit=0): positive = read_file('positive.txt', limit / 2 if limit else 0) negative = read_file('negative.txt', limit / 2 if limit else 0) for p, n in izip_longest(positive, negative): if p is not None: yield (1, p) if n is not None: yield (0, n)
Example #29
Source File: dexcom.py From openaps with MIT License | 5 votes |
def main (self, args, app): params = self.get_params(args) self.dateSelector = params.get('date')[0] self.comparison = self.fill.itertool(app, **params) records = self.fill.records iter_glucose = self.get_glucose_data(params, args) iter_sensor = self.get_sensor_data(params, args) template = dict(device="openaps://{}/{}".format(socket.gethostname(),self.device.name), type='sgv') for egv, raw in itertools.izip_longest(iter_glucose, iter_sensor): item = dict(**template) if egv: # trend = getattr(egv, 'full_trend', self.arrow_to_trend(egv.trend_arrow)) trend = self.arrow_to_trend(egv.trend_arrow) item.update(sgv=egv.glucose, direction=self.trend_to_direction(trend, egv.trend_arrow), **egv.to_dict( )) # https://github.com/nightscout/cgm-remote-monitor/blob/dev/lib/mqtt.js#L233-L296 if raw: delta = abs((raw.display_time - egv.display_time).total_seconds( )) if delta < args.threshold: item.update(filtered=raw.filtered, unfiltered=raw.unfiltered, rssi=raw.rssi) else: # create two items instead of one # if raw: self.adjust_dates(item) records.append(item) item = dict(sgv=-1, **template) item.update(**raw.to_dict( )) self.adjust_dates(item) records.append(item) # item = dict( ) elif raw: item.update(type='sgv', sgv=-1, **raw.to_dict( )) self.adjust_dates(item) records.append(item) return records
Example #30
Source File: recfunctions.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def izip_records(seqarrays, fill_value=None, flatten=True): """ Returns an iterator of concatenated items from a sequence of arrays. Parameters ---------- seqarrays : sequence of arrays Sequence of arrays. fill_value : {None, integer} Value used to pad shorter iterables. flatten : {True, False}, Whether to """ # Should we flatten the items, or just use a nested approach if flatten: zipfunc = _izip_fields_flat else: zipfunc = _izip_fields if sys.version_info[0] >= 3: zip_longest = itertools.zip_longest else: zip_longest = itertools.izip_longest for tup in zip_longest(*seqarrays, fillvalue=fill_value): yield tuple(zipfunc(tup))