Package: namedzip
This Python package implementsnamedzipandnamedzip_longest, which extendzipanditertools.zip_longestrespectively to generate named tuples.Installation$pipinstallnamedzipUsage examples>>>fromnamedzipimportnamedtuple,namedzip,namedzip_longestnamedzipandnamedzip_longestcan either be usedwith 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"])>>>>>>forpointinnamedzip(Point,x_vals,y_vals):...print(point)...Point(x=1,y=9)Point(x=2,y=8)>>>>>>forpointinnamedzip_longest(Point,x_vals,y_vals):...print(point)...Point(x=1,y=9)Point(x=2,y=8)Point(x=3,y=None)>>>Orwithout iterable argumentsto return reusable function objects:>>>zip_points=namedzip(Point)>>>forpointinzip_points(x_vals,y_vals):...print(point)...Point(x=1,y=9)Point(x=2,y=8)>>>>>>zip_points=namedzip_longest(Point)>>>forpointinzip_points(x_vals,y_vals):...print(point)...Point(x=1,y=9)Point(x=2,y=8)Point(x=3,y=None)>>>Just likeitertools.zip_longest,namedzip_longesttakes a customfillvalue.>>>iterables=[(1,2),(9,8,7),(11,22)]>>>Point3D=namedtuple("Point3D",["x","y","z"])>>>>>>forpointinnamedzip_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)>>>Howevernamedzip_longestalso 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))>>>>>>forpointinnamedzip_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)>>>>>>forpointinnamedzip_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,namedzipallows 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))fordatainzip_data(*sensor_data):temp_c=(data.temp_f-32)/1.8wind_knots=data.wind_mph/1.15078pressure_atm=data.pressure_hpa/1013.25dew_point=calculate_dew_point(temp_c,data.humidity)# NOTE: The formulas used above may not be accurate.DocumentationAdditional documentation is available athttps://namedzip.readthedocs.io/en/latest/.Development setupClone repo:$gitclonehttps://github.com/erberlin/namedzip.git$cdnamedzipCreate and activate virtual environment on Windows:>python-mvenvvenv>venv\Scripts\activateCreate and activate virtual environment on OS X & Linux:$python3-mvenvvenv$sourcevenv/bin/activateInstall development packages:$pipinstall-rrequirements.txtRun test suite:$pytest-vMetaErik R Berlin -erberlin.dev@gmail.comDistributed under the MIT license. SeeLICENSEfor more information.https://github.com/erberlin/namedzip