Python itertools.zip_longest() Examples

The following are 30 code examples for showing how to use itertools.zip_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: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 6 votes vote down vote up
def loose_version_compare(a, b):
    for i, j in zip_longest(a.version, b.version, fillvalue=''):
        if type(i) != type(j):
            i = str(i)
            j = str(j)
        if i == j:
            continue
        elif i < j:
            return -1
        else:  # i > j
            return 1

    #Longer version strings with equal prefixes are equal, but if one version string is longer than it is greater
    aLen = len(a.version)
    bLen = len(b.version)
    if aLen == bLen:
        return 0
    elif aLen < bLen:
        return -1
    else:
        return 1 
Example 3
Project: mars   Author: mars-project   File: core.py    License: Apache License 2.0 6 votes vote down vote up
def base_equal(self, ob1, ob2):
        if type(ob1) != type(ob2):
            return False

        def cmp(obj1, obj2):
            if isinstance(obj1, np.ndarray):
                return np.array_equal(obj1, obj2)
            elif isinstance(obj1, Iterable) and \
                    not isinstance(obj1, str) and \
                    isinstance(obj2, Iterable) and \
                    not isinstance(obj2, str):
                return all(cmp(it1, it2) for it1, it2 in itertools.zip_longest(obj1, obj2))
            elif hasattr(obj1, 'key') and hasattr(obj2, 'key'):
                return obj1.key == obj2.key
            elif isinstance(obj1, ReferenceType) and isinstance(obj2, ReferenceType):
                return cmp(obj1(), obj2())
            else:
                return obj1 == obj2

        for slot in ob1.__slots__:
            if not cmp(getattr(ob1, slot, None), getattr(ob2, slot, None)):
                return False

        return True 
Example 4
Project: python-netsurv   Author: sofia-netsurv   File: node_classes.py    License: MIT License 6 votes vote down vote up
def _format_args(args, defaults=None, annotations=None):
    values = []
    if args is None:
        return ""
    if annotations is None:
        annotations = []
    if defaults is not None:
        default_offset = len(args) - len(defaults)
    packed = itertools.zip_longest(args, annotations)
    for i, (arg, annotation) in enumerate(packed):
        if isinstance(arg, Tuple):
            values.append("(%s)" % _format_args(arg.elts))
        else:
            argname = arg.name
            if annotation is not None:
                argname += ":" + annotation.as_string()
            values.append(argname)

            if defaults is not None and i >= default_offset:
                if defaults[i - default_offset] is not None:
                    values[-1] += "=" + defaults[i - default_offset].as_string()
    return ", ".join(values) 
Example 5
Project: python-netsurv   Author: sofia-netsurv   File: node_classes.py    License: MIT License 6 votes vote down vote up
def _format_args(args, defaults=None, annotations=None):
    values = []
    if args is None:
        return ""
    if annotations is None:
        annotations = []
    if defaults is not None:
        default_offset = len(args) - len(defaults)
    packed = itertools.zip_longest(args, annotations)
    for i, (arg, annotation) in enumerate(packed):
        if isinstance(arg, Tuple):
            values.append("(%s)" % _format_args(arg.elts))
        else:
            argname = arg.name
            if annotation is not None:
                argname += ":" + annotation.as_string()
            values.append(argname)

            if defaults is not None and i >= default_offset:
                if defaults[i - default_offset] is not None:
                    values[-1] += "=" + defaults[i - default_offset].as_string()
    return ", ".join(values) 
Example 6
Project: torf   Author: rndusr   File: _utils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __add__(self, other):
        if isinstance(other, type(self)):
            other_tiers = other._tiers
        elif isinstance(other, collections.abc.Iterable):
            other_tiers = other
        new_tiers = []
        for tier1,x in itertools.zip_longest(self._tiers, other_tiers):
            if tier1 is None:
                tier1 = []
            if isinstance(x, str) and len(x) > 1:
                new_tier = tier1 + [x]
            elif isinstance(x, collections.abc.Iterable):
                new_tier = tier1 + list(x)
            elif x is not None:
                return NotImplemented
            else:
                new_tier = tier1
            new_tiers.append(new_tier)
        return type(self)(new_tiers, callback=self._callback) 
