Python itertools.izip_longest() Examples

The following are 30 code examples for showing how to use itertools.izip_longest(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module itertools , or try the search function .

Example 1
Project: praatIO   Author: timmahrt   File: utils.py    License: MIT License 7 votes vote down vote up
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
Project: ironpython2   Author: IronLanguages   File: test_xrange.py    License: Apache License 2.0 6 votes vote down vote up
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 3
Project: slpkg   Author: dslackw   File: new_config.py    License: GNU General Public License v3.0 6 votes vote down vote up
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
Project: deimos   Author: mesosphere-backup   File: docker.py    License: Apache License 2.0 6 votes vote down vote up
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
Project: vnpy_crypto   Author: birforce   File: list.py    License: MIT License 6 votes vote down vote up
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 6
Project: BinderFilter   Author: dxwu   File: test_xrange.py    License: MIT License 6 votes vote down vote up
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 7
Project: oss-ftp   Author: aliyun   File: test_xrange.py    License: MIT License 6 votes vote down vote up
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 8
Project: anpr   Author: italia   File: list.py    License: Creative Commons Attribution 4.0 International 6 votes vote down vote up
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 9
Project: Safejumper-for-Desktop   Author: proxysh   File: list.py    License: GNU General Public License v2.0 6 votes vote down vote up
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
Project: picklable-itertools   Author: mila-iqia   File: __init__.py    License: MIT License 6 votes vote down vote up
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 11
Project: k8s   Author: fiaas   File: conftest.py    License: Apache License 2.0 6 votes vote down vote up
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 12
Project: pychievements   Author: PacketPerception   File: cli.py    License: MIT License 6 votes vote down vote up
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 13
Project: Jandroid   Author: FSecureLABS   File: logcat_monitor_test.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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
Project: Jandroid   Author: FSecureLABS   File: reference.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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
Project: Jandroid   Author: FSecureLABS   File: import_statement.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 16
Project: Jandroid   Author: FSecureLABS   File: logcat_monitor_test.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 17
Project: Jandroid   Author: FSecureLABS   File: reference.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 18
Project: Jandroid   Author: FSecureLABS   File: logcat_monitor_test.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 19
Project: Jandroid   Author: FSecureLABS   File: reference.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 20
Project: Jandroid   Author: FSecureLABS   File: import_statement.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 21
Project: hiku   Author: vmagamedov   File: base.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def result_match(result, value, path=None):
    path = [] if path is None else path
    if result is _missing:
        return False, path, result, value
    if isinstance(value, dict):
        for k, v in value.items():
            ok, sp, sr, sv = result_match(result[k], v, path + [k])
            if not ok:
                return ok, sp, sr, sv
    elif isinstance(value, (list, tuple)):
        pairs = zip_longest(result, value, fillvalue=_missing)
        for i, (v1, v2) in enumerate(pairs):
            ok, sp, sr, sv = result_match(v1, v2, path + [i])
            if not ok:
                return ok, sp, sr, sv
    elif result != value:
        return False, path, result, value

    return True, None, None, None 
Example 22
Project: me-ica   Author: ME-ICA   File: advanced_config.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def chunk(self, iterable, n, fillvalue=None):
    "itertools recipe: Collect data into fixed-length chunks or blocks"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args) 
Example 23
Project: Jamais-Vu   Author: CwbhX   File: database_sql.py    License: MIT License 5 votes vote down vote up
def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return (filter(None, values) for values
            in izip_longest(fillvalue=fillvalue, *args)) 
Example 24
Project: honeybee   Author: ladybug-tools   File: resultgrid.py    License: GNU General Public License v3.0 5 votes vote down vote up
def grouper(iterable, n, fillvalue=None):
        args = [iter(iterable)] * n
        return zip_longest(*args, fillvalue=fillvalue) 
Example 25
Project: linter-pylama   Author: AtomLinter   File: utils.py    License: MIT License 5 votes vote down vote up
def pairwise(iterable, default_value):
    """Return pairs of items from `iterable`.

    pairwise([1, 2, 3], default_value=None) -> (1, 2) (2, 3), (3, None)
    """
    a, b = tee(iterable)
    _ = next(b, default_value)
    return zip_longest(a, b, fillvalue=default_value) 
Example 26
Project: recruit   Author: Frank-qlu   File: recfunctions.py    License: Apache License 2.0 5 votes vote down vote up
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 27
Project: recruit   Author: Frank-qlu   File: list.py    License: Apache License 2.0 5 votes vote down vote up
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 28
Project: python-mysql-pool   Author: LuciferJack   File: compiler.py    License: MIT License 5 votes vote down vote up
def resolve_columns(self, row, fields=()):
        values = []
        index_extra_select = len(self.query.extra_select)
        bool_fields = ("BooleanField", "NullBooleanField")
        for value, field in zip_longest(row[index_extra_select:], fields):
            if (field and field.get_internal_type() in bool_fields and
                        value in (0, 1)):
                value = bool(value)
            values.append(value)
        return row[:index_extra_select] + tuple(values) 
Example 29
Project: script.tvguide.fullscreen   Author: primaeval   File: utils.py    License: GNU General Public License v2.0 5 votes vote down vote up
def grouper(n, iterable, fillvalue=None):
    """ grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx """
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

#TODO this is wrong 
Example 30
Project: rucio   Author: rucio   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def grouper(iterable, n, fillvalue=None):
    """ Collect data into fixed-length chunks or blocks """
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(*args, fillvalue=fillvalue)