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.
Installation
$ pip install namedzip
Usage examples
>>> from namedzip import namedtuple, namedzip, namedzip_longest
namedzip and namedzip_longest can either be used with iterable arguments, like the interfaces which they extend, to return generator objects:
>>> x_vals = [1, 2, 3]
>>> y_vals = [9, 8]
>>> Point = namedtuple("Point", ["x", "y"])
>>>
>>> for point in namedzip(Point, x_vals, y_vals):
... print(point)
...
Point(x=1, y=9)
Point(x=2, y=8)
>>>
>>> for point in namedzip_longest(Point, x_vals, y_vals):
... print(point)
...
Point(x=1, y=9)
Point(x=2, y=8)
Point(x=3, y=None)
>>>
Or without iterable arguments to return reusable function objects:
>>> zip_points = namedzip(Point)
>>> for point in zip_points(x_vals, y_vals):
... print(point)
...
Point(x=1, y=9)
Point(x=2, y=8)
>>>
>>> zip_points = namedzip_longest(Point)
>>> for point in zip_points(x_vals, y_vals):
... print(point)
...
Point(x=1, y=9)
Point(x=2, y=8)
Point(x=3, y=None)
>>>
Just like itertools.zip_longest, namedzip_longest takes a custom fillvalue.
>>> iterables = [(1, 2), (9, 8, 7), (11, 22)]
>>> Point3D = namedtuple("Point3D", ["x", "y", "z"])
>>>
>>> for point in namedzip_longest(Point3D, *iterables, fillvalue=0):
... print(point)
...
Point3D(x=1, y=9, z=11)
Point3D(x=2, y=8, z=22)
Point3D(x=0, y=7, z=0)
>>>
However namedzip_longest also allows for the use of individual default values specified in the named tuple or in the function call.
>>> iterables = [(1, 2), (9, 8, 7), (11, 22)]
>>> Point3D = namedtuple("Point3D", ["x", "y", "z"], defaults=(100, 1, 0))
>>>
>>> for point in namedzip_longest(Point3D, *iterables):
... print(point)
...
Point3D(x=1, y=9, z=11)
Point3D(x=2, y=8, z=22)
Point3D(x=100, y=7, z=0)
>>>
>>> for point in namedzip_longest(Point3D, *iterables, defaults=(77, 88, 99)):
... print(point)
...
Point3D(x=1, y=9, z=11)
Point3D(x=2, y=8, z=22)
Point3D(x=77, y=7, z=99)
>>>
How could this be useful?
The idea behind this package is to help improve readability in cases where you have a need to iterate over multiple collections/streams of data, as well as to allow for individual default values like show above.
Instead of messing with indices or unpacking long tuples, namedzip allows you to access aggregated values by attribute names using dot notation.
sensor_data = [fahrenheit_vals, humidity_vals, wind_mph_vals, pressure_hpa_vals]
Data = namedtuple("Data", ("temp_f", "humidity", "wind_mph", "pressure_hpa"))
zip_data = namedzip_longest(Data, defaults=(57.2, 68.3, 17.1, 1016.93))
for data in zip_data(*sensor_data):
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)
# NOTE: The formulas used above may not be accurate.
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.1.0.tar.gz
.
File metadata
- Download URL: namedzip-1.1.0.tar.gz
- Upload date:
- Size: 5.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.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c8d93b00adff89744e76e275b803b0d051d24299216d42e181c3c3087523024 |
|
MD5 | 7091288dbbef1f77ecdc5ccba5b283de |
|
BLAKE2b-256 | 07717d0909f876067ab9cf0069ea6205bdd48e04356fd3e7c2dcff8c3cc81e8c |
Provenance
File details
Details for the file namedzip-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: namedzip-1.1.0-py3-none-any.whl
- Upload date:
- Size: 6.7 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.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 782dc6e50d5c9d18d5a87cd7422930f62e39dd7d7176aee71f0bb354e412f751 |
|
MD5 | 959d875e6befbf8201ba9f4ddd116c08 |
|
BLAKE2b-256 | 9cbec9bf6db8c840b34c46c8313ada2a2f81e4f4e7b06eb18ede8ec47aeb0c11 |