Skip to content
Extends zip() and itertools.zip_longest() to generate named tuples.
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
docs
namedzip
tests
.flake8
.gitignore
LICENSE
README.rst
ci_requirements.txt
requirements.txt
setup.py

README.rst

namedzip

license pypi pyversions wheel build docs

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.

https://github.com/erberlin/namedzip

You can’t perform that action at this time.