{"id":29089761,"url":"https://github.com/timothyhull/namedtuple-maker","last_synced_at":"2025-06-28T04:04:58.473Z","repository":{"id":44675896,"uuid":"398991576","full_name":"timothyhull/namedtuple-maker","owner":"timothyhull","description":"Convert Python Iterable Objects to `namedtuple`","archived":false,"fork":false,"pushed_at":"2022-01-31T17:25:12.000Z","size":224,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-27T17:12:04.998Z","etag":null,"topics":["iterable","namedtuple","namedtuple-in-python","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/namedtuple-maker/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timothyhull.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-23T06:06:23.000Z","updated_at":"2021-10-31T04:38:33.000Z","dependencies_parsed_at":"2022-09-12T15:23:04.058Z","dependency_job_id":null,"html_url":"https://github.com/timothyhull/namedtuple-maker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/timothyhull/namedtuple-maker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothyhull%2Fnamedtuple-maker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothyhull%2Fnamedtuple-maker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothyhull%2Fnamedtuple-maker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothyhull%2Fnamedtuple-maker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timothyhull","download_url":"https://codeload.github.com/timothyhull/namedtuple-maker/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothyhull%2Fnamedtuple-maker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262371678,"owners_count":23300595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["iterable","namedtuple","namedtuple-in-python","python"],"created_at":"2025-06-28T04:04:57.727Z","updated_at":"2025-06-28T04:04:58.464Z","avatar_url":"https://github.com/timothyhull.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `namedtuple` Maker\n\n![GitHub Actions Status](https://img.shields.io/github/workflow/status/wwt/devasc-data-formats/Markdown%20Linting?logo=github \"GitHub Actions Status\")\n[![BCH compliance](https://bettercodehub.com/edge/badge/timothyhull/namedtuple-maker?branch=main)](https://bettercodehub.com/)\n\n## Easily Convert Python iterable objects into `namedtuple` objects\n\n### Contents\n\n- [Capabilities](#capabilities \"Capabilities\")\n- [Requirements](#requirements \"Requirements\")\n- [Installation](#installation \"Installation\")\n- [Usage](#usage \"Usage\")\n- [Background](#background \"Background\")\n- [`namedtuple` objects and The Zen of Python](#namedtuple-objects-and-the-zen-of-python \"The Zen of Python\")\n\n---\n\n### Capabilities\n\n- Converts Python iterable objects (`list`, `tuple`, `set`, etc.) into `namedtuple` objects using a decorator function, **so you don't have to rewrite code that already returns iterable objects**.\n\n- Sets `namedtuple` attribute names in one of three ways:\n\n    1. Entering attribute names at input prompts (default).\n    2. Manually, by passing an iterable object of attribute names as a keyword argument to the decorated function.\n    3. Setting a keyword argument to automatically generate attribute names.\n\n- Automatically corrects attribute name entries that would be invalid.\n\n---\n\n### Requirements\n\n- Python 3.9+\n\n---\n\n### Installation\n\nInstall via Python pip:\n\n```bash\npip install namedtuple-maker\n```\n\n---\n\n### Usage\n\n#### Usage as a Decorator\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eClick to expand and view a decorator example\u003c/b\u003e\u003c/summary\u003e\n\n1. Create an iterable object:\n\n    ```python\n    my_favorites = (\n        'pizza',\n        'summer',\n        'too personal'\n    )\n    ```\n\n2. Import the `convert_to_namedtuple` decorator function:\n\n    ```python\n    from namedtuple_maker.namedtuple_maker import named_tuple_converter\n    ```\n\n3. Create a function that returns an iterable object, and decorate that function with the `convert_to_namedtuple` decorator function:\n\n    ```python\n    @named_tuple_converter\n    def tuple_to_namedtuple(\n        iterable_input=my_favorites,\n        attribute_names=None\n    ):\n\n        return iterable_input\n    ```\n\n4. Call the `tuple_to_namedtuple` function:\n    - Pass an iterable object (the default `my_favorites` object, in this example) to the `iterable_input` parameter.\n    - By default, you will receive a prompt to provide an attribute name for each iterable input value.\n    - You may instead pass a separate iterable object of attribute names to the `attribute_names` parameter.\n\n    \u003cdetails\u003e\u003csummary\u003eOption #1 - Enter attribute names using prompts:\u003c/summary\u003e\n\n    ```python\n    # Call the tuple_to_namedtuple function and fill the attribute name prompts\n    my_named_favorites = tuple_to_namedtuple()\n    ```\n\n    ```text\n    Enter an attribute name for the value \"pizza\": food\n    Enter an attribute name for the value \"summer\": season\n    Enter an attribute name for the value \"too personal\": sports team\n    ```\n\n    \u003c/details\u003e\n\n    \u003cdetails\u003e\u003csummary\u003eOption #2 - Pass an iterable object of attribute names to the `attribute_names` parameter:\u003c/summary\u003e\n\n    ```python\n    # Create an iterable object with attribute names\n    my_attributes = (\n        'food',\n        'season',\n        'sports team'\n    )\n\n    # Call the make_named_tuple function and pass in the attribute names\n    my_named_favorites = tuple_to_namedtuple(\n        attribute_names=my_attributes\n    )\n    ```\n\n    \u003c/details\u003e\n\n5. Display the resulting `namedtuple` object:\n\n    ```python\n    print(my_named_favorites)\n    ```\n\n6. Observe the `print` function output:\n\n    ```text\n    NamedTuple(food='pizza', season='summer', sports_team='too personal')\n    ```\n\n\u003c/details\u003e\n\n---\n\n#### Usage as a Function\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eClick to expand and view a function usage example\u003c/b\u003e\u003c/summary\u003e\n\n1. Create an iterable object:\n\n    ```python\n    my_favorites = (\n        'pizza',\n        'summer',\n        'too personal'\n    )\n    ```\n\n2. Import the `make_named_tuple` function:\n\n    ```python\n    from namedtuple_maker.namedtuple_maker import make_named_tuple\n    ```\n\n3. Call the `make_named_tuple` function:\n    - Pass an iterable object (the default `my_favorites` object, in this example) to the `iterable_input` parameter.\n    - By default, you will receive a prompt to provide an attribute name for each iterable input value.\n    - You may instead pass a separate iterable object of attribute names to the `attribute_names` parameter.\n\n    \u003cdetails\u003e\u003csummary\u003eOption #1 - Enter attribute names using prompts:\u003c/summary\u003e\n\n    ```python\n    # Call the make_named_tuple function and fill the attribute name prompts\n    my_named_favorites = make_named_tuple(\n        iterable_input=my_favorites\n    )\n    ```\n\n    ```text\n    Enter an attribute name for the value \"pizza\": food\n    Enter an attribute name for the value \"summer\": season\n    Enter an attribute name for the value \"too personal\": sports team\n    ```\n\n    \u003c/details\u003e\n\n    \u003cdetails\u003e\u003csummary\u003eOption #2 - Pass an iterable object of attribute names to the `attribute_names` parameter:\u003c/summary\u003e\n\n    ```python\n    # Create an iterable object with attribute names\n    my_attributes = (\n        'food',\n        'season',\n        'sports team'\n    )\n\n    # Call the make_named_tuple function and pass in the attribute names\n    my_named_favorites = make_named_tuple(\n        iterable_input=my_favorites,\n        attribute_names=my_attributes\n    )\n    ```\n\n    \u003c/details\u003e\n\n4. Display the resulting `namedtuple` object:\n\n    ```python\n    print(my_named_favorites)\n    ```\n\n5. Observe the `print` function output:\n\n    ```text\n    NamedTuple(food='pizza', season='summer', sports_team='too personal')\n    ```\n\n\u003c/details\u003e\n\n---\n\n### Background\n\nPython `tuple` objects are great, right?  So are `list`, `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 following `list` object stores data about the foods I might eat in a given day:\n\n```python\nmy_meals = [\n    'pizza',\n    'blueberry pancakes',\n    'granola',\n    'fruit smoothie',\n    'rice and beans'\n]\n```\n\nLet's say that I want to access values in the `list` object named `my_food`. I can do that by referencing one or more numeric **list indices** like this:\n\n```python\nprint('My Meals')\nprint('--------')\nprint(f'Breakfast: {my_meals[1]}\\n'\n      f'Snack: {my_meals[3]}\\n'\n      f'Lunch: {my_meals[4]}')\n```\n\nThe resulting output from this code is:\n\n```text\nMy Meals\n--------\nBreakfast: blueberry pancakes\nSnack: fruit smoothie\nLunch: rice and beans\n```\n\nThat works just fine, although it's not terribly intuitive to associate a `list` (or `tuple`) **index number** with a certain meal of the day, since this `list` doesn't have meals in any particular order. The list index assigned to each meal is _arbitrary_.\n\n---\n\n### The Python `namedtuple` Function\n\nThe `collections` module in the Python Standard Library includes the [`namedtuple` function](https://docs.python.org/3/library/collections.html#collections.namedtuple), 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 an _explicit_ **attribute name** that is much more meaningful than an _arbitrary_ **index number**.\n\nFor example, I'll recreate the `my_meals` data using a `namedtuple` object:\n\n1. First, import the `namedtuple` function from the `collections` module:\n\n    ```python\n    from collections import namedtuple\n    ```\n\n2. Next, create a new object type using the `namedtuple` function.\n    - Think of a `namedtuple` object like a `class` object with named attributes, but no methods.\n    - The `typename` parameter is an arbitrary name for the object class.\n    - The `field_names` parameter defines the attribute names for the new object.\n\n    ```python\n    Meals = namedtuple(\n        typename='Meals',\n        field_names=[\n            'breakfast',\n            'snack',\n            'lunch',\n            'dinner',\n            'dessert'\n        ]\n    )\n    ```\n\n3. Now, create an instance of the `Meals` object, and assign the individual foods to each of the named attributes (specified by the `field_names` parameter):\n\n    ```python\n    my_meals = Meals(\n        breakfast='blueberry pancakes',\n        snack='fruit smoothie',\n        lunch='rice and beans',\n        dinner='pizza',\n        dessert='granola'\n    )\n    ```\n\nWhen I want to access or display data from the `namedtuple` object named `my_meals`, my code will look something like this:\n\n```python\nprint('My Meals')\nprint('--------')\nprint(f'Breakfast: {my_meals.breakfast}\\n'\n      f'Snack: {my_meals.snack}\\n'\n      f'Lunch: {my_meals.lunch}')\n```\n\nThe result from this code looks exactly like it did in the first example:\n\n```text\nMy Meals\n--------\nBreakfast: blueberry pancakes\nSnack: fruit smoothie\nLunch: rice and beans\n```\n\nThe key difference between the `list` example and the `namedtuple` example is, accessing the values in the `namedtuple` object uses _explicit_ **named attributes** (`my_meals.lunch`), rather than _arbitrary_ **index numbers** (`my_meals[4]`).\n\nFor me, it's much easier to remember that the food I ate for breakfast is accessible as `my_foods.breakfast` within a `namedtuple` object, than it is to remember an arbitrary `list` index value like `my_foods[3]`.\n\n---\n\n### `namedtuple` objects and The Zen of Python\n\nThe [Zen of Python](https://www.python.org/dev/peps/pep-0020/ \"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.\n\nThe intent of the `named-tuplemaker` 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_ **index numbers**.\n\n```text\nThe Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\n...\nReadability counts.\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothyhull%2Fnamedtuple-maker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimothyhull%2Fnamedtuple-maker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothyhull%2Fnamedtuple-maker/lists"}