Skip to main content

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

Project description

license pyversions wheel build docs

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 interfaces 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)
>>>

Documentation

Additional documentation is available at https://namedzip.readthedocs.io/en/latest/.

Development setup

Clone repo:

$ git clone https://github.com/erberlin/namedzip.git
$ cd namedzip

Create and activate virtual environment on Windows:

> python -m venv venv
> venv\Scripts\activate

Create and activate virtual environment on OS X & Linux:

$ python3 -m venv venv
$ source venv/bin/activate

Install development 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.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

namedzip-1.0.3-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file namedzip-1.0.3.tar.gz.

File metadata

  • Download URL: namedzip-1.0.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for namedzip-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7fc10b068269f4d855e74c2c6c5362ddc2ba8def17ad0ff00a3de077fd672bf3
MD5 0adf57e4e1340e202fe617149232c0a5
BLAKE2b-256 0131357324f5bf6cf3163b043356b184d4d43d26e47269ede06e51870d276f8b

See more details on using hashes here.

File details

Details for the file namedzip-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: namedzip-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for namedzip-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dbc889b691e360a3667825359d48d40be27212cf210e51a663755e9e16cfa916
MD5 976ac05d86c9efcd61c8faa3b825fe74
BLAKE2b-256 f112729b56cdf581a76bd2a2045cd2623907ee80baa87ad8f529884e7260229e

See more details on using hashes here.

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