flexablelib 2.0.20__tar.gz → 2.1.0__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.20 → flexablelib-2.1.0}/CHANGELOG.md +43 -0
- {flexablelib-2.0.20/flexablelib.egg-info → flexablelib-2.1.0}/PKG-INFO +10 -1
- {flexablelib-2.0.20 → flexablelib-2.1.0}/README.md +9 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/cells.py +9 -2
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/merges.py +20 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/style_compositions.py +1 -1
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/table.py +97 -9
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/writers.py +28 -7
- {flexablelib-2.0.20 → flexablelib-2.1.0/flexablelib.egg-info}/PKG-INFO +10 -1
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib.egg-info/SOURCES.txt +3 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/package.json +1 -1
- flexablelib-2.1.0/tests/test_style_line_widths.py +46 -0
- flexablelib-2.1.0/tests/test_v2021_fixes.py +262 -0
- flexablelib-2.1.0/tests/test_v210_rows_copy.py +94 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/LICENSE +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/MANIFEST.in +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/__init__.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/_speedups.c +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/_width_tables.h +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/colors.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/columns.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/fast.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/options.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/py.typed +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/readers.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/table_strings.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/text_width.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib/utils.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/pytest.ini +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/setup.cfg +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/setup.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/style_examples.md +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/conftest.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/helpers.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_cells.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_columns.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_excel.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_issues.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_merge_edge_matrix.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_merges.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_readme_examples.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_styles.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_text_width.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_upstream_issues.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v2110_fixes.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v213_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v214_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v215_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v216_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v217_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v218_features.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v219_coverage.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tests/test_v219_fixes.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tools/benchmark.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tools/generate_width_tables.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tools/readme_gallery.py +0 -0
- {flexablelib-2.0.20 → flexablelib-2.1.0}/tools/text_to_png.py +0 -0
|
@@ -2,6 +2,49 @@
|
|
|
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.0] - 2026-11-03
|
|
6
|
+
|
|
7
|
+
### Changed (breaking)
|
|
8
|
+
- ``Table.rows`` returns a copy (new outer list and new row lists) with
|
|
9
|
+
``None`` in empty or missing cells, the same values ``to_records`` gives;
|
|
10
|
+
editing it no longer changes the table. Code that edited ``t.rows`` in place
|
|
11
|
+
should use ``add_row`` and the new ``set_cell(row, column, value)``.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- ``Table.set_cell(row, column, value)``: replace one cell by row position and
|
|
15
|
+
column position or header (``table.missing`` empties it). It keeps the row
|
|
16
|
+
and column stores in sync, which editing ``t.rows`` never did.
|
|
17
|
+
|
|
18
|
+
## [2.0.21] - 2026-11-03
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
- Text exports (``to_csv``, ``to_markdown``, ``to_rst``, ``to_asciidoc``,
|
|
22
|
+
``to_latex``, ``to_html``) and ``to_records`` wrote
|
|
23
|
+
``<flexablelib.utils.ValuePlacer object at ...>`` for gaps -- short rows
|
|
24
|
+
padded by the table, NaN/NULL from the readers, ``table.missing``. Gaps
|
|
25
|
+
now export as empty cells (``None`` in records).
|
|
26
|
+
- ``include_index=True`` wrote ``<...IndexCounter object at ...>`` in every
|
|
27
|
+
row, and left the index out when ``show_index`` was off. The index is now
|
|
28
|
+
numbered like the rendered one (``index_start``, ``index_step``,
|
|
29
|
+
``index_title``, ``roman_index``).
|
|
30
|
+
- A float column whose numbers have no decimal point (``1e+20``) rendered
|
|
31
|
+
one character wider than its border.
|
|
32
|
+
- ``vrules='none'`` kept the corners of the horizontal rules, which made
|
|
33
|
+
them two columns wider than the rows.
|
|
34
|
+
- ``transpose()`` / ``rotate()`` showed gaps blank instead of the missing
|
|
35
|
+
value and exported the sentinel's repr; the missing value is carried over.
|
|
36
|
+
- ``merge_cells`` with a non-integer coordinate (a float, or a
|
|
37
|
+
``(row, column)`` pair) raises a ``TypeError`` naming the argument instead
|
|
38
|
+
of a comparison error.
|
|
39
|
+
- ``plain`` style: the blank line under the header was one column shorter
|
|
40
|
+
than the rows (no junction between columns), so ``compose()`` output was
|
|
41
|
+
not rectangular.
|
|
42
|
+
|
|
43
|
+
### Tests
|
|
44
|
+
- Every style renders lines of equal terminal width with CJK / emoji
|
|
45
|
+
headers and ``column_max_width``. Pins that ``*_eheader`` styles draw the
|
|
46
|
+
header row without column separators by design.
|
|
47
|
+
|
|
5
48
|
## [2.0.20] - 2026-11-02
|
|
6
49
|
|
|
7
50
|
### Fixed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.1.0
|
|
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
|
|
@@ -182,8 +182,15 @@ def fljust(string: str,
|
|
|
182
182
|
right = pad_to_width(right_string, right_width, 'l', fill_char)
|
|
183
183
|
|
|
184
184
|
# A number with no point leaves the point's own column blank, so it is
|
|
185
|
-
# still exactly as wide as the ones that have one.
|
|
186
|
-
|
|
185
|
+
# still exactly as wide as the ones that have one. When no number in the
|
|
186
|
+
# column has a point (a column of exponentials such as ``1e+20``) that
|
|
187
|
+
# column is zero wide, and padding it anyway made the cell one character
|
|
188
|
+
# wider than the column and pushed the right border out.
|
|
189
|
+
if not point:
|
|
190
|
+
point_width = (sides_widths[POINT_WIDTH_I]
|
|
191
|
+
if len(sides_widths) > 2 else 1)
|
|
192
|
+
point = fill_char * point_width
|
|
193
|
+
return ''.join([left, point, right])
|
|
187
194
|
|
|
188
195
|
|
|
189
196
|
def __fill_to(aligned: str, cell_length: int, fill_char: str) -> str:
|
|
@@ -16,6 +16,7 @@ The consequence is that a merged region never widens the table. Content longer
|
|
|
16
16
|
than the span it was given is truncated, not accommodated.
|
|
17
17
|
"""
|
|
18
18
|
|
|
19
|
+
import operator
|
|
19
20
|
import re
|
|
20
21
|
from typing import List, NamedTuple, Optional
|
|
21
22
|
|
|
@@ -99,6 +100,25 @@ def normalise(first_row, first_column, last_row, last_column, value, align,
|
|
|
99
100
|
single cell, coordinates outside the table, an unknown alignment, or an
|
|
100
101
|
overlap with a merge already in place.
|
|
101
102
|
"""
|
|
103
|
+
# Coordinates are indexes. ``operator.index`` accepts ints and integer
|
|
104
|
+
# types such as numpy's, and turns anything else -- a float, or a
|
|
105
|
+
# ``(row, column)`` pair given where a single number goes -- into a
|
|
106
|
+
# TypeError naming the argument, instead of a comparison error from
|
|
107
|
+
# deep inside the validation or the renderer.
|
|
108
|
+
coordinates = []
|
|
109
|
+
for name, coordinate in (('first_row', first_row),
|
|
110
|
+
('first_column', first_column),
|
|
111
|
+
('last_row', last_row),
|
|
112
|
+
('last_column', last_column)):
|
|
113
|
+
try:
|
|
114
|
+
coordinates.append(operator.index(coordinate))
|
|
115
|
+
except TypeError:
|
|
116
|
+
raise TypeError(
|
|
117
|
+
f'{name} must be an integer, not '
|
|
118
|
+
f'{type(coordinate).__name__} ({coordinate!r})'
|
|
119
|
+
) from None
|
|
120
|
+
first_row, first_column, last_row, last_column = coordinates
|
|
121
|
+
|
|
102
122
|
first_row, last_row = sorted((first_row, last_row))
|
|
103
123
|
first_column, last_column = sorted((first_column, last_column))
|
|
104
124
|
|
|
@@ -154,7 +154,7 @@ Compositions = namedtuple(
|
|
|
154
154
|
__style_compositions = Compositions(
|
|
155
155
|
plain=TableComposition(
|
|
156
156
|
superior_header_line=None,
|
|
157
|
-
inferior_header_line=SeparatorLine(None, INVISIBLE_SEPARATOR,
|
|
157
|
+
inferior_header_line=SeparatorLine(None, INVISIBLE_SEPARATOR, INVISIBLE_SEPARATOR, None),
|
|
158
158
|
superior_header_line_no_header=None,
|
|
159
159
|
table_body_line=None,
|
|
160
160
|
table_end_line=None,
|
|
@@ -1153,9 +1153,22 @@ class Table(object):
|
|
|
1153
1153
|
@property
|
|
1154
1154
|
def rows(self) -> List[list]:
|
|
1155
1155
|
"""
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1156
|
+
A copy of the table's data as rows.
|
|
1157
|
+
|
|
1158
|
+
Each call returns a new outer list with new row lists, so changing it
|
|
1159
|
+
never changes the table; use :meth:`add_row` and :meth:`set_cell` for
|
|
1160
|
+
that. Empty or missing cells come back as ``None``, the same value
|
|
1161
|
+
:meth:`to_records` and the exports use.
|
|
1162
|
+
"""
|
|
1163
|
+
width = len(self.__headers)
|
|
1164
|
+
copied = []
|
|
1165
|
+
for row in self.__rows:
|
|
1166
|
+
values = [None if isinstance(cell, ValuePlacer) else cell
|
|
1167
|
+
for cell in row]
|
|
1168
|
+
if len(values) < width:
|
|
1169
|
+
values.extend([None] * (width - len(values)))
|
|
1170
|
+
copied.append(values)
|
|
1171
|
+
return copied
|
|
1159
1172
|
|
|
1160
1173
|
@property
|
|
1161
1174
|
def internal_rows(self) -> List[list]:
|
|
@@ -2357,17 +2370,30 @@ class Table(object):
|
|
|
2357
2370
|
fill = sep.middle if sep.middle not in (None, '') else INVISIBLE_SEPARATOR
|
|
2358
2371
|
return SeparatorLine(sep.left, fill, fill, sep.right)
|
|
2359
2372
|
|
|
2373
|
+
def drop_verticals(sep):
|
|
2374
|
+
"""
|
|
2375
|
+
Horizontal rule with no junctions and no corners (vrules none).
|
|
2376
|
+
|
|
2377
|
+
The rows lose their left and right borders in this mode, so a
|
|
2378
|
+
rule that kept its corners came out two columns wider than every
|
|
2379
|
+
row it framed.
|
|
2380
|
+
"""
|
|
2381
|
+
sep = drop_intersections(sep)
|
|
2382
|
+
if sep is None:
|
|
2383
|
+
return None
|
|
2384
|
+
return SeparatorLine(None, sep.middle, sep.intersection, None)
|
|
2385
|
+
|
|
2360
2386
|
if mode in ('none', 'n', 'off', 'false', '0'):
|
|
2361
2387
|
return {
|
|
2362
2388
|
'vertical_header_lines': invisible,
|
|
2363
2389
|
'vertical_table_body_lines': invisible,
|
|
2364
|
-
'superior_header_line':
|
|
2365
|
-
'inferior_header_line':
|
|
2366
|
-
'superior_header_line_no_header':
|
|
2390
|
+
'superior_header_line': drop_verticals(base.superior_header_line),
|
|
2391
|
+
'inferior_header_line': drop_verticals(base.inferior_header_line),
|
|
2392
|
+
'superior_header_line_no_header': drop_verticals(
|
|
2367
2393
|
base.superior_header_line_no_header
|
|
2368
2394
|
),
|
|
2369
|
-
'table_body_line':
|
|
2370
|
-
'table_end_line':
|
|
2395
|
+
'table_body_line': drop_verticals(base.table_body_line),
|
|
2396
|
+
'table_end_line': drop_verticals(base.table_end_line),
|
|
2371
2397
|
}
|
|
2372
2398
|
if mode in ('frame', 'f'):
|
|
2373
2399
|
def frame_only(sep):
|
|
@@ -3321,6 +3347,57 @@ class Table(object):
|
|
|
3321
3347
|
self.__transpose_row_to_columns(data)
|
|
3322
3348
|
self.__adjust_columns_to_row_count(rows_added_before=True)
|
|
3323
3349
|
|
|
3350
|
+
def set_cell(self, row: int, column: Union[int, str], value) -> None:
|
|
3351
|
+
"""
|
|
3352
|
+
Replace the value of one cell.
|
|
3353
|
+
|
|
3354
|
+
``row`` is the row position (negative counts from the end) and
|
|
3355
|
+
``column`` is the column position or its header. Pass
|
|
3356
|
+
:attr:`missing` to empty the cell.
|
|
3357
|
+
|
|
3358
|
+
Example::
|
|
3359
|
+
|
|
3360
|
+
table = Table(rows=[['api', 99.9]], headers=['Service', 'Uptime'])
|
|
3361
|
+
table.set_cell(0, 'Uptime', 98.2)
|
|
3362
|
+
"""
|
|
3363
|
+
if isinstance(row, bool) or not isinstance(row, int):
|
|
3364
|
+
raise TypeError(
|
|
3365
|
+
f'set_cell: row must be an int, got {type(row).__name__}'
|
|
3366
|
+
)
|
|
3367
|
+
row_count = len(self.__rows)
|
|
3368
|
+
if not -row_count <= row < row_count:
|
|
3369
|
+
raise IndexError(
|
|
3370
|
+
f'set_cell: row {row} out of range for {row_count} rows'
|
|
3371
|
+
)
|
|
3372
|
+
row %= row_count
|
|
3373
|
+
if isinstance(column, str):
|
|
3374
|
+
if column not in self.__headers:
|
|
3375
|
+
raise KeyError(f'set_cell: no column named {column!r}')
|
|
3376
|
+
column_i = self.__headers.index(column)
|
|
3377
|
+
elif isinstance(column, bool) or not isinstance(column, int):
|
|
3378
|
+
raise TypeError(
|
|
3379
|
+
'set_cell: column must be an int or a header, '
|
|
3380
|
+
f'got {type(column).__name__}'
|
|
3381
|
+
)
|
|
3382
|
+
else:
|
|
3383
|
+
column_count = len(self.__headers)
|
|
3384
|
+
if not -column_count <= column < column_count:
|
|
3385
|
+
raise IndexError(
|
|
3386
|
+
f'set_cell: column {column} out of range for '
|
|
3387
|
+
f'{column_count} columns'
|
|
3388
|
+
)
|
|
3389
|
+
column_i = column % column_count
|
|
3390
|
+
if self.__parse_str_numbers and isinstance(value, str):
|
|
3391
|
+
from .readers import parse_value
|
|
3392
|
+
value = parse_value(value, True)
|
|
3393
|
+
header = self.__headers[column_i]
|
|
3394
|
+
# Both orientations hold the same data and both are read while
|
|
3395
|
+
# rendering, so all four stores change together.
|
|
3396
|
+
self.__rows[row][column_i] = value
|
|
3397
|
+
self.__rows_with_i[row][column_i + 1] = value
|
|
3398
|
+
self.__columns[header][row] = value
|
|
3399
|
+
self.__columns_with_i[header][row] = value
|
|
3400
|
+
|
|
3324
3401
|
def __adjust_headers_to_data_length(self, data: Union[list, tuple]) -> None:
|
|
3325
3402
|
if data is None:
|
|
3326
3403
|
headers_to_add = 0
|
|
@@ -5988,9 +6065,20 @@ class Table(object):
|
|
|
5988
6065
|
for r in range(len(rows))]
|
|
5989
6066
|
)
|
|
5990
6067
|
style = options.pop('style_name', self.style_name)
|
|
6068
|
+
keep_missing = 'missing_val' not in options
|
|
6069
|
+
# Gaps carry this table's ``missing`` sentinel, which the new table
|
|
6070
|
+
# does not recognise: it rendered as a blank instead of the missing
|
|
6071
|
+
# value and the exports wrote its repr. Hand them over as the new
|
|
6072
|
+
# table's own sentinel.
|
|
5991
6073
|
out = type(self).from_records(
|
|
5992
|
-
|
|
6074
|
+
[], headers=new_headers, style_name=style, **options
|
|
5993
6075
|
)
|
|
6076
|
+
if keep_missing:
|
|
6077
|
+
out.missing_value = self.__missing_value
|
|
6078
|
+
placer = self.__value_placer
|
|
6079
|
+
for row in new_rows:
|
|
6080
|
+
out.add_row([out.missing if cell is placer else cell
|
|
6081
|
+
for cell in row])
|
|
5994
6082
|
return out
|
|
5995
6083
|
|
|
5996
6084
|
def rotate(self, **options):
|
|
@@ -27,6 +27,7 @@ from typing import Any, List, Optional, Sequence
|
|
|
27
27
|
from .fast import strip_ansi
|
|
28
28
|
from .colors import background_hex, foreground_hex, spec_to_css
|
|
29
29
|
from .options import FLT_FILTER, INT_FILTER
|
|
30
|
+
from .utils import to_roman
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
|
|
@@ -58,14 +59,34 @@ def _plain(value: Any) -> str:
|
|
|
58
59
|
|
|
59
60
|
|
|
60
61
|
def _table_data(table, include_index: bool):
|
|
61
|
-
"""
|
|
62
|
+
"""
|
|
63
|
+
(headers, rows) honouring the index setting.
|
|
64
|
+
|
|
65
|
+
Gaps hold the table's ``missing`` sentinel -- short rows padded by the
|
|
66
|
+
table, NaN and NULL from the readers, ``table.missing`` placed by hand.
|
|
67
|
+
It is an internal marker with no printable form, and every export used
|
|
68
|
+
to write its ``repr`` (``<flexablelib.utils.ValuePlacer object at ...>``)
|
|
69
|
+
into the CSV field, the Markdown cell or the record. It leaves here as
|
|
70
|
+
``None``, which every writer already turns into an empty cell.
|
|
71
|
+
"""
|
|
72
|
+
headers = [str(header) for header in table.headers]
|
|
73
|
+
# ``table.rows`` is already a copy with ``None`` in the gaps.
|
|
74
|
+
rows = table.rows
|
|
62
75
|
if include_index:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
rows
|
|
68
|
-
|
|
76
|
+
# The stored index column holds a counter object that only the
|
|
77
|
+
# renderer knows how to read, so exports wrote its ``repr`` in every
|
|
78
|
+
# row -- and left the column out altogether when ``show_index`` was
|
|
79
|
+
# off, although the Excel writer already shifted merges for it.
|
|
80
|
+
# Number the rows here the way the rendered index does.
|
|
81
|
+
start, step = table.index_start, table.index_step
|
|
82
|
+
roman = table.roman_index
|
|
83
|
+
labels = []
|
|
84
|
+
for row_i in range(len(rows)):
|
|
85
|
+
label = start + row_i * step
|
|
86
|
+
labels.append(to_roman(label) if roman else label)
|
|
87
|
+
headers = [str(table.index_title)] + headers
|
|
88
|
+
rows = [[label] + row for label, row in zip(labels, rows)]
|
|
89
|
+
return headers, rows
|
|
69
90
|
|
|
70
91
|
|
|
71
92
|
def _effective_region_color(table, region):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.1.0
|
|
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
|
|
@@ -39,10 +39,13 @@ tests/test_issues.py
|
|
|
39
39
|
tests/test_merge_edge_matrix.py
|
|
40
40
|
tests/test_merges.py
|
|
41
41
|
tests/test_readme_examples.py
|
|
42
|
+
tests/test_style_line_widths.py
|
|
42
43
|
tests/test_styles.py
|
|
43
44
|
tests/test_text_width.py
|
|
44
45
|
tests/test_two_pass_measurement.py
|
|
45
46
|
tests/test_upstream_issues.py
|
|
47
|
+
tests/test_v2021_fixes.py
|
|
48
|
+
tests/test_v210_rows_copy.py
|
|
46
49
|
tests/test_v2110_fixes.py
|
|
47
50
|
tests/test_v213_features.py
|
|
48
51
|
tests/test_v214_features.py
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Every rendered line has the same terminal width, in every style.
|
|
2
|
+
|
|
3
|
+
Regression for ``plain``: its header separator line was one column short
|
|
4
|
+
because it had no junction between columns. Also pins that wide (CJK /
|
|
5
|
+
emoji) headers and ``column_max_width`` keep all lines aligned. Note that
|
|
6
|
+
``*_eheader`` styles intentionally draw the header row without column
|
|
7
|
+
separators (documented design, not a bug).
|
|
8
|
+
"""
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
from flexablelib import Table
|
|
12
|
+
from flexablelib.style_compositions import __style_compositions as catalogue
|
|
13
|
+
from flexablelib.text_width import visible_width
|
|
14
|
+
|
|
15
|
+
CASES = [
|
|
16
|
+
(['名前', 'n'], [['日本語テキスト', 1.25], ['😀😀', -3]], {'名前': 4}),
|
|
17
|
+
(['名前', 'n'], [['日本語テキスト', 1.25], ['😀😀', -3]], None),
|
|
18
|
+
(['x', 'yy'], [['abc', 1], ['d', 2]], None),
|
|
19
|
+
(['東京', '😀値'], [['東京タワー展望台', 'x'], ['a', '👍 ok']], 3),
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.mark.parametrize('style', catalogue._fields)
|
|
24
|
+
@pytest.mark.parametrize('headers,rows,max_width', CASES)
|
|
25
|
+
def test_all_lines_same_width(style, headers, rows, max_width):
|
|
26
|
+
table = Table(rows=rows, headers=headers)
|
|
27
|
+
table.style_name = style
|
|
28
|
+
if max_width is not None:
|
|
29
|
+
table.column_max_width = max_width
|
|
30
|
+
lines = table.compose().split('\n')
|
|
31
|
+
widths = {visible_width(line) for line in lines}
|
|
32
|
+
assert len(widths) == 1, (style, [visible_width(l) for l in lines])
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_plain_header_separator_spans_table():
|
|
36
|
+
table = Table(rows=[['abc', 1], ['d', 2]], headers=['x', 'yy'])
|
|
37
|
+
table.style_name = 'plain'
|
|
38
|
+
lines = table.compose().split('\n')
|
|
39
|
+
assert lines[1] == ' ' * len(lines[0])
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_eheader_header_row_has_no_column_separator_by_design():
|
|
43
|
+
table = Table(rows=[['a', 'b']], headers=['x', 'y'])
|
|
44
|
+
table.style_name = 'grid_eheader'
|
|
45
|
+
assert table.compose().split('\n')[:3] == [
|
|
46
|
+
'+-------+', '| x y |', '+===+===+']
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"""Fixes and coverage added for 2.0.21."""
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from flexablelib import Table
|
|
5
|
+
from flexablelib.text_width import strip_ansi, visible_width
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _widths(text):
|
|
9
|
+
return {visible_width(line) for line in strip_ansi(text).split('\n')}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# --- exponent-only float columns ---------------------------------------------
|
|
13
|
+
|
|
14
|
+
@pytest.mark.parametrize('values', [
|
|
15
|
+
[1e20], [1e20, -2e30], [1e16, 12345], [1.5e-7, 2e-9],
|
|
16
|
+
])
|
|
17
|
+
def test_float_column_without_any_point_stays_rectangular(values):
|
|
18
|
+
t = Table(rows=[[v] for v in values], headers=['x'])
|
|
19
|
+
t.style_name = 'grid'
|
|
20
|
+
out = t.compose()
|
|
21
|
+
assert len(_widths(out)) == 1, out
|
|
22
|
+
for v in values:
|
|
23
|
+
assert str(v) in out
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_exponent_still_aligns_with_decimals_in_the_same_column():
|
|
27
|
+
t = Table(rows=[[1e20], [1.5]], headers=['x'])
|
|
28
|
+
lines = t.compose().split('\n')
|
|
29
|
+
assert len(_widths('\n'.join(lines))) == 1
|
|
30
|
+
body = [line for line in lines if '1e+20' in line or '1.5' in line]
|
|
31
|
+
# The integer part of 1.5 lines up with the end of the mantissa block.
|
|
32
|
+
assert body[0].index('1e+20') + len('1e+20') - 1 == body[1].index('1.5')
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_exponent_column_next_to_other_columns_with_index():
|
|
36
|
+
t = Table(rows=[[1e20, 'a'], [3e21, 'bb']], headers=['n', 's'])
|
|
37
|
+
t.show_index = True
|
|
38
|
+
t.style_name = 'rounded'
|
|
39
|
+
assert len(_widths(t.compose())) == 1
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# --- vrules='none' -----------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
@pytest.mark.parametrize('style', [
|
|
45
|
+
'grid', 'rounded', 'pretty_grid', 'dashes', 'ascii_rounded',
|
|
46
|
+
'bold_header', 'pretty_columns', 'markdown',
|
|
47
|
+
])
|
|
48
|
+
def test_vrules_none_rules_are_as_wide_as_the_rows(style):
|
|
49
|
+
t = Table(rows=[[1, 'ab'], [2.5, '漢字']], headers=['A', 'B'])
|
|
50
|
+
t.style_name = style
|
|
51
|
+
t.vrules = 'none'
|
|
52
|
+
out = t.compose()
|
|
53
|
+
assert len(_widths(out)) == 1, out
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_vrules_none_draws_no_corners():
|
|
57
|
+
t = Table(rows=[[1, 2]], headers=['A', 'B'])
|
|
58
|
+
t.style_name = 'rounded'
|
|
59
|
+
t.vrules = 'none'
|
|
60
|
+
out = t.compose()
|
|
61
|
+
for corner in '╭╮╰╯├┤┬┴┼│':
|
|
62
|
+
assert corner not in out
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_vrules_frame_keeps_the_outer_corners():
|
|
66
|
+
t = Table(rows=[[1, 2]], headers=['A', 'B'])
|
|
67
|
+
t.style_name = 'rounded'
|
|
68
|
+
t.vrules = 'frame'
|
|
69
|
+
out = t.compose()
|
|
70
|
+
assert out.split('\n')[0].startswith('╭')
|
|
71
|
+
assert out.split('\n')[-1].endswith('╯')
|
|
72
|
+
assert len(_widths(out)) == 1
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# --- merge_cells argument types ----------------------------------------------
|
|
76
|
+
|
|
77
|
+
def test_merge_cells_rejects_a_pair_where_a_number_goes():
|
|
78
|
+
t = Table(rows=[[1, 2], [3, 4]], headers=['a', 'b'])
|
|
79
|
+
with pytest.raises(TypeError, match='first_row must be an integer'):
|
|
80
|
+
t.merge_cells((0, 0), (1, 1))
|
|
81
|
+
assert t.merged_regions == []
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_merge_cells_rejects_floats():
|
|
85
|
+
t = Table(rows=[[1, 2], [3, 4]], headers=['a', 'b'])
|
|
86
|
+
with pytest.raises(TypeError, match='last_column must be an integer'):
|
|
87
|
+
t.merge_cells(0, 0, 1, 1.0)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_merge_cells_accepts_integer_like_objects():
|
|
91
|
+
class Idx:
|
|
92
|
+
def __init__(self, n):
|
|
93
|
+
self.n = n
|
|
94
|
+
|
|
95
|
+
def __index__(self):
|
|
96
|
+
return self.n
|
|
97
|
+
|
|
98
|
+
t = Table(rows=[[1, 2], [3, 4]], headers=['a', 'b'])
|
|
99
|
+
region = t.merge_cells(Idx(0), Idx(0), last_column=Idx(1), value='X')
|
|
100
|
+
assert (region.first_row, region.last_column) == (0, 1)
|
|
101
|
+
assert type(region.first_row) is int
|
|
102
|
+
assert 'X' in t.compose()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# --- coverage: public paths not exercised before -----------------------------
|
|
106
|
+
|
|
107
|
+
def test_init_with_columns_and_more_headers_pads_the_extra_column():
|
|
108
|
+
t = Table(columns=[[1, 2], [3, 4]], headers=['a', 'b', 'c'])
|
|
109
|
+
assert t.headers == ['a', 'b', 'c']
|
|
110
|
+
assert t.column_count == 3
|
|
111
|
+
assert t.to_records()[0] == {'a': 1, 'b': 3, 'c': None}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_init_with_rows_and_more_headers_pads_the_extra_column():
|
|
115
|
+
t = Table(rows=[[1, 2]], headers=['a', 'b', 'c'])
|
|
116
|
+
assert t.column_count == 3
|
|
117
|
+
assert t.to_records() == [{'a': 1, 'b': 2, 'c': None}]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_cell_valign_must_be_a_dict():
|
|
121
|
+
t = Table(rows=[[1, 2]], headers=['a', 'b'])
|
|
122
|
+
with pytest.raises(TypeError, match='cell_valign'):
|
|
123
|
+
t.cell_valign = [('t',)]
|
|
124
|
+
t.cell_valign = None
|
|
125
|
+
assert t.cell_valign == {}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def test_col_valign_list_is_normalised_per_column():
|
|
129
|
+
t = Table(rows=[['x\ny\nz', 1, 2]], headers=['a', 'b', 'c'])
|
|
130
|
+
t.col_valign = ['top', 'middle', 'bottom']
|
|
131
|
+
assert t.col_valign == ['t', 'm', 'b']
|
|
132
|
+
lines = t.compose().split('\n')
|
|
133
|
+
body = [line for line in lines if 'x' in line or 'y' in line or 'z' in line]
|
|
134
|
+
assert '1' in body[1] and '2' in body[2]
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@pytest.mark.parametrize('args, message', [
|
|
138
|
+
((0,), 'n must be >= 1'),
|
|
139
|
+
((2, -1), 'start must be >= 0'),
|
|
140
|
+
])
|
|
141
|
+
def test_divide_every_rejects_bad_arguments(args, message):
|
|
142
|
+
t = Table(rows=[[i] for i in range(4)], headers=['x'])
|
|
143
|
+
with pytest.raises(ValueError, match=message):
|
|
144
|
+
t.divide_every(*args)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_add_dividers_accepts_a_single_iterable():
|
|
148
|
+
t = Table(rows=[[i] for i in range(4)], headers=['x'], style_name='rounded')
|
|
149
|
+
plain = t.compose()
|
|
150
|
+
t.add_dividers([0, 2])
|
|
151
|
+
out = t.compose()
|
|
152
|
+
assert out.count('\n') == plain.count('\n') + 2
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def test_title_wider_than_the_table_is_printed_whole():
|
|
156
|
+
t = Table(rows=[[1, 2]], headers=['a', 'b'])
|
|
157
|
+
t.title = 'a title much wider than the table'
|
|
158
|
+
out = t.compose()
|
|
159
|
+
assert out.split('\n')[0] == 'a title much wider than the table'
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_paginate_without_frozen_header_shows_it_once():
|
|
163
|
+
t = Table(rows=[[i] for i in range(5)], headers=['head'])
|
|
164
|
+
pages = t.paginate(2, freeze_header=False)
|
|
165
|
+
assert len(pages) == 3
|
|
166
|
+
assert 'head' in pages[0]
|
|
167
|
+
assert all('head' not in page for page in pages[1:])
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_shadow_with_coloured_border_keeps_every_line_equal():
|
|
171
|
+
t = Table(rows=[[1, 2]], headers=['a', 'b'])
|
|
172
|
+
t.shadow = True
|
|
173
|
+
t.border_color = 'red'
|
|
174
|
+
out = t.compose()
|
|
175
|
+
assert '░' in strip_ansi(out)
|
|
176
|
+
assert len(_widths(out)) == 1
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# --- exports: gaps and the index column --------------------------------------
|
|
180
|
+
|
|
181
|
+
def _padded_table():
|
|
182
|
+
return Table(rows=[[1, 2], [3]], headers=['a', 'b', 'c'])
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def test_exports_never_write_the_missing_sentinel():
|
|
186
|
+
t = _padded_table()
|
|
187
|
+
t.add_row([4, t.missing, 6])
|
|
188
|
+
outputs = [
|
|
189
|
+
t.to_csv(), t.to_markdown(), t.to_rst(), t.to_asciidoc(),
|
|
190
|
+
t.to_latex(), t.to_html(), str(t.to_records()),
|
|
191
|
+
]
|
|
192
|
+
for out in outputs:
|
|
193
|
+
assert 'ValuePlacer' not in out
|
|
194
|
+
assert 'object at 0x' not in out
|
|
195
|
+
assert t.to_records() == [
|
|
196
|
+
{'a': 1, 'b': 2, 'c': None},
|
|
197
|
+
{'a': 3, 'b': None, 'c': None},
|
|
198
|
+
{'a': 4, 'b': None, 'c': 6},
|
|
199
|
+
]
|
|
200
|
+
assert t.to_csv().splitlines() == ['a,b,c', '1,2,', '3,,', '4,,6']
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def test_include_index_numbers_the_rows():
|
|
204
|
+
t = Table(rows=[[1, 2], [3, 4]], headers=['a', 'b'])
|
|
205
|
+
t.show_index = True
|
|
206
|
+
assert t.to_csv(include_index=True).splitlines() == [
|
|
207
|
+
'i,a,b', '0,1,2', '1,3,4',
|
|
208
|
+
]
|
|
209
|
+
for out in (t.to_markdown(include_index=True), t.to_rst(include_index=True),
|
|
210
|
+
t.to_latex(include_index=True), t.to_html(include_index=True),
|
|
211
|
+
t.to_asciidoc(include_index=True)):
|
|
212
|
+
assert 'IndexCounter' not in out
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_include_index_follows_start_step_title_and_roman():
|
|
216
|
+
t = Table(rows=[[1], [2], [3]], headers=['a'])
|
|
217
|
+
t.show_index = True
|
|
218
|
+
t.index_title = '#'
|
|
219
|
+
t.index_start = 4
|
|
220
|
+
t.index_step = 2
|
|
221
|
+
assert [r['#'] for r in t.to_records(include_index=True)] == [4, 6, 8]
|
|
222
|
+
t.roman_index = True
|
|
223
|
+
assert [r['#'] for r in t.to_records(include_index=True)] == [
|
|
224
|
+
'IV', 'VI', 'VIII',
|
|
225
|
+
]
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def test_include_index_works_without_show_index():
|
|
229
|
+
t = Table(rows=[[1, 2]], headers=['a', 'b'])
|
|
230
|
+
assert t.to_records(include_index=True) == [{'i': 0, 'a': 1, 'b': 2}]
|
|
231
|
+
assert t.to_records() == [{'a': 1, 'b': 2}]
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_include_index_html_with_merge_has_one_cell_per_column():
|
|
235
|
+
t = Table(rows=[[1, 2], [3, 4]], headers=['a', 'b'])
|
|
236
|
+
t.merge_cells(0, 0, last_row=1, value='m')
|
|
237
|
+
html = t.to_html(include_index=True)
|
|
238
|
+
assert 'IndexCounter' not in html
|
|
239
|
+
assert 'rowspan="2"' in html
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
# --- transpose keeps gaps as gaps --------------------------------------------
|
|
243
|
+
|
|
244
|
+
def test_transpose_shows_the_missing_value_in_gaps():
|
|
245
|
+
t = Table(rows=[[1, 2], [3]], headers=['a', 'b', 'c'])
|
|
246
|
+
t.missing_value = '?'
|
|
247
|
+
out = t.transpose()
|
|
248
|
+
assert out.missing_value == '?'
|
|
249
|
+
text = out.compose()
|
|
250
|
+
assert text.count('?') == 3
|
|
251
|
+
assert out.to_csv().splitlines() == [
|
|
252
|
+
'field,row0,row1', 'a,1,3', 'b,2,', 'c,,',
|
|
253
|
+
]
|
|
254
|
+
assert 'ValuePlacer' not in out.to_csv()
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def test_transpose_missing_val_option_wins():
|
|
258
|
+
t = Table(rows=[[1, 2], [3]], headers=['a', 'b'])
|
|
259
|
+
t.missing_value = '?'
|
|
260
|
+
out = t.transpose(missing_val='-')
|
|
261
|
+
assert '-' in out.compose().split('\n')[-2]
|
|
262
|
+
assert '?' not in out.compose()
|
|
@@ -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]]
|
|
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
|