flexablelib 2.1.0__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.1.0 → flexablelib-2.1.1}/CHANGELOG.md +24 -0
- {flexablelib-2.1.0/flexablelib.egg-info → flexablelib-2.1.1}/PKG-INFO +1 -1
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/table.py +177 -68
- {flexablelib-2.1.0 → flexablelib-2.1.1/flexablelib.egg-info}/PKG-INFO +1 -1
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib.egg-info/SOURCES.txt +1 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/package.json +1 -1
- flexablelib-2.1.1/tests/test_v211_fixes.py +301 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/LICENSE +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/MANIFEST.in +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/README.md +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/__init__.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/_speedups.c +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/_width_tables.h +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/cells.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/colors.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/columns.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/fast.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/merges.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/options.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/py.typed +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/readers.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/style_compositions.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/table_strings.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/text_width.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/utils.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib/writers.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/pytest.ini +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/setup.cfg +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/setup.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/style_examples.md +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/conftest.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/helpers.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_cells.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_columns.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_excel.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_issues.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_merge_edge_matrix.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_merges.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_readme_examples.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_style_line_widths.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_styles.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_text_width.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_upstream_issues.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v2021_fixes.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v210_rows_copy.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v2110_fixes.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v213_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v214_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v215_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v216_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v217_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v218_features.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v219_coverage.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tests/test_v219_fixes.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tools/benchmark.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tools/generate_width_tables.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tools/readme_gallery.py +0 -0
- {flexablelib-2.1.0 → flexablelib-2.1.1}/tools/text_to_png.py +0 -0
|
@@ -2,6 +2,30 @@
|
|
|
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
|
+
|
|
5
29
|
## [2.1.0] - 2026-11-03
|
|
6
30
|
|
|
7
31
|
### Changed (breaking)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.1.
|
|
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
|
|
@@ -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
|
|
@@ -2145,10 +2148,10 @@ class Table(object):
|
|
|
2145
2148
|
@index_title.setter
|
|
2146
2149
|
def index_title(self, value):
|
|
2147
2150
|
label = I_COL_TIT if value is None else str(value)
|
|
2148
|
-
new_title = IndexColumnTitle(label)
|
|
2149
2151
|
old_title = self.__index_title
|
|
2150
|
-
if
|
|
2152
|
+
if label == str(old_title):
|
|
2151
2153
|
return
|
|
2154
|
+
new_title = IndexColumnTitle(label)
|
|
2152
2155
|
for mapping in (
|
|
2153
2156
|
self.__columns_with_i,
|
|
2154
2157
|
self.__cell_types_with_i,
|
|
@@ -2160,7 +2163,16 @@ class Table(object):
|
|
|
2160
2163
|
self.__semi_processed_columns_with_i,
|
|
2161
2164
|
):
|
|
2162
2165
|
if old_title in mapping:
|
|
2163
|
-
|
|
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)
|
|
2164
2176
|
if self.__headers_with_i and self.__headers_with_i[0] is old_title:
|
|
2165
2177
|
self.__headers_with_i[0] = new_title
|
|
2166
2178
|
self.__index_title = new_title
|
|
@@ -3071,6 +3083,13 @@ class Table(object):
|
|
|
3071
3083
|
except IndexError:
|
|
3072
3084
|
return None
|
|
3073
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]
|
|
3074
3093
|
try:
|
|
3075
3094
|
return source[title][row_i]
|
|
3076
3095
|
except (KeyError, IndexError):
|
|
@@ -3559,6 +3578,7 @@ class Table(object):
|
|
|
3559
3578
|
# Cleared every render; set after the display rows are materialised
|
|
3560
3579
|
# so filter/sort and empty-row hiding agree on the same indices.
|
|
3561
3580
|
self.__compose_empty_row_indexes = None
|
|
3581
|
+
self.__compose_row_order = None
|
|
3562
3582
|
if len(self.__columns) != 0:
|
|
3563
3583
|
# Refuse to render into a space where the result would be
|
|
3564
3584
|
# illegible, if the caller asked to be told -- issue #14.
|
|
@@ -4471,6 +4491,7 @@ class Table(object):
|
|
|
4471
4491
|
if old_i in empty_stored
|
|
4472
4492
|
]
|
|
4473
4493
|
if order != list(range(len(rows))):
|
|
4494
|
+
self.__compose_row_order = list(order)
|
|
4474
4495
|
rows = [rows[i] for i in order]
|
|
4475
4496
|
rows_with_i = [rows_with_i[i] for i in order]
|
|
4476
4497
|
# Index was consumed in stored order; renumber visible rows so
|
|
@@ -5896,6 +5917,11 @@ class Table(object):
|
|
|
5896
5917
|
Requires headers (or existing columns) so widths can be measured
|
|
5897
5918
|
before the first streamed row.
|
|
5898
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
|
+
|
|
5899
5925
|
Parameters
|
|
5900
5926
|
----------
|
|
5901
5927
|
rows : iterable
|
|
@@ -5987,42 +6013,81 @@ class Table(object):
|
|
|
5987
6013
|
n += 1
|
|
5988
6014
|
return n
|
|
5989
6015
|
|
|
5990
|
-
|
|
5991
|
-
|
|
5992
|
-
|
|
5993
|
-
|
|
5994
|
-
|
|
5995
|
-
|
|
5996
|
-
|
|
5997
|
-
|
|
5998
|
-
|
|
5999
|
-
|
|
6000
|
-
|
|
6001
|
-
|
|
6002
|
-
|
|
6003
|
-
|
|
6004
|
-
|
|
6005
|
-
|
|
6006
|
-
|
|
6007
|
-
|
|
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(
|
|
6008
6059
|
rows,
|
|
6009
6060
|
headers=headers if headers else None,
|
|
6061
|
+
parse_numbers=False,
|
|
6010
6062
|
style_name=self.style_name,
|
|
6011
6063
|
missing_val=self.__missing_value,
|
|
6012
6064
|
)
|
|
6013
|
-
|
|
6014
|
-
|
|
6015
|
-
|
|
6016
|
-
|
|
6017
|
-
|
|
6018
|
-
|
|
6019
|
-
|
|
6020
|
-
|
|
6021
|
-
|
|
6022
|
-
|
|
6023
|
-
|
|
6024
|
-
|
|
6025
|
-
|
|
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)
|
|
6026
6091
|
return out
|
|
6027
6092
|
|
|
6028
6093
|
def __lookup_width_map(self, spec, header, index):
|
|
@@ -6093,44 +6158,96 @@ class Table(object):
|
|
|
6093
6158
|
headers repeat on every page; with False, only the first page shows
|
|
6094
6159
|
them. ``page_label`` adds ``page k/n`` to each page title.
|
|
6095
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.
|
|
6096
6169
|
Inspired by PrettyTable ``paginate`` / go-pretty.
|
|
6097
6170
|
"""
|
|
6098
6171
|
page_length = int(page_length)
|
|
6099
6172
|
if page_length < 1:
|
|
6100
6173
|
raise ValueError('page_length must be >= 1')
|
|
6101
6174
|
headers = list(self.headers)
|
|
6102
|
-
|
|
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)
|
|
6103
6187
|
if n == 0:
|
|
6104
6188
|
return [self.compose()]
|
|
6105
6189
|
total_pages = (n + page_length - 1) // page_length
|
|
6190
|
+
footer_from = len(order) - int(self.__footer_rows or 0)
|
|
6106
6191
|
pages = []
|
|
6107
6192
|
for page_num, start in enumerate(range(0, n, page_length), start=1):
|
|
6108
|
-
|
|
6109
|
-
|
|
6110
|
-
|
|
6111
|
-
|
|
6112
|
-
|
|
6113
|
-
|
|
6114
|
-
|
|
6115
|
-
slice_rows.append(row)
|
|
6116
|
-
page = type(self).from_records(
|
|
6117
|
-
slice_rows, headers=headers, style_name=self.style_name,
|
|
6118
|
-
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]
|
|
6119
6200
|
)
|
|
6120
|
-
|
|
6121
|
-
for
|
|
6122
|
-
|
|
6123
|
-
|
|
6124
|
-
|
|
6125
|
-
|
|
6126
|
-
|
|
6127
|
-
|
|
6128
|
-
|
|
6129
|
-
|
|
6130
|
-
|
|
6131
|
-
|
|
6132
|
-
|
|
6133
|
-
|
|
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
|
+
))
|
|
6134
6251
|
if not freeze_header and page_num > 1:
|
|
6135
6252
|
page.show_headers = False
|
|
6136
6253
|
if page_label:
|
|
@@ -6139,14 +6256,6 @@ class Table(object):
|
|
|
6139
6256
|
page.title = (
|
|
6140
6257
|
'{0} ({1})'.format(existing, label) if existing else label
|
|
6141
6258
|
)
|
|
6142
|
-
if self.__cell_colors:
|
|
6143
|
-
# Remap absolute row indexes into the page's local indexes.
|
|
6144
|
-
remapped = {}
|
|
6145
|
-
for (r, c), spec in self.__cell_colors.items():
|
|
6146
|
-
if isinstance(r, int) and start <= r < end:
|
|
6147
|
-
remapped[(r - start, c)] = spec
|
|
6148
|
-
if remapped:
|
|
6149
|
-
page.cell_colors = remapped
|
|
6150
6259
|
pages.append(page.compose())
|
|
6151
6260
|
return pages
|
|
6152
6261
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.1.
|
|
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
|
|
@@ -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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|