flexablelib 2.0.21__tar.gz → 2.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flexablelib-2.0.21 → flexablelib-2.1.1}/CHANGELOG.md +37 -0
- {flexablelib-2.0.21/flexablelib.egg-info → flexablelib-2.1.1}/PKG-INFO +10 -1
- {flexablelib-2.0.21 → flexablelib-2.1.1}/README.md +9 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/table.py +244 -71
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/writers.py +2 -5
- {flexablelib-2.0.21 → flexablelib-2.1.1/flexablelib.egg-info}/PKG-INFO +10 -1
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib.egg-info/SOURCES.txt +2 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/package.json +1 -1
- flexablelib-2.1.1/tests/test_v210_rows_copy.py +94 -0
- flexablelib-2.1.1/tests/test_v211_fixes.py +301 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/LICENSE +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/MANIFEST.in +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/__init__.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/_speedups.c +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/_width_tables.h +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/cells.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/colors.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/columns.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/fast.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/merges.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/options.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/py.typed +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/readers.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/style_compositions.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/table_strings.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/text_width.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib/utils.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/pytest.ini +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/setup.cfg +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/setup.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/style_examples.md +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/conftest.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/helpers.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_cells.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_columns.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_excel.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_issues.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_merge_edge_matrix.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_merges.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_readme_examples.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_style_line_widths.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_styles.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_text_width.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_upstream_issues.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v2021_fixes.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v2110_fixes.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v213_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v214_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v215_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v216_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v217_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v218_features.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v219_coverage.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tests/test_v219_fixes.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tools/benchmark.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tools/generate_width_tables.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tools/readme_gallery.py +0 -0
- {flexablelib-2.0.21 → flexablelib-2.1.1}/tools/text_to_png.py +0 -0
|
@@ -2,6 +2,43 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.1.1] - 2026-11-04
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
- ``paginate()`` and ``stream()`` dropped most of the table's settings:
|
|
9
|
+
``float_format``, ``int_format``, ``custom_format``, ``leading_zeros``,
|
|
10
|
+
``str_align``/``int_align``/``float_align``/``bool_align``,
|
|
11
|
+
``column_colors``, ``max_table_width``, ``show_empty_rows`` and border
|
|
12
|
+
texts, among others. They now render like ``compose()``.
|
|
13
|
+
- ``paginate()`` and ``stream()`` re-parsed text cells, so ``'1e3'`` printed
|
|
14
|
+
as ``1000.0``. Values are kept as stored.
|
|
15
|
+
- ``paginate()`` ignored ``sort_by`` and ``row_filter``, and with
|
|
16
|
+
``show_empty_rows=False`` a page could lose visible rows. Pages now hold
|
|
17
|
+
the rows in display order, the index keeps counting across pages, and
|
|
18
|
+
``row_colors``, ``cell_colors``, ``cell_valign``, ``color_rule``,
|
|
19
|
+
``zebra``, ``footer_rows``, dividers and merges follow their rows (a merge
|
|
20
|
+
split by a page break is dropped).
|
|
21
|
+
- ``stream()`` now honours ``row_filter``, ``row_colors``, ``cell_colors``,
|
|
22
|
+
``color_rule``, dividers and merges. ``sort_by`` is still not applied while
|
|
23
|
+
streaming (documented).
|
|
24
|
+
- ``color_rule`` received another row's value after ``sort_by`` or
|
|
25
|
+
``row_filter`` reordered the body, so it painted the wrong cells.
|
|
26
|
+
- Setting ``index_title`` (even to its current value) shifted the column
|
|
27
|
+
alignments and widths by one column.
|
|
28
|
+
|
|
29
|
+
## [2.1.0] - 2026-11-03
|
|
30
|
+
|
|
31
|
+
### Changed (breaking)
|
|
32
|
+
- ``Table.rows`` returns a copy (new outer list and new row lists) with
|
|
33
|
+
``None`` in empty or missing cells, the same values ``to_records`` gives;
|
|
34
|
+
editing it no longer changes the table. Code that edited ``t.rows`` in place
|
|
35
|
+
should use ``add_row`` and the new ``set_cell(row, column, value)``.
|
|
36
|
+
|
|
37
|
+
### Added
|
|
38
|
+
- ``Table.set_cell(row, column, value)``: replace one cell by row position and
|
|
39
|
+
column position or header (``table.missing`` empties it). It keeps the row
|
|
40
|
+
and column stores in sync, which editing ``t.rows`` never did.
|
|
41
|
+
|
|
5
42
|
## [2.0.21] - 2026-11-03
|
|
6
43
|
|
|
7
44
|
### Fixed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.1.1
|
|
4
4
|
Summary: Fast, dependency-free terminal tables for Python: 56 border styles, ANSI colors, cell merges, decimal alignment, width fitting, and CSV/Markdown/HTML/Excel/LaTeX I/O.
|
|
5
5
|
Home-page: https://github.com/Opus-Perpetuus/flexablelib
|
|
6
6
|
Author: Benjamin Ramirez
|
|
@@ -439,6 +439,15 @@ table.parse_str_numbers = True
|
|
|
439
439
|
|
|
440
440
|
Values with leading zeros are left alone, so `'007'` stays a string.
|
|
441
441
|
|
|
442
|
+
`table.rows` is a copy of the data, with `None` in empty cells (the same values
|
|
443
|
+
as `to_records()`), so editing it does not change the table. Use `add_row` and
|
|
444
|
+
`set_cell` instead:
|
|
445
|
+
|
|
446
|
+
```python
|
|
447
|
+
table.set_cell(0, 'Uptime', 98.2) # row position, column position or header
|
|
448
|
+
table.set_cell(1, 2, table.missing) # empty a cell
|
|
449
|
+
```
|
|
450
|
+
|
|
442
451
|
## Merging cells
|
|
443
452
|
|
|
444
453
|
Render a rectangular block as a single cell, across columns, down rows, or
|
|
@@ -374,6 +374,15 @@ table.parse_str_numbers = True
|
|
|
374
374
|
|
|
375
375
|
Values with leading zeros are left alone, so `'007'` stays a string.
|
|
376
376
|
|
|
377
|
+
`table.rows` is a copy of the data, with `None` in empty cells (the same values
|
|
378
|
+
as `to_records()`), so editing it does not change the table. Use `add_row` and
|
|
379
|
+
`set_cell` instead:
|
|
380
|
+
|
|
381
|
+
```python
|
|
382
|
+
table.set_cell(0, 'Uptime', 98.2) # row position, column position or header
|
|
383
|
+
table.set_cell(1, 2, table.missing) # empty a cell
|
|
384
|
+
```
|
|
385
|
+
|
|
377
386
|
## Merging cells
|
|
378
387
|
|
|
379
388
|
Render a rectangular block as a single cell, across columns, down rows, or
|
|
@@ -850,6 +850,9 @@ class Table(object):
|
|
|
850
850
|
# Filled during compose so filter/sort and empty-row hiding share
|
|
851
851
|
# the same row indices for the current render.
|
|
852
852
|
self.__compose_empty_row_indexes = None
|
|
853
|
+
# Stored row behind each display row of the current render, or None
|
|
854
|
+
# when no filter/sort reordered them. color_rule reads values by it.
|
|
855
|
+
self.__compose_row_order = None
|
|
853
856
|
# +--------------------------+ STYLE +---------------------------+
|
|
854
857
|
# HEADER STYLES: None, l, u, t, c
|
|
855
858
|
# None: unmodified
|
|
@@ -1153,9 +1156,22 @@ class Table(object):
|
|
|
1153
1156
|
@property
|
|
1154
1157
|
def rows(self) -> List[list]:
|
|
1155
1158
|
"""
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
+
A copy of the table's data as rows.
|
|
1160
|
+
|
|
1161
|
+
Each call returns a new outer list with new row lists, so changing it
|
|
1162
|
+
never changes the table; use :meth:`add_row` and :meth:`set_cell` for
|
|
1163
|
+
that. Empty or missing cells come back as ``None``, the same value
|
|
1164
|
+
:meth:`to_records` and the exports use.
|
|
1165
|
+
"""
|
|
1166
|
+
width = len(self.__headers)
|
|
1167
|
+
copied = []
|
|
1168
|
+
for row in self.__rows:
|
|
1169
|
+
values = [None if isinstance(cell, ValuePlacer) else cell
|
|
1170
|
+
for cell in row]
|
|
1171
|
+
if len(values) < width:
|
|
1172
|
+
values.extend([None] * (width - len(values)))
|
|
1173
|
+
copied.append(values)
|
|
1174
|
+
return copied
|
|
1159
1175
|
|
|
1160
1176
|
@property
|
|
1161
1177
|
def internal_rows(self) -> List[list]:
|
|
@@ -2132,10 +2148,10 @@ class Table(object):
|
|
|
2132
2148
|
@index_title.setter
|
|
2133
2149
|
def index_title(self, value):
|
|
2134
2150
|
label = I_COL_TIT if value is None else str(value)
|
|
2135
|
-
new_title = IndexColumnTitle(label)
|
|
2136
2151
|
old_title = self.__index_title
|
|
2137
|
-
if
|
|
2152
|
+
if label == str(old_title):
|
|
2138
2153
|
return
|
|
2154
|
+
new_title = IndexColumnTitle(label)
|
|
2139
2155
|
for mapping in (
|
|
2140
2156
|
self.__columns_with_i,
|
|
2141
2157
|
self.__cell_types_with_i,
|
|
@@ -2147,7 +2163,16 @@ class Table(object):
|
|
|
2147
2163
|
self.__semi_processed_columns_with_i,
|
|
2148
2164
|
):
|
|
2149
2165
|
if old_title in mapping:
|
|
2150
|
-
|
|
2166
|
+
# Rebuild in place, keeping the index key where it was. A
|
|
2167
|
+
# pop-and-reinsert moved it to the end, and every structure
|
|
2168
|
+
# read positionally (alignments, widths) then shifted by one
|
|
2169
|
+
# column: renaming the index realigned the data columns.
|
|
2170
|
+
items = [
|
|
2171
|
+
(new_title if key is old_title else key, value)
|
|
2172
|
+
for key, value in mapping.items()
|
|
2173
|
+
]
|
|
2174
|
+
mapping.clear()
|
|
2175
|
+
mapping.update(items)
|
|
2151
2176
|
if self.__headers_with_i and self.__headers_with_i[0] is old_title:
|
|
2152
2177
|
self.__headers_with_i[0] = new_title
|
|
2153
2178
|
self.__index_title = new_title
|
|
@@ -3058,6 +3083,13 @@ class Table(object):
|
|
|
3058
3083
|
except IndexError:
|
|
3059
3084
|
return None
|
|
3060
3085
|
source = self.__columns_with_i if self.__show_index else self.__columns
|
|
3086
|
+
# row_i counts display rows. After a sort or filter they are no longer
|
|
3087
|
+
# in stored order, and the rule was handed another row's value.
|
|
3088
|
+
order = self.__compose_row_order
|
|
3089
|
+
if order is not None:
|
|
3090
|
+
if not 0 <= row_i < len(order):
|
|
3091
|
+
return None
|
|
3092
|
+
row_i = order[row_i]
|
|
3061
3093
|
try:
|
|
3062
3094
|
return source[title][row_i]
|
|
3063
3095
|
except (KeyError, IndexError):
|
|
@@ -3334,6 +3366,57 @@ class Table(object):
|
|
|
3334
3366
|
self.__transpose_row_to_columns(data)
|
|
3335
3367
|
self.__adjust_columns_to_row_count(rows_added_before=True)
|
|
3336
3368
|
|
|
3369
|
+
def set_cell(self, row: int, column: Union[int, str], value) -> None:
|
|
3370
|
+
"""
|
|
3371
|
+
Replace the value of one cell.
|
|
3372
|
+
|
|
3373
|
+
``row`` is the row position (negative counts from the end) and
|
|
3374
|
+
``column`` is the column position or its header. Pass
|
|
3375
|
+
:attr:`missing` to empty the cell.
|
|
3376
|
+
|
|
3377
|
+
Example::
|
|
3378
|
+
|
|
3379
|
+
table = Table(rows=[['api', 99.9]], headers=['Service', 'Uptime'])
|
|
3380
|
+
table.set_cell(0, 'Uptime', 98.2)
|
|
3381
|
+
"""
|
|
3382
|
+
if isinstance(row, bool) or not isinstance(row, int):
|
|
3383
|
+
raise TypeError(
|
|
3384
|
+
f'set_cell: row must be an int, got {type(row).__name__}'
|
|
3385
|
+
)
|
|
3386
|
+
row_count = len(self.__rows)
|
|
3387
|
+
if not -row_count <= row < row_count:
|
|
3388
|
+
raise IndexError(
|
|
3389
|
+
f'set_cell: row {row} out of range for {row_count} rows'
|
|
3390
|
+
)
|
|
3391
|
+
row %= row_count
|
|
3392
|
+
if isinstance(column, str):
|
|
3393
|
+
if column not in self.__headers:
|
|
3394
|
+
raise KeyError(f'set_cell: no column named {column!r}')
|
|
3395
|
+
column_i = self.__headers.index(column)
|
|
3396
|
+
elif isinstance(column, bool) or not isinstance(column, int):
|
|
3397
|
+
raise TypeError(
|
|
3398
|
+
'set_cell: column must be an int or a header, '
|
|
3399
|
+
f'got {type(column).__name__}'
|
|
3400
|
+
)
|
|
3401
|
+
else:
|
|
3402
|
+
column_count = len(self.__headers)
|
|
3403
|
+
if not -column_count <= column < column_count:
|
|
3404
|
+
raise IndexError(
|
|
3405
|
+
f'set_cell: column {column} out of range for '
|
|
3406
|
+
f'{column_count} columns'
|
|
3407
|
+
)
|
|
3408
|
+
column_i = column % column_count
|
|
3409
|
+
if self.__parse_str_numbers and isinstance(value, str):
|
|
3410
|
+
from .readers import parse_value
|
|
3411
|
+
value = parse_value(value, True)
|
|
3412
|
+
header = self.__headers[column_i]
|
|
3413
|
+
# Both orientations hold the same data and both are read while
|
|
3414
|
+
# rendering, so all four stores change together.
|
|
3415
|
+
self.__rows[row][column_i] = value
|
|
3416
|
+
self.__rows_with_i[row][column_i + 1] = value
|
|
3417
|
+
self.__columns[header][row] = value
|
|
3418
|
+
self.__columns_with_i[header][row] = value
|
|
3419
|
+
|
|
3337
3420
|
def __adjust_headers_to_data_length(self, data: Union[list, tuple]) -> None:
|
|
3338
3421
|
if data is None:
|
|
3339
3422
|
headers_to_add = 0
|
|
@@ -3495,6 +3578,7 @@ class Table(object):
|
|
|
3495
3578
|
# Cleared every render; set after the display rows are materialised
|
|
3496
3579
|
# so filter/sort and empty-row hiding agree on the same indices.
|
|
3497
3580
|
self.__compose_empty_row_indexes = None
|
|
3581
|
+
self.__compose_row_order = None
|
|
3498
3582
|
if len(self.__columns) != 0:
|
|
3499
3583
|
# Refuse to render into a space where the result would be
|
|
3500
3584
|
# illegible, if the caller asked to be told -- issue #14.
|
|
@@ -4407,6 +4491,7 @@ class Table(object):
|
|
|
4407
4491
|
if old_i in empty_stored
|
|
4408
4492
|
]
|
|
4409
4493
|
if order != list(range(len(rows))):
|
|
4494
|
+
self.__compose_row_order = list(order)
|
|
4410
4495
|
rows = [rows[i] for i in order]
|
|
4411
4496
|
rows_with_i = [rows_with_i[i] for i in order]
|
|
4412
4497
|
# Index was consumed in stored order; renumber visible rows so
|
|
@@ -5832,6 +5917,11 @@ class Table(object):
|
|
|
5832
5917
|
Requires headers (or existing columns) so widths can be measured
|
|
5833
5918
|
before the first streamed row.
|
|
5834
5919
|
|
|
5920
|
+
Lines look as ``compose()`` would draw them: formats, alignment,
|
|
5921
|
+
colours, ``row_filter``, dividers and merges carry over. ``sort_by``
|
|
5922
|
+
does not -- a sorted row could land above lines already yielded --
|
|
5923
|
+
so rows stream in the order they arrive.
|
|
5924
|
+
|
|
5835
5925
|
Parameters
|
|
5836
5926
|
----------
|
|
5837
5927
|
rows : iterable
|
|
@@ -5923,42 +6013,81 @@ class Table(object):
|
|
|
5923
6013
|
n += 1
|
|
5924
6014
|
return n
|
|
5925
6015
|
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
|
|
5938
|
-
|
|
5939
|
-
|
|
5940
|
-
|
|
5941
|
-
|
|
5942
|
-
|
|
5943
|
-
|
|
6016
|
+
# Display settings a derived table (stream, paginate) takes from its
|
|
6017
|
+
# parent. Each is global or keyed by column, so it means the same thing
|
|
6018
|
+
# on any subset of the rows. Row-keyed settings are handled by the caller.
|
|
6019
|
+
_DISPLAY_SETTINGS = (
|
|
6020
|
+
'title', 'caption', 'show_headers', 'show_index', 'header_color',
|
|
6021
|
+
'border_color', 'use_colors', 'footer_color', 'cell_padding',
|
|
6022
|
+
'overflow', 'auto_wrap', 'valign', 'col_valign', 'panel', 'shadow',
|
|
6023
|
+
'str_align', 'int_align', 'float_align', 'bool_align', 'col_alignment',
|
|
6024
|
+
'header_align', 'max_width', 'max_table_width', 'table_align',
|
|
6025
|
+
'hide_columns', 'index_title', 'roman_index', 'number_columns',
|
|
6026
|
+
'strip_cells', 'hrules', 'vrules', 'index_start', 'index_step',
|
|
6027
|
+
'leading_zeros', 'float_format', 'int_format', 'custom_format',
|
|
6028
|
+
'column_min_width', 'column_max_width', 'column_colors',
|
|
6029
|
+
'expand_to_window', 'justify', 'suppress_empty', 'show_margin',
|
|
6030
|
+
'show_empty_rows', 'show_empty_columns', 'parse_str_numbers',
|
|
6031
|
+
'too_narrow_message',
|
|
6032
|
+
)
|
|
6033
|
+
|
|
6034
|
+
def __copy_display_settings(self, out):
|
|
6035
|
+
"""Give ``out`` this table's row-independent display settings."""
|
|
6036
|
+
for attr in self._DISPLAY_SETTINGS:
|
|
6037
|
+
try:
|
|
6038
|
+
setattr(out, attr, getattr(self, attr))
|
|
6039
|
+
except (AttributeError, TypeError, ValueError):
|
|
6040
|
+
pass
|
|
6041
|
+
out.__border_texts = [dict(entry) for entry in self.__border_texts]
|
|
6042
|
+
|
|
6043
|
+
def __stored_row(self, row_i, headers):
|
|
6044
|
+
"""One stored row with gaps turned into the missing value."""
|
|
6045
|
+
row = []
|
|
6046
|
+
for header in headers:
|
|
6047
|
+
col = self.__columns.get(header, [])
|
|
6048
|
+
cell = col[row_i] if row_i < len(col) else self.__missing_value
|
|
6049
|
+
if isinstance(cell, ValuePlacer):
|
|
6050
|
+
cell = self.__missing_value
|
|
6051
|
+
row.append(cell)
|
|
6052
|
+
return row
|
|
6053
|
+
|
|
6054
|
+
def __derived_table(self, rows, headers):
|
|
6055
|
+
"""A table of ``rows`` styled like this one, values kept as stored."""
|
|
6056
|
+
# parse_numbers=False: the cells are already typed. Parsing them again
|
|
6057
|
+
# turned the text '1e3' into 1000.0 and '007' into 7 on every page.
|
|
6058
|
+
return type(self).from_records(
|
|
5944
6059
|
rows,
|
|
5945
6060
|
headers=headers if headers else None,
|
|
6061
|
+
parse_numbers=False,
|
|
5946
6062
|
style_name=self.style_name,
|
|
5947
6063
|
missing_val=self.__missing_value,
|
|
5948
6064
|
)
|
|
5949
|
-
|
|
5950
|
-
|
|
5951
|
-
|
|
5952
|
-
|
|
5953
|
-
|
|
5954
|
-
|
|
5955
|
-
|
|
5956
|
-
|
|
5957
|
-
|
|
5958
|
-
|
|
5959
|
-
|
|
5960
|
-
|
|
5961
|
-
|
|
6065
|
+
|
|
6066
|
+
def __display_clone(self, include_rows=True):
|
|
6067
|
+
"""Display copy used by ``stream``: same rows, same settings."""
|
|
6068
|
+
headers = list(self.headers)
|
|
6069
|
+
rows = []
|
|
6070
|
+
if include_rows:
|
|
6071
|
+
rows = [
|
|
6072
|
+
self.__stored_row(row_i, headers)
|
|
6073
|
+
for row_i in range(self.__real_row_count)
|
|
6074
|
+
]
|
|
6075
|
+
out = self.__derived_table(rows, headers)
|
|
6076
|
+
self.__copy_display_settings(out)
|
|
6077
|
+
out.zebra = self.__zebra
|
|
6078
|
+
out.footer_rows = self.__footer_rows
|
|
6079
|
+
if include_rows:
|
|
6080
|
+
# The clone holds the same rows in the same order, so row-keyed
|
|
6081
|
+
# settings carry over as they are.
|
|
6082
|
+
out.row_colors = self.__row_colors
|
|
6083
|
+
out.cell_colors = self.__cell_colors
|
|
6084
|
+
out.cell_valign = self.__cell_valign
|
|
6085
|
+
out.color_rule = self.__color_rule
|
|
6086
|
+
# A filter only drops rows, so lines already streamed stay put.
|
|
6087
|
+
# sort_by is left out: it would move them.
|
|
6088
|
+
out.row_filter = self.__row_filter
|
|
6089
|
+
out.__dividers = set(self.__dividers)
|
|
6090
|
+
out.__merged_regions = list(self.__merged_regions)
|
|
5962
6091
|
return out
|
|
5963
6092
|
|
|
5964
6093
|
def __lookup_width_map(self, spec, header, index):
|
|
@@ -6029,44 +6158,96 @@ class Table(object):
|
|
|
6029
6158
|
headers repeat on every page; with False, only the first page shows
|
|
6030
6159
|
them. ``page_label`` adds ``page k/n`` to each page title.
|
|
6031
6160
|
Returns a list of strings. ``page_length`` must be >= 1.
|
|
6161
|
+
|
|
6162
|
+
Pages hold the rows in the order ``compose()`` shows them --
|
|
6163
|
+
``sort_by`` and ``row_filter`` applied, empty rows left out when
|
|
6164
|
+
``show_empty_rows`` is off -- with the same formats and colours. The
|
|
6165
|
+
index keeps counting across pages, and row-keyed settings
|
|
6166
|
+
(``row_colors``, ``cell_colors``, ``cell_valign``, ``color_rule``,
|
|
6167
|
+
``zebra``, ``footer_rows``, dividers, merges) follow their rows; a
|
|
6168
|
+
merge split by a page break is dropped.
|
|
6032
6169
|
Inspired by PrettyTable ``paginate`` / go-pretty.
|
|
6033
6170
|
"""
|
|
6034
6171
|
page_length = int(page_length)
|
|
6035
6172
|
if page_length < 1:
|
|
6036
6173
|
raise ValueError('page_length must be >= 1')
|
|
6037
6174
|
headers = list(self.headers)
|
|
6038
|
-
|
|
6175
|
+
stored = [
|
|
6176
|
+
self.__stored_row(row_i, headers)
|
|
6177
|
+
for row_i in range(self.__real_row_count)
|
|
6178
|
+
]
|
|
6179
|
+
# Display positions, as compose() numbers them for row_colors and
|
|
6180
|
+
# friends, and the stored row shown at each one.
|
|
6181
|
+
order = self.__display_row_order(stored)
|
|
6182
|
+
hidden = (
|
|
6183
|
+
set() if self.__show_empty_rows else set(self.__empty_row_indexes)
|
|
6184
|
+
)
|
|
6185
|
+
positions = [p for p, row_i in enumerate(order) if row_i not in hidden]
|
|
6186
|
+
n = len(positions)
|
|
6039
6187
|
if n == 0:
|
|
6040
6188
|
return [self.compose()]
|
|
6041
6189
|
total_pages = (n + page_length - 1) // page_length
|
|
6190
|
+
footer_from = len(order) - int(self.__footer_rows or 0)
|
|
6042
6191
|
pages = []
|
|
6043
6192
|
for page_num, start in enumerate(range(0, n, page_length), start=1):
|
|
6044
|
-
|
|
6045
|
-
|
|
6046
|
-
|
|
6047
|
-
|
|
6048
|
-
|
|
6049
|
-
|
|
6050
|
-
|
|
6051
|
-
slice_rows.append(row)
|
|
6052
|
-
page = type(self).from_records(
|
|
6053
|
-
slice_rows, headers=headers, style_name=self.style_name,
|
|
6054
|
-
missing_val=self.__missing_value,
|
|
6193
|
+
visible = positions[start:start + page_length]
|
|
6194
|
+
# Hidden empty rows travel with the page they fall inside (the
|
|
6195
|
+
# first and last pages take the ones before and after every
|
|
6196
|
+
# visible row), so the page types its columns as the table does.
|
|
6197
|
+
first_p = 0 if start == 0 else visible[0]
|
|
6198
|
+
last_p = (
|
|
6199
|
+
len(order) - 1 if start + page_length >= n else visible[-1]
|
|
6055
6200
|
)
|
|
6056
|
-
|
|
6057
|
-
for
|
|
6058
|
-
|
|
6059
|
-
|
|
6060
|
-
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6064
|
-
|
|
6065
|
-
|
|
6066
|
-
|
|
6067
|
-
|
|
6068
|
-
|
|
6069
|
-
|
|
6201
|
+
page_positions = list(range(first_p, last_p + 1))
|
|
6202
|
+
local = {p: j for j, p in enumerate(page_positions)}
|
|
6203
|
+
page = self.__derived_table(
|
|
6204
|
+
[stored[order[p]] for p in page_positions], headers
|
|
6205
|
+
)
|
|
6206
|
+
self.__copy_display_settings(page)
|
|
6207
|
+
page.sort_by = None
|
|
6208
|
+
page.row_filter = None
|
|
6209
|
+
# The index carries on from the previous page.
|
|
6210
|
+
page.index_start = self.__i_start + start * self.__i_step
|
|
6211
|
+
page.footer_rows = sum(1 for p in page_positions if p >= footer_from)
|
|
6212
|
+
zebra = self.__zebra
|
|
6213
|
+
if zebra and page_positions[0] % 2:
|
|
6214
|
+
# Stripes are by row parity; an odd first row swaps them.
|
|
6215
|
+
if isinstance(zebra, (list, tuple)) and len(zebra) >= 2:
|
|
6216
|
+
zebra = (zebra[1], zebra[0])
|
|
6217
|
+
else:
|
|
6218
|
+
zebra = (zebra, None)
|
|
6219
|
+
page.zebra = zebra
|
|
6220
|
+
page.row_colors = {
|
|
6221
|
+
local[r]: spec for r, spec in self.__row_colors.items()
|
|
6222
|
+
if r in local
|
|
6223
|
+
}
|
|
6224
|
+
for name, mapping in (('cell_colors', self.__cell_colors),
|
|
6225
|
+
('cell_valign', self.__cell_valign)):
|
|
6226
|
+
remapped = {
|
|
6227
|
+
(local[key[0]], key[1]): spec
|
|
6228
|
+
for key, spec in mapping.items()
|
|
6229
|
+
if isinstance(key, tuple) and key[0] in local
|
|
6230
|
+
}
|
|
6231
|
+
if remapped:
|
|
6232
|
+
setattr(page, name, remapped)
|
|
6233
|
+
if self.__color_rule is not None:
|
|
6234
|
+
rule = self.__color_rule
|
|
6235
|
+
page.color_rule = (
|
|
6236
|
+
lambda value, row, column, _rule=rule, _pos=page_positions:
|
|
6237
|
+
_rule(value, _pos[row] if 0 <= row < len(_pos) else row,
|
|
6238
|
+
column)
|
|
6239
|
+
)
|
|
6240
|
+
last = visible[-1]
|
|
6241
|
+
for after in self.__dividers:
|
|
6242
|
+
if after in local and after != last:
|
|
6243
|
+
page.add_divider(local[after])
|
|
6244
|
+
for region in self.__merged_regions:
|
|
6245
|
+
first, final = region.first_row, region.last_row
|
|
6246
|
+
if (first in local and final in local
|
|
6247
|
+
and local[final] - local[first] == final - first):
|
|
6248
|
+
page.__merged_regions.append(region._replace(
|
|
6249
|
+
first_row=local[first], last_row=local[final],
|
|
6250
|
+
))
|
|
6070
6251
|
if not freeze_header and page_num > 1:
|
|
6071
6252
|
page.show_headers = False
|
|
6072
6253
|
if page_label:
|
|
@@ -6075,14 +6256,6 @@ class Table(object):
|
|
|
6075
6256
|
page.title = (
|
|
6076
6257
|
'{0} ({1})'.format(existing, label) if existing else label
|
|
6077
6258
|
)
|
|
6078
|
-
if self.__cell_colors:
|
|
6079
|
-
# Remap absolute row indexes into the page's local indexes.
|
|
6080
|
-
remapped = {}
|
|
6081
|
-
for (r, c), spec in self.__cell_colors.items():
|
|
6082
|
-
if isinstance(r, int) and start <= r < end:
|
|
6083
|
-
remapped[(r - start, c)] = spec
|
|
6084
|
-
if remapped:
|
|
6085
|
-
page.cell_colors = remapped
|
|
6086
6259
|
pages.append(page.compose())
|
|
6087
6260
|
return pages
|
|
6088
6261
|
|
|
@@ -69,12 +69,9 @@ def _table_data(table, include_index: bool):
|
|
|
69
69
|
into the CSV field, the Markdown cell or the record. It leaves here as
|
|
70
70
|
``None``, which every writer already turns into an empty cell.
|
|
71
71
|
"""
|
|
72
|
-
missing = table.missing
|
|
73
72
|
headers = [str(header) for header in table.headers]
|
|
74
|
-
rows
|
|
75
|
-
|
|
76
|
-
for row in table.rows
|
|
77
|
-
]
|
|
73
|
+
# ``table.rows`` is already a copy with ``None`` in the gaps.
|
|
74
|
+
rows = table.rows
|
|
78
75
|
if include_index:
|
|
79
76
|
# The stored index column holds a counter object that only the
|
|
80
77
|
# renderer knows how to read, so exports wrote its ``repr`` in every
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.1.1
|
|
4
4
|
Summary: Fast, dependency-free terminal tables for Python: 56 border styles, ANSI colors, cell merges, decimal alignment, width fitting, and CSV/Markdown/HTML/Excel/LaTeX I/O.
|
|
5
5
|
Home-page: https://github.com/Opus-Perpetuus/flexablelib
|
|
6
6
|
Author: Benjamin Ramirez
|
|
@@ -439,6 +439,15 @@ table.parse_str_numbers = True
|
|
|
439
439
|
|
|
440
440
|
Values with leading zeros are left alone, so `'007'` stays a string.
|
|
441
441
|
|
|
442
|
+
`table.rows` is a copy of the data, with `None` in empty cells (the same values
|
|
443
|
+
as `to_records()`), so editing it does not change the table. Use `add_row` and
|
|
444
|
+
`set_cell` instead:
|
|
445
|
+
|
|
446
|
+
```python
|
|
447
|
+
table.set_cell(0, 'Uptime', 98.2) # row position, column position or header
|
|
448
|
+
table.set_cell(1, 2, table.missing) # empty a cell
|
|
449
|
+
```
|
|
450
|
+
|
|
442
451
|
## Merging cells
|
|
443
452
|
|
|
444
453
|
Render a rectangular block as a single cell, across columns, down rows, or
|
|
@@ -45,7 +45,9 @@ tests/test_text_width.py
|
|
|
45
45
|
tests/test_two_pass_measurement.py
|
|
46
46
|
tests/test_upstream_issues.py
|
|
47
47
|
tests/test_v2021_fixes.py
|
|
48
|
+
tests/test_v210_rows_copy.py
|
|
48
49
|
tests/test_v2110_fixes.py
|
|
50
|
+
tests/test_v211_fixes.py
|
|
49
51
|
tests/test_v213_features.py
|
|
50
52
|
tests/test_v214_features.py
|
|
51
53
|
tests/test_v215_features.py
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""2.1.0: ``Table.rows`` is a copy with ``None`` in the gaps; ``set_cell``."""
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from flexablelib import Table
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _table():
|
|
8
|
+
table = Table(rows=[['api', 99.9], ['auth']], headers=['Service', 'Uptime'])
|
|
9
|
+
table.add_row()
|
|
10
|
+
table.add_column('Region', ['us', table.missing, 'eu'])
|
|
11
|
+
return table
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_rows_is_a_new_list_each_call():
|
|
15
|
+
table = _table()
|
|
16
|
+
first, second = table.rows, table.rows
|
|
17
|
+
assert first == second
|
|
18
|
+
assert first is not second
|
|
19
|
+
assert all(a is not b for a, b in zip(first, second))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_mutating_rows_does_not_change_the_table():
|
|
23
|
+
table = Table(rows=[['a', 1], ['b', 2]], headers=['k', 'v'])
|
|
24
|
+
before = str(table)
|
|
25
|
+
rows = table.rows
|
|
26
|
+
rows[0][1] = 999
|
|
27
|
+
rows.append(['c', 3])
|
|
28
|
+
del rows[1]
|
|
29
|
+
assert table.rows == [['a', 1], ['b', 2]]
|
|
30
|
+
assert str(table) == before
|
|
31
|
+
assert table.row_count == 2
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_empty_and_missing_cells_are_none():
|
|
35
|
+
table = _table()
|
|
36
|
+
table.add_row(['x', table.missing, 'mx'])
|
|
37
|
+
rows = table.rows
|
|
38
|
+
assert rows[1] == ['auth', None, None]
|
|
39
|
+
assert rows[2][:2] == [None, None]
|
|
40
|
+
assert rows[3] == ['x', None, 'mx']
|
|
41
|
+
assert 'ValuePlacer' not in repr(rows)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_rows_match_records_and_exports():
|
|
45
|
+
table = _table()
|
|
46
|
+
records = table.to_records()
|
|
47
|
+
assert [list(r.values()) for r in records] == table.rows
|
|
48
|
+
assert [list(r.keys()) for r in records][0] == table.headers
|
|
49
|
+
csv_lines = table.to_csv().splitlines()[1:]
|
|
50
|
+
expected = [','.join('' if v is None else str(v) for v in row)
|
|
51
|
+
for row in table.rows]
|
|
52
|
+
assert csv_lines == expected
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_set_cell_updates_render_and_rows():
|
|
56
|
+
table = Table(rows=[['api', 99.9], ['auth', 98.0]],
|
|
57
|
+
headers=['Service', 'Uptime'])
|
|
58
|
+
table.set_cell(0, 'Uptime', 12.5)
|
|
59
|
+
table.set_cell(-1, 0, 'login')
|
|
60
|
+
assert table.rows == [['api', 12.5], ['login', 98.0]]
|
|
61
|
+
assert table.columns['Uptime'] == [12.5, 98.0]
|
|
62
|
+
out = str(table)
|
|
63
|
+
assert '12.5' in out and 'login' in out and 'auth' not in out
|
|
64
|
+
table.show_index = True
|
|
65
|
+
assert 'login' in str(table)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_set_cell_missing_empties_cell():
|
|
69
|
+
table = Table(rows=[[1, 2]], headers=['a', 'b'])
|
|
70
|
+
table.set_cell(0, 'b', table.missing)
|
|
71
|
+
assert table.rows == [[1, None]]
|
|
72
|
+
assert table.to_records() == [{'a': 1, 'b': None}]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_set_cell_parses_numbers_when_enabled():
|
|
76
|
+
table = Table(rows=[['x', 1]], headers=['k', 'v'])
|
|
77
|
+
table.parse_str_numbers = True
|
|
78
|
+
table.set_cell(0, 'v', '42')
|
|
79
|
+
assert table.rows == [['x', 42]]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@pytest.mark.parametrize('row, column, error', [
|
|
83
|
+
(5, 0, IndexError),
|
|
84
|
+
(0, 9, IndexError),
|
|
85
|
+
(0, 'nope', KeyError),
|
|
86
|
+
(1.0, 0, TypeError),
|
|
87
|
+
(True, 0, TypeError),
|
|
88
|
+
(0, 1.5, TypeError),
|
|
89
|
+
])
|
|
90
|
+
def test_set_cell_rejects_bad_coordinates(row, column, error):
|
|
91
|
+
table = Table(rows=[[1, 2]], headers=['a', 'b'])
|
|
92
|
+
with pytest.raises(error):
|
|
93
|
+
table.set_cell(row, column, 0)
|
|
94
|
+
assert table.rows == [[1, 2]]
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"""2.1.1: paginate/stream keep the parent's look; color_rule after sort; index_title."""
|
|
2
|
+
import io
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from flexablelib import Table
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
ROWS = [[3, 'c', 1.5], [1, 'a', 2.25], [None, None, None], [2, 'b', 10.0]]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _table():
|
|
14
|
+
return Table(rows=[list(r) for r in ROWS], headers=['n', 's', 'f'])
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _colour(table):
|
|
18
|
+
table.use_colors = True
|
|
19
|
+
return table
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
SETTINGS = {
|
|
23
|
+
'float_format': lambda t: setattr(t, 'float_format', '.1f'),
|
|
24
|
+
'int_format': lambda t: setattr(t, 'int_format', '{:>4}'),
|
|
25
|
+
'custom_format': lambda t: setattr(t, 'custom_format', {'s': lambda v: str(v).upper()}),
|
|
26
|
+
'leading_zeros': lambda t: setattr(t, 'leading_zeros', 3),
|
|
27
|
+
'str_align': lambda t: setattr(t, 'str_align', 'r'),
|
|
28
|
+
'show_index': lambda t: setattr(t, 'show_index', True),
|
|
29
|
+
'index_title': lambda t: (setattr(t, 'show_index', True),
|
|
30
|
+
setattr(t, 'index_title', '#')),
|
|
31
|
+
'missing_value': lambda t: setattr(t, 'missing_value', '-'),
|
|
32
|
+
'hide_empty_rows': lambda t: setattr(t, 'show_empty_rows', False),
|
|
33
|
+
'column_colors': lambda t: setattr(_colour(t), 'column_colors', {'s': 'green'}),
|
|
34
|
+
'row_colors': lambda t: setattr(_colour(t), 'row_colors', {1: 'red'}),
|
|
35
|
+
'cell_colors': lambda t: setattr(_colour(t), 'cell_colors', {(1, 's'): 'blue'}),
|
|
36
|
+
'color_rule': lambda t: setattr(
|
|
37
|
+
_colour(t), 'color_rule', lambda v, r, c: 'red' if v == 'a' else None),
|
|
38
|
+
'zebra': lambda t: setattr(_colour(t), 'zebra', 'dim'),
|
|
39
|
+
'footer': lambda t: (setattr(_colour(t), 'footer_rows', 1),
|
|
40
|
+
setattr(t, 'footer_color', 'bold')),
|
|
41
|
+
'divider': lambda t: t.add_divider(0),
|
|
42
|
+
'merge': lambda t: t.merge_cells(0, 0, 0, 1),
|
|
43
|
+
'border_text': lambda t: t.border_text('T', where='top'),
|
|
44
|
+
'row_filter': lambda t: setattr(t, 'row_filter', lambda r: r[0] != 1),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@pytest.mark.parametrize('name', sorted(SETTINGS))
|
|
49
|
+
def test_single_page_equals_the_table(name):
|
|
50
|
+
table = _table()
|
|
51
|
+
SETTINGS[name](table)
|
|
52
|
+
assert table.paginate(100) == [table.compose()]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@pytest.mark.parametrize('name', sorted(SETTINGS))
|
|
56
|
+
def test_stream_of_nothing_equals_the_table(name):
|
|
57
|
+
table = _table()
|
|
58
|
+
SETTINGS[name](table)
|
|
59
|
+
assert '\n'.join(table.stream([])) == table.compose()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_single_page_equals_the_table_when_sorted():
|
|
63
|
+
table = _table()
|
|
64
|
+
table.sort_by = 'n'
|
|
65
|
+
assert table.paginate(100) == [table.compose()]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_pages_keep_text_that_looks_like_a_number():
|
|
69
|
+
table = Table(rows=[['007', '1e3']], headers=['zip', 'code'])
|
|
70
|
+
for out in (table.paginate()[0], '\n'.join(table.stream([]))):
|
|
71
|
+
assert '007' in out and '1e3' in out
|
|
72
|
+
assert '1000.0' not in out
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_pages_follow_sort_and_filter():
|
|
76
|
+
table = Table(rows=[[5], [1], [4], [2], [3]], headers=['n'])
|
|
77
|
+
table.sort_by = 'n'
|
|
78
|
+
table.row_filter = lambda row: row[0] != 4
|
|
79
|
+
pages = table.paginate(2)
|
|
80
|
+
assert len(pages) == 2
|
|
81
|
+
assert '| 1 |' in pages[0] and '| 2 |' in pages[0]
|
|
82
|
+
assert '| 3 |' in pages[1] and '| 5 |' in pages[1]
|
|
83
|
+
assert '4' not in pages[1].split('+===+')[1]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_pages_skip_hidden_empty_rows():
|
|
87
|
+
table = Table(rows=[['a'], [None], ['b'], ['c']], headers=['x'])
|
|
88
|
+
table.show_empty_rows = False
|
|
89
|
+
pages = table.paginate(2)
|
|
90
|
+
assert len(pages) == 2
|
|
91
|
+
assert 'a' in pages[0] and 'b' in pages[0]
|
|
92
|
+
assert 'c' in pages[1]
|
|
93
|
+
assert 'None' not in ''.join(pages)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_index_keeps_counting_across_pages():
|
|
97
|
+
table = Table(rows=[[c] for c in 'abcde'], headers=['x'])
|
|
98
|
+
table.show_index = True
|
|
99
|
+
table.index_start = 1
|
|
100
|
+
pages = table.paginate(2)
|
|
101
|
+
assert '| 3 | c |' in pages[1]
|
|
102
|
+
assert '| 5 | e |' in pages[2]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_row_keyed_colours_follow_their_rows():
|
|
106
|
+
table = _colour(Table(rows=[[c] for c in 'abcd'], headers=['x']))
|
|
107
|
+
table.row_colors = {2: 'red'}
|
|
108
|
+
table.cell_colors = {(3, 'x'): 'blue'}
|
|
109
|
+
first, second = table.paginate(2)
|
|
110
|
+
assert '\x1b[' not in first
|
|
111
|
+
assert '\x1b[31m c ' in second
|
|
112
|
+
assert '\x1b[34m d ' in second
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_color_rule_sees_parent_row_numbers():
|
|
116
|
+
seen = []
|
|
117
|
+
table = _colour(Table(rows=[[c] for c in 'abcd'], headers=['x']))
|
|
118
|
+
table.color_rule = lambda v, r, c: seen.append((v, r))
|
|
119
|
+
table.paginate(2)
|
|
120
|
+
assert ('c', 2) in seen and ('d', 3) in seen
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_zebra_parity_continues_on_odd_page_breaks():
|
|
124
|
+
table = _colour(Table(rows=[[c] for c in 'abcd'], headers=['x']))
|
|
125
|
+
table.zebra = 'dim'
|
|
126
|
+
pages = table.paginate(3)
|
|
127
|
+
# Row 'b' (1) is striped on page one, row 'd' (3) on page two.
|
|
128
|
+
assert '\x1b[2m b ' in pages[0]
|
|
129
|
+
assert '\x1b[2m d ' in pages[1]
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def test_footer_band_only_on_the_last_rows():
|
|
133
|
+
table = _colour(Table(rows=[[c] for c in 'abcd'], headers=['x']))
|
|
134
|
+
table.footer_rows = 1
|
|
135
|
+
table.footer_color = 'bold'
|
|
136
|
+
first, second = table.paginate(2)
|
|
137
|
+
assert '\x1b[1m' not in first
|
|
138
|
+
assert '\x1b[1m d ' in second
|
|
139
|
+
assert '\x1b[1m c ' not in second
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_merges_and_dividers_follow_their_rows():
|
|
143
|
+
table = Table(rows=[[i, 'x'] for i in range(4)], headers=['a', 'b'])
|
|
144
|
+
table.merge_cells(2, 0, 3, 0, value='M') # inside page two
|
|
145
|
+
table.merge_cells(1, 1, 2, 1, value='S') # split by the break: dropped
|
|
146
|
+
table.add_divider(2)
|
|
147
|
+
first, second = table.paginate(2)
|
|
148
|
+
assert 'S' not in first and 'S' not in second
|
|
149
|
+
assert 'M' in second
|
|
150
|
+
assert len(table.paginate(2)[1].splitlines()) > len(first.splitlines())
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_color_rule_gets_the_displayed_value_after_sort():
|
|
154
|
+
seen = []
|
|
155
|
+
table = _colour(Table(rows=[['c'], ['a'], ['b']], headers=['x']))
|
|
156
|
+
table.sort_by = 'x'
|
|
157
|
+
table.color_rule = lambda v, r, c: seen.append((r, v))
|
|
158
|
+
table.compose()
|
|
159
|
+
assert sorted(set(seen)) == [(0, 'a'), (1, 'b'), (2, 'c')]
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_color_rule_gets_the_displayed_value_after_filter():
|
|
163
|
+
table = _colour(Table(rows=[['a', 1], ['b', 2], ['c', 3]], headers=['x', 'n']))
|
|
164
|
+
table.row_filter = lambda row: row[0] != 'a'
|
|
165
|
+
table.color_rule = lambda v, r, c: 'red' if c == 'n' and v == 2 else None
|
|
166
|
+
out = table.compose()
|
|
167
|
+
assert '\x1b[31m 2 ' in out
|
|
168
|
+
assert '\x1b[31m 3 ' not in out
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def test_index_title_keeps_column_alignment():
|
|
172
|
+
table = Table(rows=[[1, 'xx', 1.25], [22, 'y', 10.5]], headers=['n', 's', 'f'])
|
|
173
|
+
table.show_index = True
|
|
174
|
+
before = table.compose().splitlines()[3:]
|
|
175
|
+
table.index_title = '#'
|
|
176
|
+
after = table.compose().splitlines()[3:]
|
|
177
|
+
assert [line.replace('#', 'i') for line in after] == before
|
|
178
|
+
table.index_title = 'i'
|
|
179
|
+
assert table.compose().splitlines()[3:] == before
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def test_setting_the_same_index_title_changes_nothing():
|
|
183
|
+
table = _table()
|
|
184
|
+
table.show_index = True
|
|
185
|
+
before = table.compose()
|
|
186
|
+
table.index_title = table.index_title
|
|
187
|
+
assert table.compose() == before
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# Uncovered public paths ---------------------------------------------------
|
|
191
|
+
|
|
192
|
+
def test_stream_honours_column_max_width():
|
|
193
|
+
table = Table(rows=[['aaaaaaaaaaaa', 1]], headers=['h', 'k'])
|
|
194
|
+
table.column_max_width = 4
|
|
195
|
+
lines = list(table.stream([['bbbbbbbbbb', 2]]))
|
|
196
|
+
assert len({len(line) for line in lines if line.startswith('|')}) == 1
|
|
197
|
+
assert any('b...' in line for line in lines)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_paginate_of_empty_table_is_one_page():
|
|
201
|
+
table = Table(headers=['a'])
|
|
202
|
+
assert table.paginate(3) == [table.compose()]
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_row_colors_from_a_sequence_and_none():
|
|
206
|
+
table = Table(rows=[[1], [2]], headers=['a'])
|
|
207
|
+
table.row_colors = ['red', 'green']
|
|
208
|
+
assert table.row_colors == {0: 'red', 1: 'green'}
|
|
209
|
+
table.row_colors = None
|
|
210
|
+
assert table.row_colors == {}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def test_cell_colors_validation():
|
|
214
|
+
table = Table(rows=[[1]], headers=['a'])
|
|
215
|
+
table.cell_colors = {(0, 'a'): 'red'}
|
|
216
|
+
assert table.cell_colors == {(0, 'a'): 'red'}
|
|
217
|
+
table.cell_colors = None
|
|
218
|
+
assert table.cell_colors == {}
|
|
219
|
+
with pytest.raises(TypeError):
|
|
220
|
+
table.cell_colors = {0: 'red'}
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def test_leading_zeros_none_and_negative():
|
|
224
|
+
table = Table(rows=[[7]], headers=['a'])
|
|
225
|
+
table.leading_zeros = 3
|
|
226
|
+
assert '007' in table.compose()
|
|
227
|
+
table.leading_zeros = None
|
|
228
|
+
assert table.leading_zeros is None
|
|
229
|
+
assert '007' not in table.compose()
|
|
230
|
+
with pytest.raises(ValueError):
|
|
231
|
+
table.leading_zeros = -1
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_vrules_true_and_invalid():
|
|
235
|
+
table = Table(rows=[[1]], headers=['a'])
|
|
236
|
+
table.vrules = True
|
|
237
|
+
assert table.vrules == 'all'
|
|
238
|
+
with pytest.raises(ValueError):
|
|
239
|
+
table.vrules = 'bogus'
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_add_divider_defaults_and_validation():
|
|
243
|
+
Table().add_divider() # no rows: nothing to divide, no error
|
|
244
|
+
table = Table(rows=[[1], [2], [3]], headers=['a'])
|
|
245
|
+
table.style_name = 'grid_eheader'
|
|
246
|
+
with pytest.raises(ValueError):
|
|
247
|
+
table.add_divider(-1)
|
|
248
|
+
table.add_divider()
|
|
249
|
+
plain = Table(rows=[[1], [2], [3]], headers=['a'])
|
|
250
|
+
assert table.compose() == plain.compose()
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def test_empty_columns_i_ignores_the_index_column():
|
|
254
|
+
table = Table(rows=[[1, None], [2, None]], headers=['a', 'b'])
|
|
255
|
+
plain = table.empty_columns_i
|
|
256
|
+
table.show_index = True
|
|
257
|
+
assert table.empty_columns_i == plain
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def test_columns_dict_skips_non_iterables():
|
|
261
|
+
table = Table(columns={'x': [1, 2], 'y': 5})
|
|
262
|
+
assert table.headers == ['x']
|
|
263
|
+
assert table.rows == [[1], [2]]
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def test_from_markdown_file_objects_and_odd_input():
|
|
267
|
+
text = '| a | b |\n|---|---|\n| 1 | é |\n'
|
|
268
|
+
assert Table.from_markdown(io.StringIO(text)).rows == [[1, 'é']]
|
|
269
|
+
assert Table.from_markdown(io.BytesIO(text.encode('utf-8'))).rows == [[1, 'é']]
|
|
270
|
+
titled = Table.from_markdown('Report\n' + text)
|
|
271
|
+
assert titled.headers == ['a', 'b']
|
|
272
|
+
empty = Table.from_markdown('no table here\n')
|
|
273
|
+
assert empty.headers == [] and empty.rows == []
|
|
274
|
+
no_sep = Table.from_markdown('| a | b |\n| 1 | 2 |\n')
|
|
275
|
+
assert no_sep.headers == ['a', 'b'] and no_sep.rows == [[1, 2]]
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def test_from_html_file_object_and_empty_table():
|
|
279
|
+
markup = '<table><tr><th>a</th></tr><tr><td>1</td></tr></table>'
|
|
280
|
+
table = Table.from_html(io.StringIO(markup))
|
|
281
|
+
assert table.headers == ['a'] and table.rows == [[1]]
|
|
282
|
+
empty = Table.from_html('<table></table>')
|
|
283
|
+
assert empty.headers == [] and empty.rows == []
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def test_from_csv_path(tmp_path):
|
|
287
|
+
path = tmp_path / 'data.csv'
|
|
288
|
+
path.write_text('a,b\n1,中文\n', encoding='utf-8')
|
|
289
|
+
for source in (str(path), path):
|
|
290
|
+
table = Table.from_csv(source)
|
|
291
|
+
assert table.headers == ['a', 'b']
|
|
292
|
+
assert table.rows == [[1, '中文']]
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def test_from_records_with_nothing():
|
|
296
|
+
assert Table.from_records([]).headers == []
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def test_exports_of_a_table_without_headers():
|
|
300
|
+
assert Table().to_markdown() == ''
|
|
301
|
+
assert Table().to_rst() == ''
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|