Package: namedtuple-maker
namedtupleMakerEasily Convert Python iterable objects intonamedtupleobjectsContentsCapabilitiesRequirementsInstallationUsageBackgroundnamedtupleobjects and The Zen of PythonCapabilitiesConverts Python iterable objects (list,tuple,set, etc.) into anamedtupleobjects using a decorator function,so you don't have to rewrite code that already returns iterable objects.Gathersnamedtupleattribute names via either akwargin a decorated function or using prompts to collect attribute names.Automatically corrects attribute name entries that would be invalid.RequirementsPython 3.9+InstallationInstall via Python pip:pipinstallnamedtuple-makerUsageUsage as a DecoratorClick to expand and view a decorator exampleCreate an iterable object:my_favorites=('pizza','summer','too personal')Import theconvert_to_namedtupledecorator function:fromnamedtuple_maker.namedtuple_makerimportnamed_tuple_converterCreate a function that returns an iterable object, and decorate that function with theconvert_to_namedtupledecorator function:@named_tuple_converterdeftuple_to_namedtuple(iterable_input=my_favorites,attribute_names=None):returniterable_inputCall thetuple_to_namedtuplefunction:Pass an iterable object (the defaultmy_favoritesobject, in this example) to theiterable_inputparameter.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 theattribute_namesparameter.Option #1 - Enter attribute names using prompts:# Call the tuple_to_namedtuple function and fill the attribute name promptsmy_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 teamOption #2 - Pass an iterable object of attribute names to the `attribute_names` parameter:# Create an iterable object with attribute namesmy_attributes=('food','season','sports team')# Call the make_named_tuple function and pass in the attribute namesmy_named_favorites=tuple_to_namedtuple(attribute_names=my_attributes)Display the resultingnamedtupleobject:print(my_named_favorites)Observe theprintfunction output:NamedTuple(food='pizza', season='summer', sports_team='too personal')Usage as a FunctionClick to expand and view a function usage exampleCreate an iterable object:my_favorites=('pizza','summer','too personal')Import themake_named_tuplefunction:fromnamedtuple_maker.namedtuple_makerimportmake_named_tupleCall themake_named_tuplefunction:Pass an iterable object (the defaultmy_favoritesobject, in this example) to theiterable_inputparameter.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 theattribute_namesparameter.Option #1 - Enter attribute names using prompts:# Call the make_named_tuple function and fill the attribute name promptsmy_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 teamOption #2 - Pass an iterable object of attribute names to the `attribute_names` parameter:# Create an iterable object with attribute namesmy_attributes=('food','season','sports team')# Call the make_named_tuple function and pass in the attribute namesmy_named_favorites=make_named_tuple(iterable_input=my_favorites,attribute_names=my_attributes)Display the resultingnamedtupleobject:print(my_named_favorites)Observe theprintfunction output:NamedTuple(food='pizza', season='summer', sports_team='too personal')BackgroundPythontupleobjects are great, right?  So arelist,set, and many other iterable Python objects. However, accessing the values of an iterable object using arbitrary index numbers can make code difficult to read.  For example, the followinglistobject 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 thelistobject namedmy_food. I can do that by referencing one or more numericlist indiceslike 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 is:My Meals
--------
Breakfast: blueberry pancakes
Snack: fruit smoothie
Lunch: rice and beansThat works just fine, although it's not terribly intuitive to associate alist(ortuple)index numberwith a certain meal of the day, since thislistdoesn't have meals in any particular order. The list index assigned to each meal isarbitrary.The PythonnamedtupleFunctionThecollectionsmodule in the Python Standard Library includes thenamedtuplefunction, 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 the values in an iterable object by anexplicitattribute namethat is much more meaningful than anarbitraryindex number.For example, I'll recreate themy_mealsdata using anamedtupleobject:First, import thenamedtuplefunction from thecollectionsmodule:fromcollectionsimportnamedtupleNext, create a new object type using thenamedtuplefunction.Think of anamedtupleobject like aclassobject with named attributes, but no methods.Thetypenameparameter is an arbitrary name for the object class.Thefield_namesparameter defines the attribute names for the new object.Meals=namedtuple(typename='Meals',field_names=['breakfast','snack','lunch','dinner','dessert'])Now, create an instance of theMealsobject, and assign the individual foods to each of the named attributes (specified by thefield_namesparameter):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 thenamedtupleobject namedmy_meals, 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 beansThe key difference between thelistexample and thenamedtupleexample is, accessing the values in thenamedtupleobject usesexplicitnamed attributes(my_meals.lunch), rather thanarbitraryindex numbers(my_meals[4]).For me, it's much easier to remember that the food I ate for breakfast is accessible asmy_foods.breakfastwithin anamedtupleobject, than it is to remember an arbitrarylistindex value likemy_foods[3].namedtupleobjects and The Zen of PythonTheZen of Pythonis 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 animport thiscommand.The intent of thenamed-tuplemakerpackage 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 byexplicitattribute names, rather thanarbitraryindex 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.
...