Skip to content

Instantly share code, notes, and snippets.

@mols3131d
Last active May 10, 2024 08:03
Show Gist options
  • Save mols3131d/a163902fd51f3c939e203cf34e727388 to your computer and use it in GitHub Desktop.
Save mols3131d/a163902fd51f3c939e203cf34e727388 to your computer and use it in GitHub Desktop.
python3-common
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# rich\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">hi\n",
"</pre>\n"
],
"text/plain": [
"hi\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">123</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1;36m123\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'A'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'B'</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>, <span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>, <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'apple'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'banana'</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span><span style=\"font-weight: bold\">]]</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1m[\u001b[0m\u001b[32m'A'\u001b[0m, \u001b[32m'B'\u001b[0m, \u001b[1;36m1\u001b[0m, \u001b[1;36m2\u001b[0m, \u001b[3;92mTrue\u001b[0m, \u001b[1m[\u001b[0m\u001b[32m'apple'\u001b[0m, \u001b[32m'banana'\u001b[0m, \u001b[1;36m11\u001b[0m, \u001b[1;36m12\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[3;92mTrue\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print\n",
"\n",
"print(\"hi\")\n",
"print(123)\n",
"print([\"A\", \"B\", 1, 2, True, [\"apple\", \"banana\", 11, 12]])\n",
"print(True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## logging.RichHandler\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:rich:unable print!\n",
"Traceback (most recent call last):\n",
" File \"C:\\Users\\sacer3q\\AppData\\Local\\Temp\\ipykernel_26880\\2617187636.py\", line 7, in <module>\n",
" print(1 / 0)\n",
" ~~^~~\n",
"ZeroDivisionError: division by zero\n"
]
}
],
"source": [
"import logging\n",
"from rich.logging import RichHandler\n",
"\n",
"\n",
"log = logging.getLogger(\"rich\")\n",
"try:\n",
" print(1 / 0)\n",
"except Exception:\n",
" log.exception(\"unable print!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"\n",
"from rich.logging import RichHandler\n",
"\n",
"\n",
"def get_logger(\n",
" name=__name__,\n",
" level=\"NOTSET\",\n",
" rich_format=\"%(message)s\",\n",
" file_format=\"[%(asctime)s][%(filename)s:%(funcName)s:%(lineno)s] %(levelname)s \\t%(message)s\",\n",
" file_path=\"./log.log\",\n",
") -> logging.Logger:\n",
"\n",
" # stream_handler\n",
" stream_handler = RichHandler(rich_tracebacks=True)\n",
" logging.basicConfig(\n",
" level=level,\n",
" format=rich_format,\n",
" datefmt=\"[%X]\",\n",
" handlers=[stream_handler],\n",
" )\n",
"\n",
" logger = logging.getLogger(\"rich\")\n",
"\n",
" # file_handler\n",
" file_handler = logging.FileHandler(file_path, mode=\"a\", encoding=\"utf-8\")\n",
" file_handler.setFormatter(logging.Formatter(file_format))\n",
" logger.addHandler(file_handler)\n",
"\n",
" return logger\n",
"\n",
"\n",
"l = get_logger()\n",
"l.info(\"test\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## my snippet\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Color\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"from rich.color import Color\n",
"\n",
"colors = [\n",
" \"red\",\n",
" \"yellow\",\n",
" \"green\",\n",
" \"cyan\",\n",
" \"blue\",\n",
" \"magenta\",\n",
"]\n",
"bright_colors = [f\"bright_{c}\" for c in colors]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### dict to table\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">movie </span>\n",
"┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓\n",
"┃<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\"> Released </span>┃<span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> Title </span>┃<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\"> Box Office </span>┃<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\"> Director </span>┃<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\"> Genre </span>┃<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> MPAA Rating </span>┃<span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\"> Running Time </span>┃\n",
"┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩\n",
"│<span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f\"> Dec 20, 2019 </span>│<span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\"> Star Wars: The </span>│<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\"> $952,110,690 </span>│<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\"> J.J. Abrams </span>│<span style=\"color: #7f7fbf; text-decoration-color: #7f7fbf\"> Sci-Fi, Action, </span>│<span style=\"color: #bf7fbf; text-decoration-color: #bf7fbf\"> PG-13 </span>│<span style=\"color: #ff7f7f; text-decoration-color: #ff7f7f\"> 142 </span>│\n",
"│<span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f\"> </span>│<span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\"> Rise of </span>│<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\"> </span>│<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\"> </span>│<span style=\"color: #7f7fbf; text-decoration-color: #7f7fbf\"> Adventure </span>│<span style=\"color: #bf7fbf; text-decoration-color: #bf7fbf\"> </span>│<span style=\"color: #ff7f7f; text-decoration-color: #ff7f7f\"> </span>│\n",
"│<span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f\"> </span>│<span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\"> Skywalker </span>│<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\"> </span>│<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\"> </span>│<span style=\"color: #7f7fbf; text-decoration-color: #7f7fbf\"> </span>│<span style=\"color: #bf7fbf; text-decoration-color: #bf7fbf\"> </span>│<span style=\"color: #ff7f7f; text-decoration-color: #ff7f7f\"> </span>│\n",
"│ │ │ │ │ │ │ │\n",
"│<span style=\"color: #800000; text-decoration-color: #800000\"> May 25, 2018 </span>│<span style=\"color: #808000; text-decoration-color: #808000\"> Solo: A Star </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> $393,151,347 </span>│<span style=\"color: #008080; text-decoration-color: #008080\"> Ron Howard </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> Sci-Fi, Action, </span>│<span style=\"color: #800080; text-decoration-color: #800080\"> PG-13 </span>│<span style=\"color: #ff0000; text-decoration-color: #ff0000\"> 135 </span>│\n",
"│<span style=\"color: #800000; text-decoration-color: #800000\"> </span>│<span style=\"color: #808000; text-decoration-color: #808000\"> Wars Story </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> </span>│<span style=\"color: #008080; text-decoration-color: #008080\"> </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> Western </span>│<span style=\"color: #800080; text-decoration-color: #800080\"> </span>│<span style=\"color: #ff0000; text-decoration-color: #ff0000\"> </span>│\n",
"│ │ │ │ │ │ │ │\n",
"│<span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f\"> Dec 15, 2017 </span>│<span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\"> Star Wars Ep. </span>│<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\"> $1,332,539,889 </span>│<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\"> Rian Johnson </span>│<span style=\"color: #7f7fbf; text-decoration-color: #7f7fbf\"> Sci-Fi, Action, </span>│<span style=\"color: #bf7fbf; text-decoration-color: #bf7fbf\"> TL </span>│<span style=\"color: #ff7f7f; text-decoration-color: #ff7f7f\"> 152 </span>│\n",
"│<span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f\"> </span>│<span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\"> VIII: The Last </span>│<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\"> </span>│<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\"> </span>│<span style=\"color: #7f7fbf; text-decoration-color: #7f7fbf\"> Adventure </span>│<span style=\"color: #bf7fbf; text-decoration-color: #bf7fbf\"> </span>│<span style=\"color: #ff7f7f; text-decoration-color: #ff7f7f\"> </span>│\n",
"│<span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f\"> </span>│<span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\"> Jedi </span>│<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\"> </span>│<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\"> </span>│<span style=\"color: #7f7fbf; text-decoration-color: #7f7fbf\"> </span>│<span style=\"color: #bf7fbf; text-decoration-color: #bf7fbf\"> </span>│<span style=\"color: #ff7f7f; text-decoration-color: #ff7f7f\"> </span>│\n",
"│ │ │ │ │ │ │ │\n",
"│<span style=\"color: #800000; text-decoration-color: #800000\"> Dec 16, 2016 </span>│<span style=\"color: #808000; text-decoration-color: #808000\"> Rogue One: A </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> $1,332,439,889 </span>│<span style=\"color: #008080; text-decoration-color: #008080\"> Gareth Edwards </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> Sci-Fi, Action, </span>│<span style=\"color: #800080; text-decoration-color: #800080\"> PG-13 </span>│<span style=\"color: #ff0000; text-decoration-color: #ff0000\"> 120 </span>│\n",
"│<span style=\"color: #800000; text-decoration-color: #800000\"> </span>│<span style=\"color: #808000; text-decoration-color: #808000\"> Star Wars Story </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> </span>│<span style=\"color: #008080; text-decoration-color: #008080\"> </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> War </span>│<span style=\"color: #800080; text-decoration-color: #800080\"> </span>│<span style=\"color: #ff0000; text-decoration-color: #ff0000\"> </span>│\n",
"└──────────────┴─────────────────┴────────────────┴────────────────┴─────────────────┴─────────────┴──────────────┘\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1mmovie \u001b[0m\n",
"┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓\n",
"┃\u001b[1;31m \u001b[0m\u001b[1;31mReleased \u001b[0m\u001b[1;31m \u001b[0m┃\u001b[1;33m \u001b[0m\u001b[1;33mTitle \u001b[0m\u001b[1;33m \u001b[0m┃\u001b[1;32m \u001b[0m\u001b[1;32mBox Office \u001b[0m\u001b[1;32m \u001b[0m┃\u001b[1;36m \u001b[0m\u001b[1;36mDirector \u001b[0m\u001b[1;36m \u001b[0m┃\u001b[1;34m \u001b[0m\u001b[1;34mGenre \u001b[0m\u001b[1;34m \u001b[0m┃\u001b[1;35m \u001b[0m\u001b[1;35mMPAA Rating\u001b[0m\u001b[1;35m \u001b[0m┃\u001b[1;91m \u001b[0m\u001b[1;91mRunning Time\u001b[0m\u001b[1;91m \u001b[0m┃\n",
"┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩\n",
"│\u001b[2;31m \u001b[0m\u001b[2;31mDec 20, 2019\u001b[0m\u001b[2;31m \u001b[0m│\u001b[2;33m \u001b[0m\u001b[2;33mStar Wars: The \u001b[0m\u001b[2;33m \u001b[0m│\u001b[2;32m \u001b[0m\u001b[2;32m$952,110,690 \u001b[0m\u001b[2;32m \u001b[0m│\u001b[2;36m \u001b[0m\u001b[2;36mJ.J. Abrams \u001b[0m\u001b[2;36m \u001b[0m│\u001b[2;34m \u001b[0m\u001b[2;34mSci-Fi, Action,\u001b[0m\u001b[2;34m \u001b[0m│\u001b[2;35m \u001b[0m\u001b[2;35mPG-13 \u001b[0m\u001b[2;35m \u001b[0m│\u001b[2;91m \u001b[0m\u001b[2;91m 142\u001b[0m\u001b[2;91m \u001b[0m│\n",
"│\u001b[2;31m \u001b[0m│\u001b[2;33m \u001b[0m\u001b[2;33mRise of \u001b[0m\u001b[2;33m \u001b[0m│\u001b[2;32m \u001b[0m│\u001b[2;36m \u001b[0m│\u001b[2;34m \u001b[0m\u001b[2;34mAdventure \u001b[0m\u001b[2;34m \u001b[0m│\u001b[2;35m \u001b[0m│\u001b[2;91m \u001b[0m│\n",
"│\u001b[2;31m \u001b[0m│\u001b[2;33m \u001b[0m\u001b[2;33mSkywalker \u001b[0m\u001b[2;33m \u001b[0m│\u001b[2;32m \u001b[0m│\u001b[2;36m \u001b[0m│\u001b[2;34m \u001b[0m│\u001b[2;35m \u001b[0m│\u001b[2;91m \u001b[0m│\n",
"│ │ │ │ │ │ │ │\n",
"│\u001b[31m \u001b[0m\u001b[31mMay 25, 2018\u001b[0m\u001b[31m \u001b[0m│\u001b[33m \u001b[0m\u001b[33mSolo: A Star \u001b[0m\u001b[33m \u001b[0m│\u001b[32m \u001b[0m\u001b[32m$393,151,347 \u001b[0m\u001b[32m \u001b[0m│\u001b[36m \u001b[0m\u001b[36mRon Howard \u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34mSci-Fi, Action,\u001b[0m\u001b[34m \u001b[0m│\u001b[35m \u001b[0m\u001b[35mPG-13 \u001b[0m\u001b[35m \u001b[0m│\u001b[91m \u001b[0m\u001b[91m 135\u001b[0m\u001b[91m \u001b[0m│\n",
"│\u001b[31m \u001b[0m│\u001b[33m \u001b[0m\u001b[33mWars Story \u001b[0m\u001b[33m \u001b[0m│\u001b[32m \u001b[0m│\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34mWestern \u001b[0m\u001b[34m \u001b[0m│\u001b[35m \u001b[0m│\u001b[91m \u001b[0m│\n",
"│ │ │ │ │ │ │ │\n",
"│\u001b[2;31m \u001b[0m\u001b[2;31mDec 15, 2017\u001b[0m\u001b[2;31m \u001b[0m│\u001b[2;33m \u001b[0m\u001b[2;33mStar Wars Ep. \u001b[0m\u001b[2;33m \u001b[0m│\u001b[2;32m \u001b[0m\u001b[2;32m$1,332,539,889\u001b[0m\u001b[2;32m \u001b[0m│\u001b[2;36m \u001b[0m\u001b[2;36mRian Johnson \u001b[0m\u001b[2;36m \u001b[0m│\u001b[2;34m \u001b[0m\u001b[2;34mSci-Fi, Action,\u001b[0m\u001b[2;34m \u001b[0m│\u001b[2;35m \u001b[0m\u001b[2;35mTL \u001b[0m\u001b[2;35m \u001b[0m│\u001b[2;91m \u001b[0m\u001b[2;91m 152\u001b[0m\u001b[2;91m \u001b[0m│\n",
"│\u001b[2;31m \u001b[0m│\u001b[2;33m \u001b[0m\u001b[2;33mVIII: The Last \u001b[0m\u001b[2;33m \u001b[0m│\u001b[2;32m \u001b[0m│\u001b[2;36m \u001b[0m│\u001b[2;34m \u001b[0m\u001b[2;34mAdventure \u001b[0m\u001b[2;34m \u001b[0m│\u001b[2;35m \u001b[0m│\u001b[2;91m \u001b[0m│\n",
"│\u001b[2;31m \u001b[0m│\u001b[2;33m \u001b[0m\u001b[2;33mJedi \u001b[0m\u001b[2;33m \u001b[0m│\u001b[2;32m \u001b[0m│\u001b[2;36m \u001b[0m│\u001b[2;34m \u001b[0m│\u001b[2;35m \u001b[0m│\u001b[2;91m \u001b[0m│\n",
"│ │ │ │ │ │ │ │\n",
"│\u001b[31m \u001b[0m\u001b[31mDec 16, 2016\u001b[0m\u001b[31m \u001b[0m│\u001b[33m \u001b[0m\u001b[33mRogue One: A \u001b[0m\u001b[33m \u001b[0m│\u001b[32m \u001b[0m\u001b[32m$1,332,439,889\u001b[0m\u001b[32m \u001b[0m│\u001b[36m \u001b[0m\u001b[36mGareth Edwards\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34mSci-Fi, Action,\u001b[0m\u001b[34m \u001b[0m│\u001b[35m \u001b[0m\u001b[35mPG-13 \u001b[0m\u001b[35m \u001b[0m│\u001b[91m \u001b[0m\u001b[91m 120\u001b[0m\u001b[91m \u001b[0m│\n",
"│\u001b[31m \u001b[0m│\u001b[33m \u001b[0m\u001b[33mStar Wars Story\u001b[0m\u001b[33m \u001b[0m│\u001b[32m \u001b[0m│\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34mWar \u001b[0m\u001b[34m \u001b[0m│\u001b[35m \u001b[0m│\u001b[91m \u001b[0m│\n",
"└──────────────┴─────────────────┴────────────────┴────────────────┴─────────────────┴─────────────┴──────────────┘\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import datetime\n",
"from typing import Literal\n",
"\n",
"from numpy import dtype\n",
"from regex import F\n",
"from rich import print\n",
"from rich.color import Color\n",
"from rich.console import Console, JustifyMethod\n",
"from rich.padding import Padding\n",
"from rich.panel import Panel\n",
"from rich.pretty import Pretty\n",
"from rich.rule import Rule\n",
"from rich.style import Style\n",
"from rich.table import Table\n",
"from rich.text import Text\n",
"\n",
"colors = [\n",
" \"red\",\n",
" \"yellow\",\n",
" \"green\",\n",
" \"cyan\",\n",
" \"blue\",\n",
" \"magenta\",\n",
"]\n",
"bright_colors = [f\"bright_{c}\" for c in colors]\n",
"\n",
"\n",
"def _justify_right(data) -> bool:\n",
" \"\"\"## _summary_\n",
"\n",
" ### Args:\n",
" - `data (_type_)`: _description_\n",
"\n",
" ### Returns:\n",
" - `bool`: _description_\n",
"\n",
" ### Examples:\n",
" >>> _justify_right(123)\n",
" True\n",
"\n",
" >>> _justify_right(\"123,456\")\n",
" True\n",
"\n",
" >>> _justify_right(\"$123,456\")\n",
" False\n",
" \"\"\"\n",
" if isinstance(data, int):\n",
" return True\n",
"\n",
" if str(data).replace(\",\", \"\").replace(\".\", \"\").isdigit():\n",
" return True\n",
"\n",
" return False\n",
"\n",
"\n",
"def dict_to_table(\n",
" data,\n",
" title=None,\n",
" caption=None,\n",
" title_justify: \"JustifyMethod\" = \"left\",\n",
" caption_justify: \"JustifyMethod\" = \"left\",\n",
" table_dictargs: dict = {},\n",
" colorful: bool = True\n",
") -> Table:\n",
" style = Style(\n",
" bgcolor=None,\n",
" bold=True,\n",
" )\n",
" title = Text(title, style=style)\n",
"\n",
" table = Table(\n",
" **table_dictargs,\n",
" title=title,\n",
" caption=caption,\n",
" title_justify=title_justify,\n",
" caption_justify=caption_justify,\n",
" leading=True,\n",
" row_styles=[\"dim\", \"\"],\n",
" )\n",
" \n",
" # colors\n",
" if colorful:\n",
" colors = [\n",
" \"red\",\n",
" \"yellow\",\n",
" \"green\",\n",
" \"cyan\",\n",
" \"blue\",\n",
" \"magenta\",\n",
" ]\n",
" bright_colors = [f\"bright_{c}\" for c in colors]\n",
"\n",
" # add cols\n",
" colors = colors + bright_colors\n",
"\n",
" \n",
" for i, col_name in enumerate(data.keys()):\n",
" # 컬럼의 데이터 타입을 확인하여 col_justify를 설정.\n",
" col_justify = \"left\"\n",
" try:\n",
" if data[col_name][0]:\n",
" if _justify_right(data[col_name][0]):\n",
" col_justify = \"right\"\n",
" except:\n",
" pass\n",
"\n",
" col_dictargs = {\n",
" \"justify\": col_justify,\n",
" \"style\": Style(\n",
" color=Color.parse(colors[i % len(colors)]) if colorful else None,\n",
" bgcolor=None,\n",
" bold=False,\n",
" ),\n",
" \"header_style\": Style(\n",
" color=Color.parse(colors[i % len(colors)]) if colorful else None,\n",
" bgcolor=None,\n",
" bold=True,\n",
" ),\n",
" }\n",
" col_text = Text(col_name)\n",
" table.add_column(header=col_text, footer=col_text, **col_dictargs)\n",
"\n",
" # add rows\n",
" for i in range(len(next(iter(data.values())))):\n",
" row_texts = []\n",
" for col_name, col_data in data.items():\n",
" text = str(col_data[i])\n",
" row_texts.append(Text(text))\n",
"\n",
" table.add_row(*row_texts)\n",
"\n",
" return table\n",
"\n",
"\n",
"data = {\n",
" \"Released\": [\"Dec 20, 2019\", \"May 25, 2018\", \"Dec 15, 2017\", \"Dec 16, 2016\"],\n",
" \"Title\": [\n",
" \"Star Wars: The Rise of Skywalker\",\n",
" \"Solo: A Star Wars Story\",\n",
" \"Star Wars Ep. VIII: The Last Jedi\",\n",
" \"Rogue One: A Star Wars Story\",\n",
" ],\n",
" \"Box Office\": [\"$952,110,690\", \"$393,151,347\", \"$1,332,539,889\", \"$1,332,439,889\"],\n",
" \"Director\": [\"J.J. Abrams\", \"Ron Howard\", \"Rian Johnson\", \"Gareth Edwards\"],\n",
" \"Genre\": [\n",
" \"Sci-Fi, Action, Adventure\",\n",
" \"Sci-Fi, Action, Western\",\n",
" \"Sci-Fi, Action, Adventure\",\n",
" \"Sci-Fi, Action, War\",\n",
" ],\n",
" \"MPAA Rating\": [\"PG-13\", \"PG-13\", \"TL\", \"PG-13\"],\n",
" \"Running Time\": [142, 135, 152, 120],\n",
"}\n",
"\n",
"t = dict_to_table(data, title=\"movie\", colorful=True)\n",
"print(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Printer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"from typing import Literal\n",
"\n",
"\n",
"from rich.color import Color\n",
"from rich.console import Console, JustifyMethod\n",
"from rich.padding import Padding\n",
"from rich.panel import Panel\n",
"from rich.pretty import Pretty\n",
"from rich.rule import Rule\n",
"from rich.style import Style\n",
"from rich.table import Table\n",
"from rich.text import Text\n",
"\n",
"\n",
"class Printer:\n",
"\n",
" colors = [\n",
" \"red\",\n",
" \"yellow\",\n",
" \"green\",\n",
" \"cyan\",\n",
" \"blue\",\n",
" \"magenta\",\n",
" ]\n",
" bright_colors = [f\"bright_{c}\" for c in colors]\n",
"\n",
" console = Console()\n",
" text_dictargs = {\"style\": \"bold\"}\n",
" rule_dictargs = {\"style\": \"bold\"}\n",
" pretty_dictargs = ({},)\n",
" panel_dictargs = {\n",
" \"title\": None,\n",
" \"title_align\": \"center\",\n",
" \"subtitle\": None,\n",
" \"subtitle_align\": \"center\",\n",
" \"expand\": \"\",\n",
" \"style\": \"bold\",\n",
" \"width\": None,\n",
" \"height\": None,\n",
" \"padding\": (0, 1),\n",
" \"highlight\": False,\n",
" }\n",
" padding_dictargs = {\n",
" \"pad\": (0, 0, 0, 0),\n",
" \"style\": \"\",\n",
" \"expand\": True,\n",
" }\n",
"\n",
" @classmethod\n",
" def print(\n",
" cls,\n",
" data,\n",
" text_dictargs={\"style\": \"\"},\n",
" rule_dictargs={\"style\": \"\"},\n",
" pretty_dictargs={},\n",
" panel_dictargs={},\n",
" padding_dictargs={},\n",
" print_dictargs={},\n",
" instead_log=False,\n",
" ):\n",
" _flag = None\n",
" d = data\n",
"\n",
" if isinstance(d, str):\n",
" if d[:3] == \"---\" and d[-3:] == \"---\":\n",
" d = d[3:-3]\n",
" d = Text(d, **text_dictargs)\n",
" d = Rule(d, **rule_dictargs)\n",
"\n",
" else:\n",
" d = Text(d, **text_dictargs)\n",
"\n",
" elif isinstance(d, Table):\n",
" pass\n",
"\n",
" else:\n",
" d = Pretty(d, **pretty_dictargs)\n",
"\n",
" # panel\n",
" if panel_dictargs:\n",
" d = Panel(d, **panel_dictargs)\n",
"\n",
" # padding\n",
" if padding_dictargs:\n",
" d = Padding(d, **padding_dictargs)\n",
"\n",
" # print or log\n",
" if instead_log:\n",
" cls.console.log(d, **print_dictargs)\n",
" else:\n",
" cls.console.print(d, **print_dictargs)\n",
"\n",
" @classmethod\n",
" def log(\n",
" cls,\n",
" data,\n",
" text_dictargs={\"style\": \"\"},\n",
" rule_dictargs={\"style\": \"\"},\n",
" pretty_dictargs={},\n",
" panel_dictargs={},\n",
" padding_dictargs={},\n",
" print_dictargs={},\n",
" ):\n",
" cls.print(\n",
" data,\n",
" text_dictargs=text_dictargs,\n",
" rule_dictargs=rule_dictargs,\n",
" pretty_dictargs=pretty_dictargs,\n",
" panel_dictargs=panel_dictargs,\n",
" padding_dictargs=padding_dictargs,\n",
" print_dictargs=print_dictargs,\n",
" instead_log=True,\n",
" )\n",
"\n",
" @classmethod\n",
" def panel(\n",
" cls,\n",
" data,\n",
" panel_dictargs={\n",
" \"title\": None,\n",
" \"title_align\": \"center\",\n",
" \"subtitle\": None,\n",
" \"subtitle_align\": \"center\",\n",
" \"expand\": \"\",\n",
" \"style\": \"bold\",\n",
" \"width\": None,\n",
" \"height\": None,\n",
" \"padding\": (0, 1),\n",
" \"highlight\": False,\n",
" },\n",
" ):\n",
" d = data\n",
"\n",
" d = Panel(d, **panel_dictargs)\n",
"\n",
" return d"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "crawler",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment