Last active
April 19, 2024 06:07
-
-
Save mols3131d/a163902fd51f3c939e203cf34e727388 to your computer and use it in GitHub Desktop.
python3-common
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
import datetime | |
from typing import Literal | |
from rich.color import Color | |
from rich.console import Console, JustifyMethod | |
from rich.padding import Padding | |
from rich.panel import Panel | |
from rich.pretty import Pretty | |
from rich.rule import Rule | |
from rich.style import Style | |
from rich.table import Table | |
from rich.text import Text | |
class Printer: | |
colors = [ | |
"red", | |
"yellow", | |
"green", | |
"cyan", | |
"blue", | |
"magenta", | |
] | |
bright_colors = [f"bright_{c}" for c in colors] | |
console = Console() | |
text_dictargs = {"style": "bold"} | |
rule_dictargs = {"style": "bold"} | |
pretty_dictargs = ({},) | |
panel_dictargs = { | |
"title": None, | |
"title_align": "center", | |
"subtitle": None, | |
"subtitle_align": "center", | |
"expand": "", | |
"style": "bold", | |
"width": None, | |
"height": None, | |
"padding": (0, 1), | |
"highlight": False, | |
} | |
padding_dictargs = { | |
"pad": (0, 0, 0, 0), | |
"style": "", | |
"expand": True, | |
} | |
@classmethod | |
def print( | |
cls, | |
data, | |
text_dictargs={"style": ""}, | |
rule_dictargs={"style": ""}, | |
pretty_dictargs={}, | |
panel_dictargs={}, | |
padding_dictargs={}, | |
print_dictargs={}, | |
instead_log=False, | |
): | |
_flag = None | |
d = data | |
if isinstance(d, str): | |
if d[:3] == "---" and d[-3:] == "---": | |
d = d[3:-3] | |
d = Text(d, **text_dictargs) | |
d = Rule(d, **rule_dictargs) | |
else: | |
d = Text(d, **text_dictargs) | |
elif isinstance(d, Table): | |
pass | |
else: | |
d = Pretty(d, **pretty_dictargs) | |
# panel | |
if panel_dictargs: | |
d = Panel(d, **panel_dictargs) | |
# padding | |
if padding_dictargs: | |
d = Padding(d, **padding_dictargs) | |
# print or log | |
if instead_log: | |
cls.console.log(d, **print_dictargs) | |
else: | |
cls.console.print(d, **print_dictargs) | |
@classmethod | |
def log( | |
cls, | |
data, | |
text_dictargs={"style": ""}, | |
rule_dictargs={"style": ""}, | |
pretty_dictargs={}, | |
panel_dictargs={}, | |
padding_dictargs={}, | |
print_dictargs={}, | |
): | |
cls.print( | |
data, | |
text_dictargs=text_dictargs, | |
rule_dictargs=rule_dictargs, | |
pretty_dictargs=pretty_dictargs, | |
panel_dictargs=panel_dictargs, | |
padding_dictargs=padding_dictargs, | |
print_dictargs=print_dictargs, | |
instead_log=True, | |
) | |
@classmethod | |
def table( | |
cls, | |
data, | |
title=None, | |
caption=None, | |
title_justify: "JustifyMethod" = "left", | |
caption_justify: "JustifyMethod" = "left", | |
table_dictargs: dict = {}, | |
): | |
s = Style( | |
bgcolor=None, | |
bold=True, | |
) | |
title = Text(title, style=s) | |
table = Table( | |
**table_dictargs, | |
title=title, | |
caption=caption, | |
title_justify=title_justify, | |
caption_justify=caption_justify, | |
leading=True, | |
row_styles=["", "dim"], | |
) | |
# add cols | |
colors = cls.colors + cls.bright_colors | |
for i, col_name in enumerate(data.keys()): | |
col_dictargs = { | |
"style": Style( | |
color=Color.parse(colors[i % len(colors)]), | |
bgcolor=None, | |
bold=False, | |
), | |
"header_style": Style( | |
color=Color.parse(colors[i % len(colors)]), | |
bgcolor=None, | |
bold=True, | |
), | |
} | |
col_text = Text(col_name) | |
table.add_column(header=col_text, footer="aaaaaaaaaaaaa", **col_dictargs) | |
for i in range(len(next(iter(data.values())))): | |
row_texts = [] | |
for col_name, col_data in data.items(): | |
row_texts.append(Text(col_data[i])) | |
table.add_row(*row_texts) | |
return table | |
@classmethod | |
def panel( | |
cls, | |
data, | |
panel_dictargs={ | |
"title": None, | |
"title_align": "center", | |
"subtitle": None, | |
"subtitle_align": "center", | |
"expand": "", | |
"style": "bold", | |
"width": None, | |
"height": None, | |
"padding": (0, 1), | |
"highlight": False, | |
}, | |
): | |
d = data | |
d = Panel(d, **panel_dictargs) | |
return d | |
if __name__ == "__main__": | |
# testing | |
s = "wpahrwpahr" | |
r = "---12asdasd3---" | |
l = ["app", 1, 2, 3, {"D": 1, "A": 2}] | |
d = {"D": 1, "A": 2} | |
Printer.print(r, rule_dictargs={"style": "blue"}) | |
Printer.log(r, rule_dictargs={"style": "blue"}) | |
table = { | |
"Released": ["Dec 20, 2019", "May 25, 2018", "Dec 15, 2017", "Dec 16, 2016"], | |
"Title": [ | |
"Star Wars: The Rise of Skywalker", | |
"Solo: A Star Wars Story", | |
"Star Wars Ep. V111: The Last Jedi", | |
"Rogue One: A Star Wars Story", | |
], | |
"Box Office": [ | |
"$952,110,690", | |
"$393,151,347", | |
"$1,332,539,889", | |
"$1,332,439,889", | |
], | |
"asd": ["Dec 20, 2019", "May 25, 2018", "Dec 15, 2017", "Dec 16, 2016"], | |
"aa": [ | |
"Star Wars: The Rise of Skywalker", | |
"Solo: A Star Wars Story", | |
"Star Wars Ep. V111: The Last Jedi", | |
"Rogue One: A Star Wars Story", | |
], | |
"Bbb": [ | |
"$952,110,690", | |
"$393,151,347", | |
"$1,332,539,889", | |
"$1,332,439,889", | |
], | |
"ggg": [ | |
"$952,110,690", | |
"$393,151,347", | |
"$1,332,539,889", | |
"$1,332,439,889", | |
], | |
} | |
table = Printer.table(table, "t", "c") | |
Printer.print(table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment