Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

namedtuple_table-0.1.0.post2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

namedtuple_table-0.1.0.post2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file namedtuple_table-0.1.0.post2.tar.gz.

File metadata

  • Download URL: namedtuple_table-0.1.0.post2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for namedtuple_table-0.1.0.post2.tar.gz
Algorithm Hash digest
SHA256 2fe88c380bca6e0130e070427d864c9b65efe8aea458b50db5486dd9e500b085
MD5 d2a6cca761eda2d98139c2b13c026469
BLAKE2b-256 2ffcbfdacefdd6e963cf0df0f7fb3f7ee791c037c63c612dcabf0d8f78e7c132

See more details on using hashes here.

Provenance

The following attestation bundles were made for namedtuple_table-0.1.0.post2.tar.gz:

Publisher: python-publish.yml on ajjackson/namedtuple-table

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file namedtuple_table-0.1.0.post2-py3-none-any.whl.

File metadata

File hashes

Hashes for namedtuple_table-0.1.0.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 edb578827d8feaef042e75e05b4435f75cd6f3338bafbf250bf240a6676fdf42
MD5 8703235429222067a65a36cbd465537e
BLAKE2b-256 56dc46e0291205c1ae30510153088e86cd7282c50dbd43e61a4186490f970a75

See more details on using hashes here.

Provenance

The following attestation bundles were made for namedtuple_table-0.1.0.post2-py3-none-any.whl:

Publisher: python-publish.yml on ajjackson/namedtuple-table

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page