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.tar.gz (4.9 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-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

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

Hashes for namedtuple_table-0.1.0.tar.gz
Algorithm Hash digest
SHA256 44e1694460da3218592aad262e80b37e3ed373e19fd178407851cac3bf286d58
MD5 b3cb31c4ee2af999b2dee644de36dda1
BLAKE2b-256 6f58a7f7699d127928deca7c9bfbecb9ce053f319afac2bd218b77f1dd98a9f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for namedtuple_table-0.1.0.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-py3-none-any.whl.

File metadata

File hashes

Hashes for namedtuple_table-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54fcf3403c7e079633bd3675409f66f1f5120d8a4791978b850af8e9d323090c
MD5 f6f8bb19b69e74f32c4f89e8b7aa81f5
BLAKE2b-256 607b44efecb8879e44903dd06132e5fe51f893363c2dadfa863ca0697577289d

See more details on using hashes here.

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

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