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.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fc10b068269f4d855e74c2c6c5362ddc2ba8def17ad0ff00a3de077fd672bf3 |
|
MD5 | 0adf57e4e1340e202fe617149232c0a5 |
|
BLAKE2b-256 | 0131357324f5bf6cf3163b043356b184d4d43d26e47269ede06e51870d276f8b |
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbc889b691e360a3667825359d48d40be27212cf210e51a663755e9e16cfa916 |
|
MD5 | 976ac05d86c9efcd61c8faa3b825fe74 |
|
BLAKE2b-256 | f112729b56cdf581a76bd2a2045cd2623907ee80baa87ad8f529884e7260229e |