# namedtuple-table
### Problem
- You want to make a "sample table" config file (e.g. for Snakemake), so that various system-specific attributes can be accessed via an index.
- You want to store it as a human-readable tab-separated text file.
- You don't want to install Pandas.
### Solution
- NamedTupleTable represents tabular data as a mapping between some index column and rows of some NamedTuple.
```
my_table["label_1"] -> ThisTableNamedTuple
```
- To index on a different column, produce a new table by calling
`.with_index("new_index")`. Values of the new index must be unique in
every row.
- Tables are immutable and hashable, so should play nicely with
caching, filters etc. We could add a "select" method etc. but it
should be straightforward to do this stuff with Python's
functional programming features.
### Drawbacks
- This is not designed to scale; in the intended use-case the table
size is modest and you are doing somewhat expensive things with the
data. If you need performance/scale, consider Pandas or a database
interface like [dataset](https://pypi.org/project/dataset/).
## Usage example
Store data in a tab-separated variable file. The first non-comment line must be a set of column headers.
Other lines can be commented out with `#` or `!`
```
# dogs.tsv
ref name collar age
1 Bertie red 4
2 Geoff blue 2
!3 Bandit none 40
4 Gertrude blue 5
```
and load with
```
>>> from namedtuple_table import NamedTupleTable
>>> from pathlib import Path
>>> dogs = NamedTupleTable.from_tsv(Path("dogs.tsv"))
```
Now you have a dict-like Mapping of data rows represented as NamedTuple objects.
All data is loaded as strings, so you might need to cast back and forth to int.
```
>>> print(dogs)
NamedTupleTable (3 items, index = ref)
>>> for i in range(5):
... if str(i) in dogs:
... print(dogs[str(i)])
...
TableRow(ref='1', name='Bertie', collar='red', age='4')
TableRow(ref='2', name='Geoff', collar='blue', age='2')
TableRow(ref='4', name='Gertrude', collar='blue', age='5')
>>> dogs['4'].collar
'blue'
```
To use a different index column, get a new table with the `.index_by` method.
```
>>> dogs_by_name = dogs.with_index("name")
>>> dogs_by_name["Geoff"]
TableRow(ref='2', name='Geoff', collar='blue', age='2')
```
Raw data
{
"_id": null,
"home_page": null,
"name": "namedtuple-table",
"maintainer": "Adam Jackson",
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": "Adam Jackson <adam.jackson@stfc.ac.uk>",
"keywords": "Python, table, index, simple, lightweight",
"author": "Adam Jackson",
"author_email": "Adam Jackson <adam.jackson@stfc.ac.uk>",
"download_url": "https://files.pythonhosted.org/packages/f7/42/6a2c6905699356d33bfcfc8324910b563866d9fb344f71820a4e7bc0cc0a/namedtuple_table-0.1.0.post1.tar.gz",
"platform": null,
"description": "# namedtuple-table\n\n### Problem\n- You want to make a \"sample table\" config file (e.g. for Snakemake), so that various system-specific attributes can be accessed via an index.\n- You want to store it as a human-readable tab-separated text file.\n- You don't want to install Pandas.\n\n### Solution\n- NamedTupleTable represents tabular data as a mapping between some index column and rows of some NamedTuple.\n\n ```\n my_table[\"label_1\"] -> ThisTableNamedTuple\n ```\n\n- To index on a different column, produce a new table by calling\n `.with_index(\"new_index\")`. Values of the new index must be unique in\n every row.\n\n- Tables are immutable and hashable, so should play nicely with\n caching, filters etc. We could add a \"select\" method etc. but it\n should be straightforward to do this stuff with Python's\n functional programming features.\n\n### Drawbacks\n\n- This is not designed to scale; in the intended use-case the table\n size is modest and you are doing somewhat expensive things with the\n data. If you need performance/scale, consider Pandas or a database\n interface like [dataset](https://pypi.org/project/dataset/).\n\n\n## Usage example\n\nStore data in a tab-separated variable file. The first non-comment line must be a set of column headers.\nOther lines can be commented out with `#` or `!`\n\n```\n# dogs.tsv\nref\tname\t\tcollar\tage\n1\tBertie\t\tred\t4\n2\tGeoff\t\tblue\t2\n!3\tBandit\t\tnone\t40\n4\tGertrude\tblue\t5\n\n```\n\nand load with\n\n```\n>>> from namedtuple_table import NamedTupleTable\n>>> from pathlib import Path\n\n>>> dogs = NamedTupleTable.from_tsv(Path(\"dogs.tsv\"))\n\n```\n\nNow you have a dict-like Mapping of data rows represented as NamedTuple objects.\nAll data is loaded as strings, so you might need to cast back and forth to int.\n\n```\n>>> print(dogs)\nNamedTupleTable (3 items, index = ref)\n\n>>> for i in range(5):\n... if str(i) in dogs:\n... print(dogs[str(i)])\n...\nTableRow(ref='1', name='Bertie', collar='red', age='4')\nTableRow(ref='2', name='Geoff', collar='blue', age='2')\nTableRow(ref='4', name='Gertrude', collar='blue', age='5')\n\n>>> dogs['4'].collar\n'blue'\n```\n\nTo use a different index column, get a new table with the `.index_by` method.\n\n```\n>>> dogs_by_name = dogs.with_index(\"name\")\n>>> dogs_by_name[\"Geoff\"]\nTableRow(ref='2', name='Geoff', collar='blue', age='2')\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Simple indexable tables using NamedTuple",
"version": "0.1.0.post1",
"project_urls": {
"Repository": "https://github.com/ajjackson/namedtuple-table"
},
"split_keywords": [
"python",
" table",
" index",
" simple",
" lightweight"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "fd39a01a15737228872aae472de6ba576d413b7c595c74411a93e11d479d40a2",
"md5": "4f3828ff5d8a8baaacd2a75b7d476e29",
"sha256": "f973c6f77ff9f185dcd51eee8bf613c59cb6121a4f732110af8b44b01cd70daf"
},
"downloads": -1,
"filename": "namedtuple_table-0.1.0.post1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4f3828ff5d8a8baaacd2a75b7d476e29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 4612,
"upload_time": "2025-07-15T14:33:50",
"upload_time_iso_8601": "2025-07-15T14:33:50.926908Z",
"url": "https://files.pythonhosted.org/packages/fd/39/a01a15737228872aae472de6ba576d413b7c595c74411a93e11d479d40a2/namedtuple_table-0.1.0.post1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "f7426a2c6905699356d33bfcfc8324910b563866d9fb344f71820a4e7bc0cc0a",
"md5": "a4934a3a7bcde700a19e454c88651e97",
"sha256": "a1eb65e35c2a71e18f2753b32c7690e9475bf85a0b10903d50128f8aa60ba562"
},
"downloads": -1,
"filename": "namedtuple_table-0.1.0.post1.tar.gz",
"has_sig": false,
"md5_digest": "a4934a3a7bcde700a19e454c88651e97",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 4912,
"upload_time": "2025-07-15T14:33:52",
"upload_time_iso_8601": "2025-07-15T14:33:52.124528Z",
"url": "https://files.pythonhosted.org/packages/f7/42/6a2c6905699356d33bfcfc8324910b563866d9fb344f71820a4e7bc0cc0a/namedtuple_table-0.1.0.post1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-15 14:33:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ajjackson",
"github_project": "namedtuple-table",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "namedtuple-table"
}