Coverage for common/formatting.py: 25%
12 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/common/formatting.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26**Ancillary formatting functions.**
28"""
30from typing import List, Tuple
31from operator import itemgetter
34# =============================================================================
35# Ancillary functions
36# =============================================================================
39def print_record_counts(counts: List[Tuple[str, int]]) -> None:
40 """
41 Prints (to stdout) record counts for tables, firstly in alphabetical
42 order of table name, then in numerical order of record count.
44 Args:
45 counts: list of ``table_name, n_record`` tuples
46 """
47 alphabetical = sorted(counts, key=itemgetter(0))
48 numerical = sorted(counts, key=itemgetter(1))
49 print("\n-- ALPHABETICALLY\n")
50 for t, n in alphabetical:
51 print(f"{t}: {n} records")
52 print("\n-- NUMERICALLY\n")
53 for t, n in numerical:
54 print(f"{n} records in {t}")
55 print()