first version
This commit is contained in:
5
utils/__init__.py
Normal file
5
utils/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
from .cprint import cprint
|
||||
from .json_load import json_load
|
||||
|
||||
|
42
utils/cprint.py
Normal file
42
utils/cprint.py
Normal file
@ -0,0 +1,42 @@
|
||||
from colorama import Fore
|
||||
|
||||
|
||||
COLORS = {
|
||||
"black": Fore.BLACK,
|
||||
"blue": Fore.BLUE,
|
||||
"cyan": Fore.CYAN,
|
||||
"green": Fore.GREEN,
|
||||
"magenta": Fore.MAGENTA,
|
||||
"red": Fore.RED,
|
||||
"white": Fore.WHITE,
|
||||
"yellow": Fore.YELLOW,
|
||||
None: None
|
||||
}
|
||||
|
||||
|
||||
def ncprint(*objects, color=None, sep=" ", **kwargs):
|
||||
return cprint(*objects, color=None, sep=sep, **kwargs)
|
||||
|
||||
def cprint(*objects, color=None, sep=" ", **kwargs):
|
||||
color = get_color(color)
|
||||
text = flatten_strings(objects, sep)
|
||||
return _print(color, text, sep, kwargs)
|
||||
|
||||
def get_color(color):
|
||||
try:
|
||||
return COLORS[color]
|
||||
except KeyError as exc:
|
||||
color = repr(color)
|
||||
allowed = tuple(COLORS.keys())
|
||||
raise ValueError(f"{color} not from {allowed}") from exc
|
||||
|
||||
def flatten_strings(objects, sep):
|
||||
return sep.join(str(i) for i in objects)
|
||||
|
||||
def _print(color, text, sep, kwargs):
|
||||
if color is not None:
|
||||
text = color + text + Fore.RESET
|
||||
return print(text, sep=sep, **kwargs)
|
||||
|
||||
|
||||
|
8
utils/json_load.py
Normal file
8
utils/json_load.py
Normal file
@ -0,0 +1,8 @@
|
||||
import json
|
||||
|
||||
|
||||
def json_load(filename, *args, **kwargs):
|
||||
with open(filename, "r") as f:
|
||||
return json.load(f, *args, **kwargs)
|
||||
|
||||
|
Reference in New Issue
Block a user