Example 7
Project: nevergrad   Author: facebookresearch   File: test_experiments.py    License: MIT License 6 votes vote down vote up
def check_maker(maker: tp.Callable[[], tp.Iterator[experiments.Experiment]]) -> None:
    generators = [maker() for _ in range(2)]
    # check 1 sample
    sample = next(maker())
    assert isinstance(sample, experiments.Experiment)
    # check names, coherence and non-randomness
    for k, (elem1, elem2) in enumerate(itertools.zip_longest(*generators)):
        assert not elem1.is_incoherent, f"Incoherent settings should be filtered out from generator:\n{elem1}"
        try:
            assert elem1 == elem2  # much faster but lacks explicit message
        except AssertionError:
            testing.printed_assert_equal(
                elem1.get_description(),
                elem2.get_description(),
                err_msg=f"Two paths on the generator differed (see element #{k})\n"
                "Generators need to be deterministic in order to split the workload!",
            ) 
Example 8
Project: fairseq   Author: pytorch   File: iterators.py    License: MIT License 6 votes vote down vote up
def __init__(self, iterable, num_shards, shard_id, fill_value=None):
        if shard_id < 0 or shard_id >= num_shards:
            raise ValueError('shard_id must be between 0 and num_shards')
        sharded_len = int(math.ceil(len(iterable) / float(num_shards)))
        itr = map(
            operator.itemgetter(1),
            itertools.zip_longest(
                range(sharded_len),
                itertools.islice(iterable, shard_id, len(iterable), num_shards),
                fillvalue=fill_value,
            ),
        )
        super().__init__(
            itr,
            start=int(math.ceil(getattr(iterable, 'n', 0) / float(num_shards))),
            total=sharded_len,
        ) 
Example 9
Project: chainer-compiler   Author: pfnet-research   File: vevaluator.py    License: MIT License 6 votes vote down vote up
def veval_ast_arguments(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph', context : 'functions.VEvalContext' = None):
    assert(isinstance(astc.nast, gast.gast.arguments))
    lineprop = utils.LineProperty(astc.lineno, astc.filename)

    ret = functions.FunctionArgCollection()

    argspec = inspect.FullArgSpec(astc.nast.args, astc.nast.vararg, astc.nast.kwarg,
                                  astc.nast.defaults, astc.nast.kwonlyargs, astc.nast.kw_defaults, None)

    assert not argspec.kwonlyargs, "Keyword only args are not supported"
    assert not argspec.varargs, "Varaibale arguments *args is not supported"
    assert not argspec.varkw, "Variable keywords **kwargs is not supported"

    defaults = [veval_ast(astc.c(default), local_field, graph, context) for default in argspec.defaults]
    arg_list = []
    for k, v in itertools.zip_longest(reversed(argspec.args), defaults):
        arg_list.append((k.id, v))

    # reverse the list
    for k, v in reversed(arg_list):
        ret.add_arg(k, v)

    return ret 
Example 10
Project: pinax-documents   Author: pinax   File: models.py    License: MIT License 5 votes vote down vote up
def shared_parent(self):
        """
        Returns the folder object that is the shared parent (the root of
        a shared folder hierarchy) or None if there is no shared parent.
        """
        root = self
        a, b = itertools.tee(reversed(self.breadcrumbs()))
        next(b, None)
        for folder, parent in itertools.zip_longest(a, b):
            if folder.shared:
                root = folder
            if parent is None or not parent.shared:
                break
        return root 
Example 11
Project: L3C-PyTorch   Author: fab-jul   File: aligned_printer.py    License: GNU General Public License v3.0 5 votes vote down vote up
def append(self, *row):
        self.rows.append(row)
        self.maxs = [max(max_cur, len(row_entry))
                     for max_cur, row_entry in
                     itertools.zip_longest(self.maxs, row, fillvalue=0)] 
Example 12
Project: hacking-tools   Author: girishramnani   File: test_fastdivmod.py    License: MIT License 5 votes vote down vote up
def runner(x, base, chunk):
    for i, j in itertools.zip_longest(divmod_iter_chunking(x, base, chunk), divmod_iter_basic(x, base)):
        if i is None:
            print("phooey")
        else:
            assert i == j 
Example 13
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 14
Project: rets   Author: opendoor-labs   File: parse.py    License: MIT License 5 votes vote down vote up
def _parse_data(elem: etree.Element) -> Iterable[dict]:
    """
    Parses a generic container element enclosing a single COLUMNS and multiple DATA elems, and
    returns a generator of dicts with keys given by the COLUMNS elem and values given by each
    DATA elem. The container elem may optionally contain a DELIMITER elem to define the delimiter
    used, otherwise a default of '\t' is assumed.

    <RETS ReplyCode="0" ReplyText="Success">
        <DELIMITER value="09"/>
        <COLUMNS>	LIST_87	LIST_105	LIST_1	</COLUMNS>
        <DATA>	2016-12-01T00:08:10	5489015	20160824051756837742000000	</DATA>
        <DATA>	2016-12-01T00:10:02	5497756	20160915055426038684000000	</DATA>
        <DATA>	2016-12-01T00:10:26	5528935	20161123230848928777000000	</DATA>
        <DATA>	2016-12-01T00:10:52	5528955	20161123234916869427000000	</DATA>
        <DATA>	2016-12-01T00:14:31	5530021	20161127221848669500000000	</DATA>
    </RETS>
    """
    delimiter = _parse_delimiter(elem)

    columns_elem = _find_or_raise(elem, 'COLUMNS')
    columns = _parse_data_line(columns_elem, delimiter)

    data_elems = elem.findall('DATA')

    return (OrderedDict(zip_longest(columns, _parse_data_line(data, delimiter)))
            for data in data_elems) 
Example 15
Project: Machine-Translation   Author: foamliu   File: data_gen.py    License: Apache License 2.0 5 votes vote down vote up
def zeroPadding(l, fillvalue=PAD_token):
    return list(itertools.zip_longest(*l, fillvalue=fillvalue)) 
Example 16
Project: bot   Author: python-discord   File: modlog.py    License: MIT License 5 votes vote down vote up
def upload_log(
        self,
        messages: t.Iterable[discord.Message],
        actor_id: int,
        attachments: t.Iterable[t.List[str]] = None
    ) -> str:
        """Upload message logs to the database and return a URL to a page for viewing the logs."""
        if attachments is None:
            attachments = []

        response = await self.bot.api_client.post(
            'bot/deleted-messages',
            json={
                'actor': actor_id,
                'creation': datetime.utcnow().isoformat(),
                'deletedmessage_set': [
                    {
                        'id': message.id,
                        'author': message.author.id,
                        'channel_id': message.channel.id,
                        'content': message.content,
                        'embeds': [embed.to_dict() for embed in message.embeds],
                        'attachments': attachment,
                    }
                    for message, attachment in zip_longest(messages, attachments, fillvalue=[])
                ]
            }
        )

        return f"{URLs.site_logs_view}/{response['id']}" 
Example 17
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 18
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 19
Project: geoopt   Author: geoopt   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def broadcast_shapes(*shapes: Tuple[int]) -> Tuple[int]:
    """Apply numpy broadcasting rules to shapes."""
    result = []
    for dims in itertools.zip_longest(*map(reversed, shapes), fillvalue=1):
        dim: int = 1
        for d in dims:
            if dim != 1 and d != 1 and d != dim:
                raise ValueError("Shapes can't be broadcasted")
            elif d > dim:
                dim = d
        result.append(dim)
    return tuple(reversed(result)) 
Example 20
Project: geoopt   Author: geoopt   File: scaled.py    License: Apache License 2.0 5 votes vote down vote up
def rescale(function, scaling_info):
    if scaling_info is ScalingInfo.NotCompatible:

        @functools.wraps(functools)
        def stub(self, *args, **kwargs):
            raise NotImplementedError(
                "Scaled version of '{}' is not available".format(function.__name__)
            )

        return stub
    signature = inspect.signature(function)

    @functools.wraps(function)
    def rescaled_function(self, *args, **kwargs):
        params = signature.bind(self.base, *args, **kwargs)
        params.apply_defaults()
        arguments = params.arguments
        for k, power in scaling_info.kwargs.items():
            arguments[k] = rescale_value(arguments[k], self.scale, power)
        params = params.__class__(signature, arguments)
        results = function(*params.args, **params.kwargs)
        if not scaling_info.results:
            # do nothing
            return results
        wrapped_results = []
        is_tuple = isinstance(results, tuple)
        results = geoopt.utils.make_tuple(results)
        for i, (res, power) in enumerate(
            itertools.zip_longest(results, scaling_info.results, fillvalue=0)
        ):
            wrapped_results.append(rescale_value(res, self.scale, power))
        if not is_tuple:
            wrapped_results = wrapped_results[0]
        else:
            wrapped_results = results.__class__(wrapped_results)
        return wrapped_results

    return rescaled_function 
Example 21
Project: coursys   Author: sfu-fas   File: photos.py    License: GNU General Public License v3.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 itertools.zip_longest(fillvalue=fillvalue, *args) 
Example 22
Project: coursys   Author: sfu-fas   File: tasks.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _grouper(iterable, n):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    groups = itertools.zip_longest(fillvalue=None, *args)
    return ((v for v in grp if v is not None) for grp in groups) 
Example 23
Project: flowlogs-reader   Author: obsrvbl   File: test_main.py    License: Apache License 2.0 5 votes vote down vote up
def test_main_print(self, mock_out, mock_reader):
        mock_out.stdout = io.BytesIO()
        mock_reader.return_value = SAMPLE_RECORDS
        main(['mygroup'])
        for call, record in zip_longest(mock_out.mock_calls, SAMPLE_INPUT):
            __, args, kwargs = call
            line = args[0]
            self.assertEqual(line, record) 
Example 24
Project: flowlogs-reader   Author: obsrvbl   File: test_main.py    License: Apache License 2.0 5 votes vote down vote up
def test_main_print_count(self, mock_out, mock_reader):
        mock_out.stdout = io.BytesIO()
        mock_reader.return_value = SAMPLE_RECORDS

        with self.assertRaises(ValueError):
            main(['mygroup', 'print', 'two'])

        with self.assertRaises(RuntimeError):
            main(['mygroup', 'print', '2', '3'])

        main(['mygroup', 'print', '2'])
        for call, record in zip_longest(mock_out.mock_calls, SAMPLE_INPUT[:2]):
            __, args, kwargs = call
            line = args[0]
            self.assertEqual(line, record) 
Example 25
Project: flowlogs-reader   Author: obsrvbl   File: test_main.py    License: Apache License 2.0 5 votes vote down vote up
def test_main_findip(self, mock_out, mock_reader):
        mock_out.stdout = io.BytesIO()
        mock_reader.return_value = SAMPLE_RECORDS
        main(['mygroup', 'findip', '198.51.100.2'])

        expected_result = [SAMPLE_INPUT[2]]
        for call, record in zip_longest(mock_out.mock_calls, expected_result):
            __, args, kwargs = call
            line = args[0]
            self.assertEqual(line, record) 
Example 26
Project: flowlogs-reader   Author: obsrvbl   File: test_main.py    License: Apache License 2.0 5 votes vote down vote up
def test_main_bad_action(self, mock_out, mock_reader):
        mock_out.stdout = io.BytesIO()
        mock_reader.return_value = SAMPLE_RECORDS
        main(['mygroup', '__'])

        expected_result = [
            'unknown action: __',
            'known actions: {}'.format(', '.join(actions)),
        ]
        for call, result in zip_longest(mock_out.mock_calls, expected_result):
            __, args, kwargs = call
            line = args[0]
            self.assertEqual(line, result) 
Example 27
Project: flowlogs-reader   Author: obsrvbl   File: test_main.py    License: Apache License 2.0 5 votes vote down vote up
def test_main_missing_arn(self, mock_out, mock_reader):
        mock_out.stdout = io.BytesIO()
        mock_reader.return_value = SAMPLE_RECORDS
        main(['--external-id', 'uuid4', 'mygroup'])

        expected_result = [
            'must give a --role-arn if an --external-id is given',
        ]
        for call, result in zip_longest(mock_out.mock_calls, expected_result):
            __, args, kwargs = call
            line = args[0]
            self.assertEqual(line, result) 
Example 28
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 29
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 30
Project: dephell   Author: dephell   File: _downloads.py    License: MIT License 5 votes vote down vote up
def make_chart(values: Sequence[int], group: int = None, ticks: str = '_▁▂▃▄▅▆▇█') -> str:
    peek = max(values)
    if peek == 0:
        chart = ticks[-1] * len(values)
    else:
        chart = ''
        for value in values:
            index = round((len(ticks) - 1) * value / peek)
            chart += ticks[int(index)]
    if group:
        chunks = map(''.join, zip_longest(*[iter(chart)] * group, fillvalue=' '))
        chart = ' '.join(chunks).strip()
    return chart