Extends zip() and itertools.zip_longest() to generate named tuples.
Project description
This Python 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)
>>>
Purpose: Why / how could this be useful?
The main idea behind this package is to help improve readability in cases where you have a need to iterate over more than just a few collections/streams of data. Instead of messing with indices or unpacking long tuples, namedzip allows you to access aggregated values by attribute names.
A small hypothetical example of iterating over streams of sensor data in three ways:
zip_data = namedzip(
typename="Data", field_names=("temp_f", "humidity", "wind_mph", "pressure_hpa")
)
# Accessing values by attribute names.
for data in zip_data(temperature_f, humidity, wind_mph, pressure_hpa):
temp_c = (data.temp_f - 32) / 1.8
wind_knots = data.wind_mph / 1.15078
pressure_atm = data.pressure_hpa / 1013.25
dew_point = calculate_dew_point(temp_c, data.humidity)
# Accessing values by indices.
for data in zip(temperature_f, humidity, wind_mph, pressure_hpa):
temp_c = (data[0] - 32) / 1.8
wind_knots = data[2] / 1.15078
pressure_atm = data[3] / 1013.25
dew_point = calculate_dew_point(temp_c, data[1])
# Unpacking values in for statement.
for temp_f, humidity, wind_mph, pressure_hpa in zip(temperature_f, humidity, wind_mph, pressure_hpa):
temp_c = (temp_f - 32) / 1.8
wind_knots = wind_mph / 1.15078
pressure_atm = pressure_hpa / 1013.25
dew_point = calculate_dew_point(temp_c, humidity)
# NOTE: The formulas included above have not been checked and may not be accurate.
Additionally, namedzip_longest allows for individual default values to be specified for each iterable which zip_longest does not.
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 -r 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.6.tar.gz
.
File metadata
- Download URL: namedzip-1.0.6.tar.gz
- Upload date:
- Size: 4.6 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 | b36a980a4e00eb774d15c0d26dd43bb9ce38f3b2def48974a58712ebba696e1e |
|
MD5 | 965a403d9ed0005384c336d9a7a768be |
|
BLAKE2b-256 | 516d5ce7e257d85c2be7e8e637363559af9fb91cfe5ffa0a840089967d4b9e29 |
File details
Details for the file namedzip-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: namedzip-1.0.6-py3-none-any.whl
- Upload date:
- Size: 5.6 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 | 90858cab0fcfc93aa1ae595d269485b13e4f0e66930bfd03ee9b053b371a6dec |
|
MD5 | 6b139731988cd48bae3c96078bebd7b1 |
|
BLAKE2b-256 | 68e365f584ee80ce0c6711ab592eb61bf4eb6ab170ebbbbd5a68a509d2803dcb |