conversion to config continues

This commit is contained in:
Stephen Mildenhall
2025-06-12 23:29:49 +01:00
parent c54ab11cf5
commit 745f553c47
3 changed files with 52 additions and 147 deletions
+1 -1
View File
@@ -322,7 +322,7 @@ class GT(object):
# self.df.columns.names = [None] * self.df.columns.nlevels
self.df_id = df_short_hash(self.df)
# TODO: update / change
self.str_table_fmt = str_table_fmt
# self.str_table_fmt = str_table_fmt
# TODO: implement
table_width_mode = table_width_mode.lower()
if table_width_mode not in ('explicit', 'natural', 'breakable', 'minimum'):
+14 -12
View File
@@ -16,6 +16,7 @@ from pathlib import Path
import re
import sys
from textwrap import wrap
from typing import Optional, Union, Literal
import warnings
from bs4 import BeautifulSoup
@@ -29,6 +30,7 @@ from rich.table import Table
from . gtenums import Breakability, Alignment
from . gtformats import GT_Format, TableFormat
from . gtconfig import GTConfigModel
from . hasher import df_short_hash
# turn this fuck-fest off
@@ -306,11 +308,11 @@ class GT(object):
# self.df.columns.names = [None] * self.df.columns.nlevels
self.df_id = df_short_hash(self.df)
# TODO: update / change
self.str_table_fmt = str_table_fmt
# self.str_table_fmt = str_table_fmt
# TODO: implement
config.table_width_mode = config.table_width_mode.lower()
if config.table_width_mode not in ('explicit', 'natural', 'breakable', 'minimum'):
raise ValueError(f'Inadmissible options {config.table_width_mode} for config.table_width_mode.')
# self.table_width_mode = config.table_width_mode.lower()
# if config.table_width_mode not in ('explicit', 'natural', 'breakable', 'minimum'):
# raise ValueError(f'Inadmissible options {config.table_width_mode} for config.table_width_mode.')
# self.table_width_mode = table_width_mode
# self.table_width_header_adjust = table_width_header_adjust
# self.table_width_header_relax = table_width_header_relax
@@ -406,7 +408,7 @@ class GT(object):
assert callable(
config.default_formatter), 'config.default_formatter must be callable'
def wrapped_config.default_formatter(x):
def wrapped_default_formatter(x):
try:
return config.default_formatter(x)
except ValueError:
@@ -1388,7 +1390,7 @@ class GT(object):
)
return txt
def make_style(self, config.tabs):
def make_style(self, tabs):
"""Write out custom CSS for the table."""
if self.config.debug:
head_tb = '#0ff'
@@ -1797,7 +1799,7 @@ class GT(object):
post_process='',
label='',
latex=None,
config.sparsify=1):
sparsify=1):
"""
Write DataFrame to custom tikz matrix to allow greater control of
formatting and insertion of horizontal and vertical divider lines
@@ -2004,7 +2006,7 @@ class GT(object):
column_sep=column_sep,
row_sep=row_sep,
latex=latex,
config.debug=config.debug))
debug=self.config.debug))
# table header
# title rows, start with the empty spacer row
@@ -2175,7 +2177,7 @@ class GT(object):
return sio.getvalue()
@staticmethod
def estimate_column_widths(df, target_width, nc_index, scale, config.equal=False):
def estimate_column_widths(df, target_width, nc_index, scale, equal=False):
"""
Estimate sensible column widths for the dataframe [in what units?]
@@ -2290,7 +2292,7 @@ class GT(object):
return colw, tabs
@staticmethod
def config.sparsify(df, cs):
def sparsify(df, cs):
out = df.copy()
for i, c in enumerate(cs):
mask = df[cs[:i + 1]].ne(df[cs[:i + 1]].shift()).any(axis=1)
@@ -2298,7 +2300,7 @@ class GT(object):
return out
@staticmethod
def config.sparsify_old(col):
def sparsify_old(col):
"""
config.sparsify col values, col a pd.Series or dict, with items and accessor
column results from a reset_index so has index 0,1,2... this is relied upon.
@@ -2320,7 +2322,7 @@ class GT(object):
return new_col, rules
@staticmethod
def config.sparsify_mi(mi, bottom_level=False):
def sparsify_mi(mi, bottom_level=False):
"""
as above for a multi index level, without the benefit of the index...
really all should use this function
+37 -134
View File
@@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "ce33d474-f3e4-4959-b0d2-9928761f42b0",
"metadata": {},
"outputs": [
@@ -22,19 +22,19 @@
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n",
"time: 1.76 ms (started: 2025-06-12 23:03:06 +01:00)\n"
"time: 1.51 ms (started: 2025-06-12 23:24:35 +01:00)\n"
]
}
],
"source": [
"%load_ext autotime\n",
"%load_ext autoreload \n",
"%autoreload 1"
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 11,
"id": "90213338-7d46-4fe8-8925-2bf08e04e483",
"metadata": {},
"outputs": [
@@ -42,20 +42,22 @@
"name": "stdout",
"output_type": "stream",
"text": [
"time: 385 ms (started: 2025-06-12 23:05:31 +01:00)\n"
"time: 231 ms (started: 2025-06-12 23:27:49 +01:00)\n"
]
}
],
"source": [
"# %aimport greater_tables.gtconfig\n",
"%aimport greater_tables.testdf\n",
"# %aimport greater_tables.gtcore as gtc\n",
"# %aimport greater_tables.testdf\n",
"# import greater_tables.gtconfig as gtc\n",
"import greater_tables.testdf as gtt"
"import greater_tables.testdf as gtt\n",
"import greater_tables.gtcore2 as gtc"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 12,
"id": "29c879ca-fa49-47ba-8919-ecee39cdc21b",
"metadata": {},
"outputs": [
@@ -63,7 +65,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"time: 125 ms (started: 2025-06-12 23:06:05 +01:00)\n"
"time: 77.2 ms (started: 2025-06-12 23:27:51 +01:00)\n"
]
}
],
@@ -73,150 +75,51 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"id": "0a5ba2a1-a914-4a63-a175-0997919a48fa",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th>c0</th>\n",
" <th>cause administration arrive</th>\n",
" <th>television four quality</th>\n",
" </tr>\n",
" <tr>\n",
" <th>i0</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2020-06-24</th>\n",
" <td>issue</td>\n",
" <td>2024-08-17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-06-25</th>\n",
" <td>hospital account direction</td>\n",
" <td>2024-08-18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-06-26</th>\n",
" <td>community create trouble enjoy</td>\n",
" <td>2024-08-19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-06-27</th>\n",
" <td>thank although best why month team class admin...</td>\n",
" <td>2024-08-20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-06-28</th>\n",
" <td>difference natural discussion report want test</td>\n",
" <td>2024-08-21</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-06-29</th>\n",
" <td>year structure position poor company word road...</td>\n",
" <td>2024-08-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-06-30</th>\n",
" <td>contain growth response win part enough if pro...</td>\n",
" <td>2024-08-23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-07-01</th>\n",
" <td>statement picture cut phone</td>\n",
" <td>2024-08-24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-07-02</th>\n",
" <td>perform force remember certainly their least l...</td>\n",
" <td>2024-08-25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-07-03</th>\n",
" <td>national common</td>\n",
" <td>2024-08-26</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"c0 cause administration arrive \\\n",
"i0 \n",
"2020-06-24 issue \n",
"2020-06-25 hospital account direction \n",
"2020-06-26 community create trouble enjoy \n",
"2020-06-27 thank although best why month team class admin... \n",
"2020-06-28 difference natural discussion report want test \n",
"2020-06-29 year structure position poor company word road... \n",
"2020-06-30 contain growth response win part enough if pro... \n",
"2020-07-01 statement picture cut phone \n",
"2020-07-02 perform force remember certainly their least l... \n",
"2020-07-03 national common \n",
"\n",
"c0 television four quality \n",
"i0 \n",
"2020-06-24 2024-08-17 \n",
"2020-06-25 2024-08-18 \n",
"2020-06-26 2024-08-19 \n",
"2020-06-27 2024-08-20 \n",
"2020-06-28 2024-08-21 \n",
"2020-06-29 2024-08-22 \n",
"2020-06-30 2024-08-23 \n",
"2020-07-01 2024-08-24 \n",
"2020-07-02 2024-08-25 \n",
"2020-07-03 2024-08-26 "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
"ename": "AttributeError",
"evalue": "'NoneType' object has no attribute 'table_width_mode'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[13]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mgtc\u001b[49m\u001b[43m.\u001b[49m\u001b[43mGT\u001b[49m\u001b[43m(\u001b[49m\u001b[43mTDF\u001b[49m\u001b[43m.\u001b[49m\u001b[43mmake\u001b[49m\u001b[43m(\u001b[49m\u001b[32;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43ms10d\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43md\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcol_index\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43ms\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[36mFile \u001b[39m\u001b[32mC:\\S\\TELOS\\Python\\greater_tables_project\\greater_tables\\gtcore2.py:313\u001b[39m, in \u001b[36mGT.__init__\u001b[39m\u001b[34m(self, df, caption, label, aligners, formatters, tabs, unbreakable, ratio_cols, year_cols, date_cols, raw_cols, show_index, config, config_path, **overrides)\u001b[39m\n\u001b[32m 309\u001b[39m \u001b[38;5;28mself\u001b[39m.df_id = df_short_hash(\u001b[38;5;28mself\u001b[39m.df)\n\u001b[32m 310\u001b[39m \u001b[38;5;66;03m# TODO: update / change\u001b[39;00m\n\u001b[32m 311\u001b[39m \u001b[38;5;66;03m# self.str_table_fmt = str_table_fmt\u001b[39;00m\n\u001b[32m 312\u001b[39m \u001b[38;5;66;03m# TODO: implement\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m313\u001b[39m config.table_width_mode = \u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mtable_width_mode\u001b[49m.lower()\n\u001b[32m 314\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m config.table_width_mode \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (\u001b[33m'\u001b[39m\u001b[33mexplicit\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mnatural\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mbreakable\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mminimum\u001b[39m\u001b[33m'\u001b[39m):\n\u001b[32m 315\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33mf\u001b[39m\u001b[33m'\u001b[39m\u001b[33mInadmissible options \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig.table_width_mode\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m for config.table_width_mode.\u001b[39m\u001b[33m'\u001b[39m)\n",
"\u001b[31mAttributeError\u001b[39m: 'NoneType' object has no attribute 'table_width_mode'"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"time: 20.6 ms (started: 2025-06-12 23:16:45 +01:00)\n"
"time: 232 ms (started: 2025-06-12 23:27:54 +01:00)\n"
]
}
],
"source": [
"TDF.make(10, 's10d', index='d', col_index='s')"
"gtc.GT(TDF.make(10, 's10d', index='d', col_index='s'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "05eb4656-4fa6-4440-bee2-1231e2ac7d49",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time: 1.38 ms (started: 2025-06-12 23:21:43 +01:00)\n"
]
}
],
"source": [
"from greater_tables. gtcore import GT"
]
},
{
"cell_type": "code",