-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_utils.py
More file actions
75 lines (63 loc) · 1.98 KB
/
console_utils.py
File metadata and controls
75 lines (63 loc) · 1.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Utility functions for console applications.
"""
import os
import sys
import logging
from typing import Union
from typing import List
from colorlog import ColoredFormatter
def setup_logging(
path: str = "/tmp",
file_name: Union[str, List[str]] = "file.log",
level: int = logging.INFO,
use_global: bool = True,
) -> logging.Logger:
"""Setup logging configuration.
Output logs to a file and stdout."""
if isinstance(file_name, str):
file_names = [file_name]
else:
file_names = file_name
if not os.path.exists(path):
os.makedirs(path)
logger_name = file_names[0]
if use_global:
logger = logging.getLogger()
else:
logger = logging.getLogger(logger_name)
logger.setLevel(level)
logger.handlers.clear()
formatter = ColoredFormatter(
fmt="[%(asctime)s.%(msecs)03d] %(log_color)s%(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
log_colors={
"DEBUG": "cyan",
"INFO": "white",
# "INFO": None,
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
}
)
for file_name in file_names:
file_handler = logging.FileHandler(f"{path}/{file_name}")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)
return logger
def bytes_to_human(num_bytes: int | float) -> str:
"""Convert a byte count to a human-readable string."""
if num_bytes < 0:
raise ValueError("num_bytes must be non-negative")
units = ["B", "KB", "MB", "GB", "TB", "PB", "EB"]
value = float(num_bytes)
for unit in units:
if value < 1024.0 or unit == units[-1]:
if unit == "B":
return f"{int(value)} {unit}"
return f"{value:.2f} {unit}"
value /= 1024.0
return f"{value:.2f} EB"