Simple indexable tables using NamedTuple
Project description
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.
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')
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file namedtuple_table-0.1.0.tar.gz.
File metadata
- Download URL: namedtuple_table-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44e1694460da3218592aad262e80b37e3ed373e19fd178407851cac3bf286d58
|
|
| MD5 |
b3cb31c4ee2af999b2dee644de36dda1
|
|
| BLAKE2b-256 |
6f58a7f7699d127928deca7c9bfbecb9ce053f319afac2bd218b77f1dd98a9f3
|
Provenance
The following attestation bundles were made for namedtuple_table-0.1.0.tar.gz:
Publisher:
python-publish.yml on ajjackson/namedtuple-table
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
namedtuple_table-0.1.0.tar.gz -
Subject digest:
44e1694460da3218592aad262e80b37e3ed373e19fd178407851cac3bf286d58 - Sigstore transparency entry: 274695174
- Sigstore integration time:
-
Permalink:
ajjackson/namedtuple-table@b56d43185ec57ecc7cb9ad5b6f75f3c339b19d25 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ajjackson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b56d43185ec57ecc7cb9ad5b6f75f3c339b19d25 -
Trigger Event:
release
-
Statement type:
File details
Details for the file namedtuple_table-0.1.0-py3-none-any.whl.
File metadata
- Download URL: namedtuple_table-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54fcf3403c7e079633bd3675409f66f1f5120d8a4791978b850af8e9d323090c
|
|
| MD5 |
f6f8bb19b69e74f32c4f89e8b7aa81f5
|
|
| BLAKE2b-256 |
607b44efecb8879e44903dd06132e5fe51f893363c2dadfa863ca0697577289d
|
Provenance
The following attestation bundles were made for namedtuple_table-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on ajjackson/namedtuple-table
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
namedtuple_table-0.1.0-py3-none-any.whl -
Subject digest:
54fcf3403c7e079633bd3675409f66f1f5120d8a4791978b850af8e9d323090c - Sigstore transparency entry: 274695181
- Sigstore integration time:
-
Permalink:
ajjackson/namedtuple-table@b56d43185ec57ecc7cb9ad5b6f75f3c339b19d25 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ajjackson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b56d43185ec57ecc7cb9ad5b6f75f3c339b19d25 -
Trigger Event:
release
-
Statement type: