Easily convert iterable objects into `namedtuple` objects.
Project description
namedtuple
Maker
Easily Convert Python iterable objects to namedtuple
objects
Contents
Capabilities
Convert a Python iterable object into a namedtuple
object using a decorator function.
Provide the namedtuple
attribute names in a kwarg
of the decorated function, or enter attribute names at prompts.
Automatically corrects attribute name entries that would be invalid.
Requirements
- Python 3.9+
Installation
Install via Python pip
pip install namedtuple-maker
Usage
Usage as a Decorator
Click to expand and view a decorator example
-
Create an iterable object:
my_favorites = ( 'pizza', 'summer', 'too personal' )
-
Import the
convert_to_namedtuple
decorator function:from namedtuple_maker.namedtuple_maker import named_tuple_converter
-
Create a function that returns an iterable object, and decorate that function with the
convert_to_namedtuple
decorator function:@named_tuple_converter def tuple_to_namedtuple( iterable_input=my_favorites, attribute_names=None ): return iterable_input
-
Call the
tuple_to_namedtuple
function:- Pass an iterable object (the default
my_favorites
object, in this example) to theiterable_input
parameter. - By default, you will receive a prompt to provide an attribute name for each iterable input value.
- You may instead pass a separate iterable object of attribute names to the
attribute_names
parameter.
Option #1 - Enter attribute names using prompts:
# Call the tuple_to_namedtuple function and fill the attribute name prompts my_named_favorites = tuple_to_namedtuple()
Enter an attribute name for the value "pizza": food Enter an attribute name for the value "summer": season Enter an attribute name for the value "too personal": sports team
Option #2 - Pass an iterable object of attribute names to the `attribute_names` parameter:
# Create an iterable object with attribute names my_attributes = ( 'food', 'season', 'sports team' ) # Call the make_named_tuple function and pass in the attribute names my_named_favorites = tuple_to_namedtuple( attribute_names=my_attributes )
- Pass an iterable object (the default
-
Display the resulting
namedtuple
object:print(my_named_favorites)
-
Observe the
print
function output:NamedTuple(food='pizza', season='summer', sports_team='too personal')
Usage as a Function
Click to expand and view a function usage example
-
Create an iterable object:
my_favorites = ( 'pizza', 'summer', 'too personal' )
-
Import the
make_named_tuple
function:from namedtuple_maker.namedtuple_maker import make_named_tuple
-
Call the
make_named_tuple
function:- Pass an iterable object (the default
my_favorites
object, in this example) to theiterable_input
parameter. - By default, you will receive a prompt to provide an attribute name for each iterable input value.
- You may instead pass a separate iterable object of attribute names to the
attribute_names
parameter.
Option #1 - Enter attribute names using prompts:
# Call the make_named_tuple function and fill the attribute name prompts my_named_favorites = make_named_tuple( iterable_input=my_favorites )
Enter an attribute name for the value "pizza": food Enter an attribute name for the value "summer": season Enter an attribute name for the value "too personal": sports team
Option #2 - Pass an iterable object of attribute names to the `attribute_names` parameter:
# Create an iterable object with attribute names my_attributes = ( 'food', 'season', 'sports team' ) # Call the make_named_tuple function and pass in the attribute names my_named_favorites = make_named_tuple( iterable_input=my_favorites, attribute_names=my_attributes )
- Pass an iterable object (the default
-
Display the resulting
namedtuple
object:print(my_named_favorites)
-
Observe the
print
function output:NamedTuple(food='pizza', season='summer', sports_team='too personal')
Background
Python tuple
objects are great, right? So are list
, set
, and many other iterable Python objects. However, accessing the values of an iterable by an arbitrary index number can make code difficult to read. For example, the following list
object stores data about the foods I might eat in a given day:
my_meals = [
'pizza',
'blueberry pancakes',
'granola',
'fruit smoothie',
'rice and beans'
]
Let's say that I want to access values in the my_food
list
object. I can do that by referencing one or more numeric list indices like this:
print('My Meals')
print('--------')
print(f'Breakfast: {my_meals[1]}\n'
f'Snack: {my_meals[3]}\n'
f'Lunch: {my_meals[4]}')
The resulting output from this code would be:
My Meals
--------
Breakfast: blueberry pancakes
Snack: fruit smoothie
Lunch: rice and beans
That works just fine, although it's not terribly intuitive to associate a list
(or tuple
) index with a certain meal of the day, since this particular list
doesn't have the meals in any particular order. The list index assigned to each meal is arbitrary.
The Python namedtuple
Function
The collections
module in the Python Standard Library includes the namedtuple
function, which allows you to, as the name implies, create tuple-like objects with values that you can reference by name. What does that mean, practically? Well, it means you can access then values in an iterable object by an attribute name that is much more meaningful than an arbitrary index number.
For example, I'll recreate the my_meals
data using a namedtuple
object:
-
First, import the
namedtuple
function from thecollections
module:from collections import namedtuple
-
Next, create a new object type using the
namedtuple
function.- Think of a
namedtuple
object like aclass
object with named attributes but no methods. - The
typename
parameter is an arbitrary name for the object class. - The
field_names
parameter defines the attribute names for the new object.
Meals = namedtuple( typename='Meals', field_names=[ 'breakfast', 'snack', 'lunch', 'dinner', 'dessert' ] )
- Think of a
-
Now, create an instance of the
Meals
object, and assign the individual foods to each of the named attributes (specified by thefield_names
parameter):my_meals = Meals( breakfast='blueberry pancakes', snack='fruit smoothie', lunch='rice and beans', dinner='pizza', dessert='granola' )
When I want to access or display data from the my_meals
namedtuple
object, my code will look something like this:
print('My Meals')
print('--------')
print(f'Breakfast: {my_meals.breakfast}\n'
f'Snack: {my_meals.snack}\n'
f'Lunch: {my_meals.lunch}')
The result from this code looks exactly like it did in the first example:
My Meals
--------
Breakfast: blueberry pancakes
Snack: fruit smoothie
Lunch: rice and beans
The key difference between the list
example and the namedtuple
example is, accessing the values in the namedtuple
object uses named attributes, rather than arbitrary index numbers. For me, it's much easier to remember that the food I ate for breakfast is accessible as my_foods.breakfast
within in a namedtuple
object, than it is to remember an arbitrary list
index value like my_foods[3]
.
namedtuple
objects and The Zen of Python
The Zen of Python is a great guide for how to write clean and effective Python code. Below is an extract of some of the lines in the output of an import this
command. The intent of this package is to help Python developers write code that improves compliance with The Zen of Python by making it simple and easy to access iterable object values by explicit attribute names, rather than arbitrary numbers.
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
...
Readability counts.
...
Project details
Release history Release notifications | RSS feed
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
Hashes for namedtuple_maker-1.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d6a139de3a3b65b2891e0c514d1e1adf9177bcf9cfa2dfb200f128fdb1bb04e |
|
MD5 | d835d2bc29d87c6300ea96710c29bc7e |
|
BLAKE2b-256 | 8c0590cc4cdf1c085587adb77cca6e059e10cb8b38cdfb8b7da64eb63c096994 |