Misc_utils¶
Misc system & data process utils
- Usage:
>>> import misc_utils as utils >>> utils.func_name() # to call functions in this file
-
misc_utils.misc_utils.color_print(text='', color=0, end='\n')[source]¶ Print colored text.
Parameters: - text (str) – text to print.
- color (int) –
- 0 black
- 1 red
- 2 green
- 3 yellow
- 4 blue
- 5 cyan (like light red)
- 6 magenta (like light blue)
- 7 white
- end (str) – end string after colored text.
- Example
>>> color_print('yellow', 3)
-
misc_utils.misc_utils.format_num(num: int) → str[source]¶ Add comma in every three digits (return a string).
Parameters: num (int) – a number. - Examples
>>> format_num(10000) # 10,000 >>> format_num(123456789) # 123,456,789
-
misc_utils.misc_utils.format_time(seconds)[source]¶ Convert seconds to formatted time string.
Parameters: seconds (int) – second number. - Examples
>>> format_time(10) # 10s >>> format_time(100) # 1m >>> format_time(10000) # 2h 47m >>> format_time(1000000) # 11d 13h 47m
-
misc_utils.misc_utils.get_dir_name(path)[source]¶ Get parent directory name.
- Args
- path(str): file’s abs path.
- Returns
- dirname.
- Example
>>> get_dir_name('root/train/0001.jpg') # mode/train >>> get_dir_name(get_dir_name('root/train/0001.jpg')) # root
-
misc_utils.misc_utils.get_file_name(path)[source]¶ Get filename by path (without extension).
- Args
- path(str): file’s abs path.
- Returns
- filename (without extension).
- Example
>>> get_file_name('train/0001.jpg') # 0001
-
misc_utils.misc_utils.get_file_paths_by_pattern(pattern='*', folder='.')[source]¶ Get a file path list matched given pattern.
Parameters: - pattern (str) – a pattern to match files.
- folder (str) – searching folder.
- Returns
- (list of str): a list of matching paths.
- Examples
>>> get_file_paths_by_pattern('*.png') # get all *.png files in folder >>> get_file_paths_by_pattern('*rotate*') # get all files with 'rotate' in name
-
misc_utils.misc_utils.get_logger(f='log.txt', mode='w', level='info', print_stream=True)[source]¶ Get a logger.
Parameters: - f (str) – log file path.
- mode (str) – ‘w’ or ‘a’.
- level (str) – ‘debug’ or ‘info’.
- print_stream (bool) – if print to terminal or not.
Returns: A logger.
- Example
>>> logger = get_logger(level='debug') >>> logger.info("test")
-
misc_utils.misc_utils.get_time_stamp(add_offset=0)[source]¶ Get time_zone+0 unix time stamp (seconds)
Parameters: add_offset (int) – bias added to time stamp Returns: time stamp seconds Return type: (str)
-
misc_utils.misc_utils.get_time_stamp_by_format_str(time_str: str, fmt='%Y/%m/%d %H:%M:%S', timezone=8)[source]¶ Get timestamp by formatted time string.
Parameters: - time_str (str) – string in fmt format.
- fmt (str) – format.
- timezone (int) – time zone.
Returns: time stamp
Return type: (str)
Example
>>> get_time_stamp_by_format_str('2020/01/01 15:30:00') >>> # 1577863800
-
misc_utils.misc_utils.get_time_str(time_stamp='1578671565', fmt='%Y/%m/%d %H:%M:%S', timezone=8, year_length=4)[source]¶ Get formatted time string.
Parameters: - time_stamp (str) – linux time string (seconds).
- fmt (str) – string format.
- timezone (int) – time zone.
- year_length (int) – 2 or 4.
Returns: formatted time string.
Return type: (str)
Example
>>> get_time_str() >>> # 2020/01/01 13:30:00
-
misc_utils.misc_utils.is_file_image(filename)[source]¶ Return if a file’s extension is an image’s.
Parameters: filename (str) – file path. Returns: if the file is image or not. Return type: (bool)
-
misc_utils.misc_utils.p(obj)[source]¶ Recursively print list, tuple or dict items
Parameters: obj (list, tuple or dict) – a list, tuple or dict to print.
-
misc_utils.misc_utils.print_args(args)[source]¶ Print args parsed by argparse.
Parameters: args – args parsed by argparse. - Example
>>> parser = argparse.ArgumentParser() >>> args = parser.parse_args() >>> print_args(args)
-
misc_utils.misc_utils.progress_bar(current, total, pre_msg=None, msg=None)[source]¶ Render a progress_bar in terminal.
- Preview
- Training… Step: [=======>… 26/100 ………..] ETA: 0s | loss:0.45
Parameters: - current (int) – current counter, range in [0, total-1].
- total (int) – total counts.
- pre_msg (str) – message before the progress bar.
- msg (str) – message after the progress bar.
- Example
>>> for i in range(100): >>> progress_bar(i, 100, 'Training...', 'loss:0.45')
-
misc_utils.misc_utils.safe_key(dic: dict, key, default=None)[source]¶ Return dict[key] if dict has the key, in case of KeyError.
Parameters: - dic (dict) – a dictionary.
- key (usually str or int) – key.
- default – default return value.
Returns: dic[key] if key in dic else default.
-
misc_utils.misc_utils.to_string(obj, last_comma=False)[source]¶ Convert to string in one line.
Parameters: - obj (list, tuple or dict) – a list, tuple or dict to convert.
- last_comma (bool) – add a comma at last.
Returns: (str) string.
Example
>>> to_string([1, 2, 3, 4], last_comma=True) >>> # 1, 2, 3, 4, >>> to_string({'a': 2,'b': 4}) >>> # a=2, b=4