Skip to main content

Extends zip() and itertools.zip_longest() to generate named tuples.

Project description

license

This package implements namedzip and namedzip_longest, which extend zip and itertools.zip_longest respectively to generate named tuples using collections.namedtuple.

Installation

$ pip install namedzip

Usage examples

>>> from namedzip import namedzip, namedzip_longest

namedzip and namedzip_longest can either be used with iterable positional arguments, like the functions which they extend, to return generator objects:

>>> iterables = (["A", "B", "C"], [1, 2, 3])
>>> pairs = namedzip(*iterables, typename="Pair", field_names=("letter", "number"))
>>> for pair in pairs:
...     print(pair)
...
Pair(letter='A', number=1)
Pair(letter='B', number=2)
Pair(letter='C', number=3)
>>>
>>> iterables = (["A", "B"], [1, 2, 3])
>>> pairs = namedzip_longest(*iterables, typename="Pair", field_names=("letter", "number"), defaults=("X", 99))
>>> for pair in pairs:
...     print(pair)
...
Pair(letter='A', number=1)
Pair(letter='B', number=2)
Pair(letter='X', number=3)
>>>

Or without positional arguments to return reusable function objects:

>>> zip_pairs = namedzip(typename="Pair", field_names=("letter", "number"))
>>> pairs = zip_pairs(["A", "B", "C"], [1, 2, 3])
>>> for pair in pairs:
...     print(pair)
...
Pair(letter='A', number=1)
Pair(letter='B', number=2)
Pair(letter='C', number=3)
>>>
>>> zip_pairs = namedzip_longest(typename="Pair", field_names=("letter", "number"), defaults=("X", 99))
>>> pairs = zip_pairs(["A", "B", "C"], [1, 2])
>>> for pair in pairs:
...     print(pair)
...
Pair(letter='A', number=1)
Pair(letter='B', number=2)
Pair(letter='C', number=99)
>>>

Development setup

Clone repo and create virtual environment:

$ git clone https://github.com/erberlin/namedzip.git
$ cd namedzip
$ python -m venv venv

Activate virtual environment on Windows:

> venv\Scripts\activate

Activate virtual environment on OS X & Linux:

$ source venv/bin/activate

Install required packages:

$ pip install requirements.txt

Run test suite:

$ pytest -v

Meta

Erik R Berlin – erberlin.dev@gmail.com

Distributed under the MIT license. See LICENSE for more information.

https://github.com/erberlin/namedzip

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

namedzip-1.0.1.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

namedzip-1.0.1-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page