14.2.82. camcops_server.cc_modules.cc_tsv

camcops_server/cc_modules/cc_tsv.py


Copyright (C) 2012-2019 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.


Helper functions/classes for spreadsheet-style tab-separated value (TSV) exports.

class camcops_server.cc_modules.cc_tsv.TsvCollection[source]

A collection of camcops_server.cc_modules.cc_tsv.TsvPage pages (spreadsheets), like an Excel workbook.

add_page(page: camcops_server.cc_modules.cc_tsv.TsvPage) → None[source]

Adds a new page to our collection. If the new page has the same name as an existing page, rows from the new page are added to the existing page. Does nothing if the new page is empty.

add_pages(pages: List[camcops_server.cc_modules.cc_tsv.TsvPage]) → None[source]

Adds all pages to our collection, via add_page().

get_page_names() → List[str][source]

Return a list of the names of all our pages.

get_tsv_file(page_name: str) → str[source]

Returns a TSV file for a named page.

Raises:AssertionError if the named page does not exist
page_with_name(page_name: str) → Union[camcops_server.cc_modules.cc_tsv.TsvPage, NoneType][source]

Returns the page with the specific name, or None if no such page exists.

sort_headings_within_all_pages() → None[source]

Sort headings within each of our pages.

sort_pages() → None[source]

Sort our pages by their page name.

class camcops_server.cc_modules.cc_tsv.TsvPage(name: str, rows: List[Union[typing.Dict[str, typing.Any], collections.OrderedDict]])[source]

Represents a single TSV “spreadsheet”.

Parameters:
  • name – name for the whole sheet
  • rows – list of rows, where each row is a dictionary mapping column name to value
add_or_set_column(heading: str, values: List[Any]) → None[source]

Set the column labelled heading so it contains the values specified in values. The length of values must equal the number of rows that we already contain.

Raises:
  • AssertionError if the number of values doesn’t match
  • the number of existing rows
add_or_set_columns_from_page(other: camcops_server.cc_modules.cc_tsv.TsvPage) → None[source]

This function presupposes that self and other are two pages (“spreadsheets”) with matching rows.

It updates values or creates columns in self such that the values from all columns in other are written to the corresponding rows of self.

Raises:
  • AssertionError if the two pages (sheets) don’t have
  • the same number of rows.
add_or_set_value(heading: str, value: Any) → None[source]

If we contain only a single row, this function will set the value for a given column (heading) to value.

Raises:AssertionError if we don’t have exactly 1 row
add_rows_from_page(other: camcops_server.cc_modules.cc_tsv.TsvPage) → None[source]

Add all rows from other to self.

empty

Do we have zero rows?

get_tsv(dialect='excel-tab') → str[source]

Returns the entire page (sheet) as TSV: one header row and then lots of data rows.

For the dialect, see https://docs.python.org/3/library/csv.html#csv.excel_tab.

See also test code in docstring for camcops_server.cc_modules.cc_convert.tsv_from_query().

sort_headings() → None[source]

Sort our headings internally.