"""Common Utils
"""
from datetime import datetime
from typing import Any
from typing import Dict
from typing import List
from typing import Union
import pytz
from texttable import Texttable
[docs]def get_str_time(timezone="Asia/Shanghai"):
"""Return localtime in the format of %Y-%m-%d-%H:%M:%S."""
# Set the timezone to timezone
pytz_timezone = pytz.timezone(timezone)
# Get the current time in UTC
utc_now = datetime.utcnow()
# Convert UTC time to timezone time
pytz_now = utc_now.replace(tzinfo=pytz.utc).astimezone(pytz_timezone)
return pytz_now.strftime("%Y-%m-%d-%H:%M:%S")
[docs]def tab_printer(
args: Dict,
thead: List[str] = None,
cols_align: List[str] = None,
cols_valign: List[str] = None,
cols_dtype: List[str] = None,
sort: bool = True,
verbose: Union[bool, int] = True,
) -> None:
"""Function to print the logs in a nice tabular format.
Args:
args (Dict): value dict.
thead (List[str], optional): table head. Defaults to None.
cols_align (List[str], optional): horizontal alignment of the columns. Defaults to None.
cols_valign (List[str], optional): vertical alignment of the columns. Defaults to None.
cols_dtype (List[str], optional): value types of the columns. Defaults to None.
sort (bool, optional): whether to sort the keys. Defaults to True.
Returns:
str: table string to print.
"""
args = vars(args) if hasattr(args, "__dict__") else args
keys = sorted(args.keys()) if sort else args.keys()
table = Texttable()
table.set_precision(5)
params = [[] if thead is None else thead]
params.extend(
[
[
k.replace("_", " "),
f"{args[k]}" if isinstance(args[k], bool) else format_value(args[k]),
] for k in keys
]
)
if cols_align is not None:
table.set_cols_align(cols_align)
if cols_valign is not None:
table.set_cols_valign(cols_valign)
if cols_dtype is not None:
table.set_cols_dtype(cols_dtype)
table.add_rows(params)
if verbose:
print(table.draw())
return table.draw()