teilab.utils.generic_utils module¶
- teilab.utils.generic_utils.now_str(tz: Optional[datetime.timezone] = None, fmt: str = '%Y-%m-%d@%H.%M.%S') str [source]¶
Returns new datetime string representing current time local to tz under the control of an explicit format string.
- Parameters
tz (Optional[datetime.timezone], optional) – Timezone object. If no
tz
is specified, uses local timezone. Defaults toNone
.fmt (str, optional) – format string. See Python Documentation Defaults to
"%Y-%m-%d@%H.%M.%S"
.
- Returns
Formatted current time.
- Return type
str
Example
>>> from teilab.utils import now_str >>> now_str() '2021-06-01@11.22.14' >>> now_str(tz=datetime.timezone.utc) '2021-06-01@02.22.16' >>> now_str(fmt="%A, %d. %B %Y %I:%M%p") 'Tuesday, 01. June 2021 11:22AM'
- teilab.utils.generic_utils.readable_bytes(byte: numbers.Number) Tuple[numbers.Number, str] [source]¶
Unit conversion for readability.
- Parameters
byte (Number) – File byte [B].
Examples
>>> from teilab.utils import readable_bytes >>> for i in range(1,30,3): ... byte = pow(10,i) ... size, unit = readable_bytes(pow(10,i)) ... print(f"{byte:.1g}[B] = {size:.2f}[{unit}]") 1e+01[B] = 10.00[B] 1e+04[B] = 9.77[KB] 1e+07[B] = 9.54[MB] 1e+10[B] = 9.31[GB] 1e+13[B] = 9.09[TB] 1e+16[B] = 8.88[PB] 1e+19[B] = 8.67[EB] 1e+22[B] = 8.47[ZB] 1e+25[B] = 8.27[YB] 1e+28[B] = 8271.81[YB]
- teilab.utils.generic_utils.progress_reporthook_create(filename: str = '', bar_width: int = 20, verbose: bool = True) Callable[[int, int, int], None] [source]¶
Create a progress reporthook for
urllib.request.urlretrieve
- Parameters
filename (str, optional) – Downloading filename.. Defaults to
""
.bar_width (int, optional) – The width of progress bar. Defaults to
20
.verbose (bool, optional) – Whether to output the status. Defaults to
True
.
- Returns
The
reporthook
which is a callable that accepts ablock number
, aread size
, and thetotal file size
of the URL target.- Return type
Callable[[int,int,int], None]
Examples
>>> import urllib >>> from teilab.utils import progress_reporthook_create >>> urllib.request.urlretrieve(url="hoge.zip", filename="hoge.zip", reporthook=progress_reporthook_create(filename="hoge.zip")) hoge.zip 1.5%[--------------------] 21.5[s] 8.0[GB/s] eta 1415.1[s]
- teilab.utils.generic_utils.verbose2print(verbose: bool = True) callable [source]¶
Create a simple print function based on verbose
- Parameters
verbose (bool, optional) – Whether to print or not. Defaults to
True
.- Returns
Print function
- Return type
callable
Examples
>>> from teilab.utils import verbose2print >>> print_verbose = verbose2print(verbose=True) >>> print_non_verbose = verbose2print(verbose=False) >>> print_verbose("Hello, world.") Hello, world. >>> print_non_verbose("Hello, world.")
- teilab.utils.generic_utils.dict2str(d: Dict[Any, Any], item_separator: str = ', ', key_separator: str = '=') str [source]¶
Convert a dictionary to string.
- Parameters
d (Dict[Any,Any]) – A dictionary.
item_separator (str, optional) – A separator between items. Defaults to
", "
.key_separator (str, optional) – A separator between keys. Defaults to
"="
.
- Returns
A textual summary of the contents of the dictionary.
- Return type
str
Examples
>>> from teilab.utils import dict2str >>> dict2str({"key1":"val", "key2":1}) 'key1=val, key2=1' >>> dict2str({"key1":"val", "key2":1}, key_separator=":") 'key1:val, key2:1' >>> dict2str({"key1":"val", "key2":1}, item_separator="🤔") 'key1=val🤔key2=1'
- teilab.utils.generic_utils.check_supported(lst: List[Any], **kwargs)[source]¶
Check whether all
kwargs.values()
in thelst
.- Parameters
lst (List[Any]) – candidates for each value in
kwargs.values()
.kwargs –
key
is the varname that is easy to understand when an error occurs
Examples
>>> from teilab.utils import check_supported >>> check_supported(lst=range(3), val=1) >>> check_supported(lst=range(3), val=100) KeyError: Please choose the argment 'val' from ['0', '1', '2'], but you chose '100' >>> check_supported(lst=range(3), val1=1, val2=2) >>> check_supported(lst=range(3), val1=1, val2=100) KeyError: Please choose the argment 'val2' from ['0', '1', '2'], but you chose '100'
- Raises
KeyError – If
kwargs.values()
not in thelst