Skip to main content

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

Metadata

Release files for namedzip 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for namedzip 1.1.0
File Size Uploaded
namedzip-1.1.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for namedzip 1.1.0
File Interpreter ABI Platform
namedzip-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / namedzip-1.1.0.tar.gz

Download URL namedzip-1.1.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1c8d93b00adff89744e76e275b803b0d051d24299216d42e181c3c3087523024
BLAKE2b-256 checksum
How to use checksums
07717d0909f876067ab9cf0069ea6205bdd48e04356fd3e7c2dcff8c3cc81e8c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / namedzip-1.1.0-py3-none-any.whl

Download URL namedzip-1.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
782dc6e50d5c9d18d5a87cd7422930f62e39dd7d7176aee71f0bb354e412f751
BLAKE2b-256 checksum
How to use checksums
9cbec9bf6db8c840b34c46c8313ada2a2f81e4f4e7b06eb18ede8ec47aeb0c11
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page