reconstruction.spreadsheets

Subclasses of DictWriter and DictReader that parse plaintext as JSON strings, allowing for basic type parsing and fields that are dictionaries or lists. The reader also supports units and comment lines.

class reconstruction.spreadsheets.JsonReader(*args, **kwargs)[source]

Bases: object

Reader for a .tsv file that supports units and json-coded values. Units are denoted with a fieldname in the format ‘name (units)’ e.g. “flux standard deviation (units.mmol / units.g / units.h)”. Fields whose names start with an underscore are removed from self._fieldnames, and discarded from each row during iteration.

The first argument should be a file-like reader open in text mode.

By default, dialect=CSV_DIALECT, which is excel-tab.

_decode_row(row_dict)[source]

Decode a DictReader row.

NOTE: Each returned row contains unicode/str keys and values.

Parameters:

row_dict (dict[str, str])

Return type:

dict[str, Any]

property dialect: excel_tab
property fieldnames: list[str]
property line_num: int
class reconstruction.spreadsheets.JsonWriter(*args, **kwargs)[source]

Bases: DictWriter

Writer for a .tsv file to be read by JsonReader. This writes a header with quotes and dict rows in TSV format, JSON encoding, and UTF-8 encoding.

NOTE: The caller needs to remove units from the dict values and add them to the fieldnames. JsonWriter does not handle units.

The first argument should be a file-like writer open in text mode.

By default, dialect=CSV_DIALECT, which is excel-tab.

_dict_to_list(rowdict)[source]
writeheader()[source]
reconstruction.spreadsheets.array_to_list(value)[source]
reconstruction.spreadsheets.comment_line(line)[source]
Parameters:

line (str)

Return type:

bool

reconstruction.spreadsheets.read_tsv(filename)[source]

Read an entire .tsv file using JsonReader and skip comment lines.

Parameters:

filename (str)

Return type:

list[dict[str, Any]]

reconstruction.spreadsheets.tsv_reader(filename)[source]

A context manager that opens a TSV JsonReader on the given filename with an input filter to skip comment lines.

Parameters:

filename (str)

Return type:

Iterator[JsonReader]

reconstruction.spreadsheets.tsv_writer(filename, fieldnames)[source]

A context manager that opens a TSV JsonWriter on the given filename. Just call its writerow() and writerows() methods.

Parameters:
Return type:

Iterator[JsonWriter]