flexablelib 2.1.0__tar.gz → 2.2.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.
Files changed (64) hide show
  1. {flexablelib-2.1.0 → flexablelib-2.2.0}/CHANGELOG.md +56 -0
  2. {flexablelib-2.1.0/flexablelib.egg-info → flexablelib-2.2.0}/PKG-INFO +23 -3
  3. {flexablelib-2.1.0 → flexablelib-2.2.0}/README.md +22 -2
  4. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/table.py +308 -71
  5. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/writers.py +209 -74
  6. {flexablelib-2.1.0 → flexablelib-2.2.0/flexablelib.egg-info}/PKG-INFO +23 -3
  7. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib.egg-info/SOURCES.txt +2 -0
  8. {flexablelib-2.1.0 → flexablelib-2.2.0}/package.json +1 -1
  9. flexablelib-2.2.0/tests/test_v211_fixes.py +301 -0
  10. flexablelib-2.2.0/tests/test_v220_exports.py +230 -0
  11. {flexablelib-2.1.0 → flexablelib-2.2.0}/LICENSE +0 -0
  12. {flexablelib-2.1.0 → flexablelib-2.2.0}/MANIFEST.in +0 -0
  13. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/__init__.py +0 -0
  14. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/_speedups.c +0 -0
  15. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/_width_tables.h +0 -0
  16. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/cells.py +0 -0
  17. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/colors.py +0 -0
  18. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/columns.py +0 -0
  19. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/fast.py +0 -0
  20. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/merges.py +0 -0
  21. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/options.py +0 -0
  22. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/py.typed +0 -0
  23. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/readers.py +0 -0
  24. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/style_compositions.py +0 -0
  25. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/table_strings.py +0 -0
  26. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/text_width.py +0 -0
  27. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib/utils.py +0 -0
  28. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib.egg-info/dependency_links.txt +0 -0
  29. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib.egg-info/requires.txt +0 -0
  30. {flexablelib-2.1.0 → flexablelib-2.2.0}/flexablelib.egg-info/top_level.txt +0 -0
  31. {flexablelib-2.1.0 → flexablelib-2.2.0}/pytest.ini +0 -0
  32. {flexablelib-2.1.0 → flexablelib-2.2.0}/setup.cfg +0 -0
  33. {flexablelib-2.1.0 → flexablelib-2.2.0}/setup.py +0 -0
  34. {flexablelib-2.1.0 → flexablelib-2.2.0}/style_examples.md +0 -0
  35. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/conftest.py +0 -0
  36. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/helpers.py +0 -0
  37. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_cells.py +0 -0
  38. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_columns.py +0 -0
  39. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_excel.py +0 -0
  40. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_features.py +0 -0
  41. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_issues.py +0 -0
  42. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_merge_edge_matrix.py +0 -0
  43. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_merges.py +0 -0
  44. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_readme_examples.py +0 -0
  45. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_style_line_widths.py +0 -0
  46. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_styles.py +0 -0
  47. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_text_width.py +0 -0
  48. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_two_pass_measurement.py +0 -0
  49. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_upstream_issues.py +0 -0
  50. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v2021_fixes.py +0 -0
  51. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v210_rows_copy.py +0 -0
  52. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v2110_fixes.py +0 -0
  53. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v213_features.py +0 -0
  54. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v214_features.py +0 -0
  55. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v215_features.py +0 -0
  56. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v216_features.py +0 -0
  57. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v217_features.py +0 -0
  58. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v218_features.py +0 -0
  59. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v219_coverage.py +0 -0
  60. {flexablelib-2.1.0 → flexablelib-2.2.0}/tests/test_v219_fixes.py +0 -0
  61. {flexablelib-2.1.0 → flexablelib-2.2.0}/tools/benchmark.py +0 -0
  62. {flexablelib-2.1.0 → flexablelib-2.2.0}/tools/generate_width_tables.py +0 -0
  63. {flexablelib-2.1.0 → flexablelib-2.2.0}/tools/readme_gallery.py +0 -0
  64. {flexablelib-2.1.0 → flexablelib-2.2.0}/tools/text_to_png.py +0 -0
@@ -2,6 +2,62 @@
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.2.0] - 2026-11-04
6
+
7
+ ### Changed (breaking)
8
+ - Exports now write the table as ``compose()`` shows it. ``to_csv``,
9
+ ``to_markdown``, ``to_html``, ``to_rst``, ``to_asciidoc``, ``to_latex``,
10
+ ``to_excel``, ``to_records`` and ``_repr_html_`` follow ``sort_by``
11
+ (``sort_reverse``, callables) and ``row_filter``, leave out
12
+ ``hide_columns`` (and empty rows/columns when ``show_empty_rows`` /
13
+ ``show_empty_columns`` are off), and number the index by display position.
14
+ Before, they wrote every stored row in insertion order.
15
+ - The text exports (CSV, Markdown, HTML, RST, AsciiDoc, LaTeX) hold the
16
+ formatted strings ``compose()`` prints -- ``float_format``,
17
+ ``int_format``, ``leading_zeros``, ``custom_format``, ``strip_cells`` --
18
+ without padding or ANSI codes. Before, they wrote ``str()`` of the raw
19
+ value. Gaps stay empty strings. Reading such an export back gives the
20
+ printed text (``'1,200'`` stays a string).
21
+ - ``to_records()`` keeps the stored Python values (numbers stay numbers, gaps
22
+ ``None``) but in display order, filtered, without hidden columns.
23
+ - ``to_excel()`` keeps numbers numeric; other cells hold the displayed text.
24
+ - Merges keep their display position, as in ``compose()``; a merge split by
25
+ ``show_empty_rows=False`` is dropped and one over hidden columns shrinks.
26
+ - ``Table.rows`` is unchanged: the stored data in insertion order.
27
+
28
+ ### Added
29
+ - ``to_records(formatted=True)`` returns the displayed strings.
30
+ - ``to_excel()`` turns simple ``int_format`` / ``float_format`` /
31
+ ``custom_format`` specs and ``leading_zeros`` into Excel number formats
32
+ (``',d'`` -> ``#,##0``, ``'.2f'`` -> ``0.00``, ``'.1%'`` -> ``0.0%``,
33
+ ``'$ {:,.2f}'`` -> ``"$ "#,##0.00``).
34
+ - ``to_html()`` sorts numeric columns on the stored number, so formatted
35
+ values such as ``1,200`` sort correctly in the browser.
36
+
37
+ ## [2.1.1] - 2026-11-04
38
+
39
+ ### Fixed
40
+ - ``paginate()`` and ``stream()`` dropped most of the table's settings:
41
+ ``float_format``, ``int_format``, ``custom_format``, ``leading_zeros``,
42
+ ``str_align``/``int_align``/``float_align``/``bool_align``,
43
+ ``column_colors``, ``max_table_width``, ``show_empty_rows`` and border
44
+ texts, among others. They now render like ``compose()``.
45
+ - ``paginate()`` and ``stream()`` re-parsed text cells, so ``'1e3'`` printed
46
+ as ``1000.0``. Values are kept as stored.
47
+ - ``paginate()`` ignored ``sort_by`` and ``row_filter``, and with
48
+ ``show_empty_rows=False`` a page could lose visible rows. Pages now hold
49
+ the rows in display order, the index keeps counting across pages, and
50
+ ``row_colors``, ``cell_colors``, ``cell_valign``, ``color_rule``,
51
+ ``zebra``, ``footer_rows``, dividers and merges follow their rows (a merge
52
+ split by a page break is dropped).
53
+ - ``stream()`` now honours ``row_filter``, ``row_colors``, ``cell_colors``,
54
+ ``color_rule``, dividers and merges. ``sort_by`` is still not applied while
55
+ streaming (documented).
56
+ - ``color_rule`` received another row's value after ``sort_by`` or
57
+ ``row_filter`` reordered the body, so it painted the wrong cells.
58
+ - Setting ``index_title`` (even to its current value) shifted the column
59
+ alignments and widths by one column.
60
+
5
61
  ## [2.1.0] - 2026-11-03
6
62
 
7
63
  ### Changed (breaking)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexablelib
3
- Version: 2.1.0
3
+ Version: 2.2.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
@@ -425,6 +425,25 @@ table.to_html('report.html', paginate=25)
425
425
  table.to_excel('report.xlsx')
426
426
  ```
427
427
 
428
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
429
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
430
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
431
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
432
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
433
+ without padding or colour. Empty cells are empty strings.
434
+
435
+ ```python
436
+ table.sort_by, table.sort_reverse = 'Uptime', True
437
+ table.row_filter = lambda row: row[1] < 99.9
438
+ table.float_format = '.1f'
439
+ table.to_markdown() # same rows, order and numbers as print(table)
440
+ ```
441
+
442
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
443
+ `None`) in display order; `to_records(formatted=True)` gives the printed
444
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
445
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
446
+
428
447
  `to_html` writes one self-contained file — the stylesheet and the sorting,
429
448
  filtering and pagination script are inline — so it works offline and from a
430
449
  `file://` URL.
@@ -439,8 +458,9 @@ table.parse_str_numbers = True
439
458
 
440
459
  Values with leading zeros are left alone, so `'007'` stays a string.
441
460
 
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
461
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
462
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
463
+ does not change the table. Use `add_row` and
444
464
  `set_cell` instead:
445
465
 
446
466
  ```python
@@ -360,6 +360,25 @@ table.to_html('report.html', paginate=25)
360
360
  table.to_excel('report.xlsx')
361
361
  ```
362
362
 
363
+ Since 2.2.0 every export writes the table as `print(table)` shows it: rows in
364
+ `sort_by` order (with `sort_reverse` and callables), `row_filter` applied,
365
+ `hide_columns` left out, and the text formats (`to_csv`, `to_markdown`,
366
+ `to_html`, `to_rst`, `to_asciidoc`, `to_latex`) hold the formatted strings —
367
+ `float_format`, `int_format`, `leading_zeros`, `custom_format`, `strip_cells` —
368
+ without padding or colour. Empty cells are empty strings.
369
+
370
+ ```python
371
+ table.sort_by, table.sort_reverse = 'Uptime', True
372
+ table.row_filter = lambda row: row[1] < 99.9
373
+ table.float_format = '.1f'
374
+ table.to_markdown() # same rows, order and numbers as print(table)
375
+ ```
376
+
377
+ `to_records()` keeps the stored Python values (numbers stay numbers, gaps are
378
+ `None`) in display order; `to_records(formatted=True)` gives the printed
379
+ strings. `to_excel()` writes numbers as numbers and turns simple format specs
380
+ (`',d'`, `'.2f'`, `'.1%'`, `'{:,.2f}'`…) into the cell's Excel number format.
381
+
363
382
  `to_html` writes one self-contained file — the stylesheet and the sorting,
364
383
  filtering and pagination script are inline — so it works offline and from a
365
384
  `file://` URL.
@@ -374,8 +393,9 @@ table.parse_str_numbers = True
374
393
 
375
394
  Values with leading zeros are left alone, so `'007'` stays a string.
376
395
 
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
396
+ `table.rows` is a copy of the stored data, in insertion order and with `None`
397
+ in empty cells (sorting, filtering and formats do not touch it), so editing it
398
+ does not change the table. Use `add_row` and
379
399
  `set_cell` instead:
380
400
 
381
401
  ```python
@@ -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 new_title is old_title:
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
- mapping[new_title] = mapping.pop(old_title)
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
- def __display_clone(self, include_rows=True):
5991
- """Shallow display copy used by stream/paginate helpers."""
5992
- headers = list(self.headers)
5993
- rows = []
5994
- if include_rows:
5995
- for row_i in range(self.__real_row_count):
5996
- row = []
5997
- for header in headers:
5998
- col = self.__columns.get(header, [])
5999
- if row_i < len(col):
6000
- cell = col[row_i]
6001
- if isinstance(cell, ValuePlacer):
6002
- cell = self.__missing_value
6003
- row.append(cell)
6004
- else:
6005
- row.append(self.__missing_value)
6006
- rows.append(row)
6007
- out = type(self).from_records(
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
- for attr in (
6014
- 'title', 'caption', 'show_headers', 'show_index', 'header_color',
6015
- 'border_color', 'use_colors', 'zebra', 'footer_rows', 'footer_color',
6016
- 'cell_padding', 'overflow', 'auto_wrap', 'valign', 'col_valign',
6017
- 'panel', 'shadow', 'col_alignment', 'header_align', 'max_width',
6018
- 'table_align', 'hide_columns', 'index_title', 'roman_index',
6019
- 'number_columns', 'strip_cells', 'hrules', 'vrules',
6020
- 'index_start', 'index_step',
6021
- ):
6022
- try:
6023
- setattr(out, attr, getattr(self, attr))
6024
- except Exception:
6025
- pass
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):
@@ -6085,6 +6150,127 @@ class Table(object):
6085
6150
  """Alias of :meth:`transpose` (tabled naming)."""
6086
6151
  return self.transpose(**options)
6087
6152
 
6153
+ def _export_view(self, include_index=False):
6154
+ """
6155
+ What the writers export: the rows ``compose()`` shows, as data.
6156
+
6157
+ Rows come in display order -- ``row_filter`` and ``sort_by`` (with
6158
+ ``sort_reverse`` and callables) applied, empty rows left out when
6159
+ ``show_empty_rows`` is off. Columns in ``hide_columns`` are left out,
6160
+ and so are empty columns when ``show_empty_columns`` is off.
6161
+
6162
+ Returns a namespace with:
6163
+
6164
+ - ``headers``: header names (``index_title`` first with the index).
6165
+ - ``raw``: cell values as stored, ``None`` in the gaps.
6166
+ - ``text``: the strings ``compose()`` prints for each cell --
6167
+ ``int_format``, ``float_format``, ``leading_zeros``,
6168
+ ``custom_format`` and ``strip_cells`` applied, colour stripped, no
6169
+ padding -- ``''`` in the gaps.
6170
+ - ``merges``: ``merged_regions`` in export coordinates (data columns,
6171
+ index excluded); merges broken up by the filter, or covering only
6172
+ hidden columns, are dropped.
6173
+ - ``index``: whether the first column is the index.
6174
+
6175
+ ``Table.rows`` stays the stored copy; this is only for writers.
6176
+ """
6177
+ from types import SimpleNamespace
6178
+
6179
+ headers = list(self.__headers)
6180
+ n_rows = self.__real_row_count
6181
+ stored = [self.__stored_row(row_i, headers) for row_i in range(n_rows)]
6182
+
6183
+ def is_gap(cell):
6184
+ return cell is None or isinstance(cell, ValuePlacer)
6185
+
6186
+ # Raw values, gaps as None -- both the padding marker and the cells
6187
+ # filled with ``missing_value`` above.
6188
+ raw_rows = []
6189
+ for row_i in range(n_rows):
6190
+ row = []
6191
+ for header in headers:
6192
+ col = self.__columns.get(header, [])
6193
+ cell = col[row_i] if row_i < len(col) else None
6194
+ row.append(None if is_gap(cell) else cell)
6195
+ raw_rows.append(row)
6196
+
6197
+ order = self.__display_row_order(stored)
6198
+ hidden_rows = (
6199
+ set() if self.__show_empty_rows else set(self.__empty_row_indexes)
6200
+ )
6201
+ # Display position (what merges are keyed on) -> exported row.
6202
+ position_to_row = {}
6203
+ kept = []
6204
+ for position, row_i in enumerate(order):
6205
+ if row_i in hidden_rows:
6206
+ continue
6207
+ position_to_row[position] = len(kept)
6208
+ kept.append(row_i)
6209
+
6210
+ hidden_cols = set(self.__user_hidden_column_indexes(False))
6211
+ if not self.__show_empty_columns:
6212
+ for column_i in range(len(headers)):
6213
+ if all(
6214
+ is_empty_cell(raw_rows[row_i][column_i], self.__value_placer)
6215
+ for row_i in range(n_rows)
6216
+ ):
6217
+ hidden_cols.add(column_i)
6218
+ visible = [i for i in range(len(headers)) if i not in hidden_cols]
6219
+ column_to_export = {c: j for j, c in enumerate(visible)}
6220
+
6221
+ out_headers = [str(headers[c]) for c in visible]
6222
+ out_raw = []
6223
+ out_text = []
6224
+ for row_i in kept:
6225
+ raw_row = raw_rows[row_i]
6226
+ out_raw.append([raw_row[c] for c in visible])
6227
+ texts = []
6228
+ for c in visible:
6229
+ value = raw_row[c]
6230
+ if value is None:
6231
+ texts.append('')
6232
+ continue
6233
+ shown = self.__format_display_value(value, headers[c], c)
6234
+ texts.append('' if shown is None else strip_ansi(str(shown)))
6235
+ out_text.append(texts)
6236
+
6237
+ merges = []
6238
+ for region in self.__merged_regions:
6239
+ rows_span = range(region.first_row, region.last_row + 1)
6240
+ mapped_rows = [position_to_row.get(p) for p in rows_span]
6241
+ if any(r is None for r in mapped_rows):
6242
+ continue
6243
+ if mapped_rows[-1] - mapped_rows[0] != len(mapped_rows) - 1:
6244
+ continue
6245
+ cols = [
6246
+ column_to_export[c]
6247
+ for c in range(region.first_column, region.last_column + 1)
6248
+ if c in column_to_export
6249
+ ]
6250
+ if not cols:
6251
+ continue
6252
+ merges.append(region._replace(
6253
+ first_row=mapped_rows[0], last_row=mapped_rows[-1],
6254
+ first_column=cols[0], last_column=cols[-1],
6255
+ ))
6256
+
6257
+ if include_index:
6258
+ start, step = self.index_start, self.index_step
6259
+ labels = []
6260
+ for export_i in range(len(kept)):
6261
+ label = start + export_i * step
6262
+ labels.append(to_roman(label) if self.roman_index else label)
6263
+ out_headers = [str(self.index_title)] + out_headers
6264
+ out_raw = [[label] + row for label, row in zip(labels, out_raw)]
6265
+ out_text = [
6266
+ [str(label)] + row for label, row in zip(labels, out_text)
6267
+ ]
6268
+
6269
+ return SimpleNamespace(
6270
+ headers=out_headers, raw=out_raw, text=out_text,
6271
+ merges=merges, index=bool(include_index),
6272
+ )
6273
+
6088
6274
  def paginate(self, page_length=20, freeze_header=True, page_label=False):
6089
6275
  """
6090
6276
  Split the table into composed string pages of ``page_length`` body rows.
@@ -6093,44 +6279,96 @@ class Table(object):
6093
6279
  headers repeat on every page; with False, only the first page shows
6094
6280
  them. ``page_label`` adds ``page k/n`` to each page title.
6095
6281
  Returns a list of strings. ``page_length`` must be >= 1.
6282
+
6283
+ Pages hold the rows in the order ``compose()`` shows them --
6284
+ ``sort_by`` and ``row_filter`` applied, empty rows left out when
6285
+ ``show_empty_rows`` is off -- with the same formats and colours. The
6286
+ index keeps counting across pages, and row-keyed settings
6287
+ (``row_colors``, ``cell_colors``, ``cell_valign``, ``color_rule``,
6288
+ ``zebra``, ``footer_rows``, dividers, merges) follow their rows; a
6289
+ merge split by a page break is dropped.
6096
6290
  Inspired by PrettyTable ``paginate`` / go-pretty.
6097
6291
  """
6098
6292
  page_length = int(page_length)
6099
6293
  if page_length < 1:
6100
6294
  raise ValueError('page_length must be >= 1')
6101
6295
  headers = list(self.headers)
6102
- n = self.row_count
6296
+ stored = [
6297
+ self.__stored_row(row_i, headers)
6298
+ for row_i in range(self.__real_row_count)
6299
+ ]
6300
+ # Display positions, as compose() numbers them for row_colors and
6301
+ # friends, and the stored row shown at each one.
6302
+ order = self.__display_row_order(stored)
6303
+ hidden = (
6304
+ set() if self.__show_empty_rows else set(self.__empty_row_indexes)
6305
+ )
6306
+ positions = [p for p, row_i in enumerate(order) if row_i not in hidden]
6307
+ n = len(positions)
6103
6308
  if n == 0:
6104
6309
  return [self.compose()]
6105
6310
  total_pages = (n + page_length - 1) // page_length
6311
+ footer_from = len(order) - int(self.__footer_rows or 0)
6106
6312
  pages = []
6107
6313
  for page_num, start in enumerate(range(0, n, page_length), start=1):
6108
- end = min(start + page_length, n)
6109
- slice_rows = []
6110
- for row_i in range(start, end):
6111
- row = []
6112
- for header in headers:
6113
- col = self.__columns.get(header, [])
6114
- row.append(col[row_i] if row_i < len(col) else self.__missing_value)
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,
6314
+ visible = positions[start:start + page_length]
6315
+ # Hidden empty rows travel with the page they fall inside (the
6316
+ # first and last pages take the ones before and after every
6317
+ # visible row), so the page types its columns as the table does.
6318
+ first_p = 0 if start == 0 else visible[0]
6319
+ last_p = (
6320
+ len(order) - 1 if start + page_length >= n else visible[-1]
6119
6321
  )
6120
- # Copy common display settings so pages look like the parent.
6121
- for attr in (
6122
- 'title', 'caption', 'show_headers', 'show_index', 'header_color',
6123
- 'border_color', 'use_colors', 'zebra', 'footer_rows', 'footer_color',
6124
- 'cell_padding', 'overflow', 'auto_wrap', 'valign', 'col_valign',
6125
- 'panel', 'shadow', 'col_alignment', 'header_align', 'max_width',
6126
- 'column_min_width', 'column_max_width', 'table_align',
6127
- 'hide_columns', 'index_title', 'roman_index', 'number_columns',
6128
- 'strip_cells', 'hrules', 'vrules', 'index_start', 'index_step',
6129
- ):
6130
- try:
6131
- setattr(page, attr, getattr(self, attr))
6132
- except Exception:
6133
- pass
6322
+ page_positions = list(range(first_p, last_p + 1))
6323
+ local = {p: j for j, p in enumerate(page_positions)}
6324
+ page = self.__derived_table(
6325
+ [stored[order[p]] for p in page_positions], headers
6326
+ )
6327
+ self.__copy_display_settings(page)
6328
+ page.sort_by = None
6329
+ page.row_filter = None
6330
+ # The index carries on from the previous page.
6331
+ page.index_start = self.__i_start + start * self.__i_step
6332
+ page.footer_rows = sum(1 for p in page_positions if p >= footer_from)
6333
+ zebra = self.__zebra
6334
+ if zebra and page_positions[0] % 2:
6335
+ # Stripes are by row parity; an odd first row swaps them.
6336
+ if isinstance(zebra, (list, tuple)) and len(zebra) >= 2:
6337
+ zebra = (zebra[1], zebra[0])
6338
+ else:
6339
+ zebra = (zebra, None)
6340
+ page.zebra = zebra
6341
+ page.row_colors = {
6342
+ local[r]: spec for r, spec in self.__row_colors.items()
6343
+ if r in local
6344
+ }
6345
+ for name, mapping in (('cell_colors', self.__cell_colors),
6346
+ ('cell_valign', self.__cell_valign)):
6347
+ remapped = {
6348
+ (local[key[0]], key[1]): spec
6349
+ for key, spec in mapping.items()
6350
+ if isinstance(key, tuple) and key[0] in local
6351
+ }
6352
+ if remapped:
6353
+ setattr(page, name, remapped)
6354
+ if self.__color_rule is not None:
6355
+ rule = self.__color_rule
6356
+ page.color_rule = (
6357
+ lambda value, row, column, _rule=rule, _pos=page_positions:
6358
+ _rule(value, _pos[row] if 0 <= row < len(_pos) else row,
6359
+ column)
6360
+ )
6361
+ last = visible[-1]
6362
+ for after in self.__dividers:
6363
+ if after in local and after != last:
6364
+ page.add_divider(local[after])
6365
+ for region in self.__merged_regions:
6366
+ first, final = region.first_row, region.last_row
6367
+ if (first in local and final in local
6368
+ and local[final] - local[first] == final - first):
6369
+ page.__merged_regions.append(region._replace(
6370
+ first_row=local[first], last_row=local[final],
6371
+ ))
6134
6372
  if not freeze_header and page_num > 1:
6135
6373
  page.show_headers = False
6136
6374
  if page_label:
@@ -6139,14 +6377,6 @@ class Table(object):
6139
6377
  page.title = (
6140
6378
  '{0} ({1})'.format(existing, label) if existing else label
6141
6379
  )
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
6380
  pages.append(page.compose())
6151
6381
  return pages
6152
6382
 
@@ -6162,10 +6392,17 @@ class Table(object):
6162
6392
  from .readers import from_markdown
6163
6393
  return from_markdown(cls, source, parse_numbers=parse_numbers, **options)
6164
6394
 
6165
- def to_records(self, include_index=False):
6166
- """The table as a list of dicts, one per row."""
6395
+ def to_records(self, include_index=False, formatted=False):
6396
+ """
6397
+ The table as a list of dicts, one per row, in display order.
6398
+
6399
+ ``sort_by`` and ``row_filter`` are applied and ``hide_columns`` left
6400
+ out, like every export since 2.2.0. Values stay the stored Python
6401
+ objects (numbers remain numbers); ``formatted=True`` returns the
6402
+ strings ``compose()`` shows instead. Gaps are ``None``.
6403
+ """
6167
6404
  from .writers import to_records
6168
- return to_records(self, include_index)
6405
+ return to_records(self, include_index, formatted)
6169
6406
 
6170
6407
  def to_csv(self, target=None, include_index=False, delimiter=',',
6171
6408
  encoding='utf-8', dialect=None, quoting=None, quotechar=None,