-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
47 lines (30 loc) · 1.03 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys
from enum import StrEnum
from typing import Any, Union, Sequence
Numeric = Union[int, float]
# output table properties
WIDTH = 8
PRECISION = 2
class Color(StrEnum):
RED = '\033[91m'
BLUE = '\033[94m'
GREEN = '\033[92m'
PURPLE = '\033[95m'
END = '\033[0m'
# TODO: replace with "logging"
def print_note(*msg: Sequence[Any]) -> None:
print(f"{Color.BLUE}[NOTE]{Color.END}", *msg, file=sys.stderr)
def print_wrn(*msg: Sequence[Any]) -> None:
print(f"{Color.PURPLE}[WARNING]{Color.END}", *msg, file=sys.stderr)
def print_err(*msg: Sequence[Any], interrupt: bool = True) -> None:
print(f"{Color.RED}[ERROR]{Color.END}", *msg, file=sys.stderr)
if interrupt:
exit(1)
def balance_color(num: Numeric, padding: int = 0, unit: str = "") -> str:
if isinstance(num, float):
out = f"{num:.{PRECISION}f}"
if unit != "" and not unit.startswith(" "):
unit = " " + unit
return (
f"{Color.RED if num < 0 else Color.GREEN}"
f"{out:>{padding}}{unit}{Color.END}")