-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move table generation into notebooks
- Loading branch information
1 parent
82d5859
commit 288bf93
Showing
3 changed files
with
102 additions
and
176 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" Routines for working with grid tables | ||
""" | ||
|
||
def replace_val(part, val): | ||
vs = f' {val}' | ||
if len(vs) > len(part) - 1: | ||
raise ValueError(f'{vs} is too long to replace in {part}') | ||
return vs + ' ' * (len(part) - len(vs)) | ||
|
||
|
||
def extend_with_row(tab_md, vals, where='before'): | ||
lines = tab_md.splitlines() | ||
lines = lines[::-1] if where == 'after' else lines | ||
assert lines[0].startswith('+') | ||
L1 = lines[1] | ||
assert (L1[0], L1[-1]) == ('|', '|') | ||
parts = lines[1].split('|')[1:-1] | ||
new_parts = [] | ||
for part, val in zip(parts, vals): | ||
new_parts.append(replace_val(part, val)) | ||
L1 = '|'.join(new_parts) | ||
lines = lines[::-1] if where == 'after' else lines | ||
return '\n'.join([lines[0], f'|{L1}|'] + lines) | ||
|
||
|
||
def footerize_table(tab_md, indices=(-2, -1)): | ||
lines = tab_md.splitlines() | ||
line_indices = [i for i, line in enumerate(lines) | ||
if line[:2] in ('+-', '+=')] | ||
for to_footerize in indices: | ||
LI = line_indices[to_footerize] | ||
lines[LI] = lines[LI].replace('-', '=') | ||
return '\n'.join(lines) | ||
|
||
|
||
def to_md(df, prepended=None, extended=None): | ||
tab_md = df.to_markdown(index=None, tablefmt='grid', numalign="left") | ||
if prepended is not None: | ||
tab_md = extend_with_row(tab_md, prepended) | ||
if extended is not None: | ||
tab_md = extend_with_row(tab_md, extended, where='after') | ||
return tab_md |