Extends zip() and itertools.zip_longest() to generate named tuples.
Project description
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.
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
Built Distribution
File details
Details for the file namedzip-1.0.2.tar.gz
.
File metadata
- Download URL: namedzip-1.0.2.tar.gz
- Upload date:
- Size: 3.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | bea0acf388c263eec407e7cdeb5e7ffa2a8706b3d6d23192d4d30bfe29a6d568 |
|
MD5 | e3175eed8b8d57a4b068f95881caebc9 |
|
BLAKE2b-256 | 975f4c78356e9dc06ee3ab5218d6dfd8cd93defe9387b9033a4de78201590578 |
File details
Details for the file namedzip-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: namedzip-1.0.2-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47a62bce2faca33ebe36e0751a2196bd28ebf819f6a17872c327398e12eef69d |
|
MD5 | c073b47ffb9e4e439c35fdcfed0effe3 |
|
BLAKE2b-256 | c532a44ba64e7102c7195dc06b1a2841da2f8d7213e5af109f3ead5a2e94a26b |