Skip to main content

Extends zip() and itertools.zip_longest() to generate named tuples.

Project description

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 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.

https://github.com/erberlin/namedzip

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

namedzip-1.0.6.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

namedzip-1.0.6-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page