Module minder_utils.util.util
Expand source code
from pathlib import Path
import sys
import time
import os
import shutil
import pickle
import posixpath
import ntpath
import platform
def save_mkdir(path):
    Path(path).mkdir(parents=True, exist_ok=True)
def delete_dir(dirpath):
    if os.path.exists(dirpath) and os.path.isdir(dirpath):
        print('Deleting existing directory: ', dirpath)
        shutil.rmtree(dirpath)
def reformat_path(path):
    if isinstance(path, list):
        path = os.path.join(*path)
    if 'mac' in platform.platform().lower():
        return path
    elif 'windows' in platform.platform().lower():
        return str(path).replace(os.sep, ntpath.sep)
    elif 'unix' in platform.platform().lower():
        return path.replace(os.sep, posixpath.sep)
rocket_base_string = '[---------]'
rocket_progress_indicator_list1 = [rocket_base_string[:n] + '>=>' + rocket_base_string[n:] for n in range(1,11)]
rocket_progress_indicator_list2 = [rocket_base_string[:n] + '<=<' + rocket_base_string[n:] for n in range(1,11)]
rocket_progress_indicator_list = rocket_progress_indicator_list1 + rocket_progress_indicator_list2[::-1]
progress_indicator_dict = {'spinning_wheel': ['\\', '|', '/', '-', '\\', '|', '/', '-'],
                           'rocket': rocket_progress_indicator_list}
def progress_spinner(total_time, statement, new_line_after = True, progress_indicator = 'rocket', time_period = 1):
    '''
    This function prints a spinning wheel for the length of time given in ```total_time```.
    This is useful when it is required to wait for some amount of time but the user wants
    to print something. For example, when waiting for a server to complete its job.
    Arguments
    ---------
    - total_time: int:
        This is the total number of seconds this function will animate for.
    
    - statement: string:
        This is the statement to print before the spinning wheel
    
    - new_line_after: bool:
        This dictates whether to print a new line when the progress bar is finished.
    
    - progress_indicator: string or list:
        If string, make sure it is either ```'spinning_wheel'``` or ```'rocket'```. If it is a 
        list, the values in the list will be the frames of the animation.
    
    - time_period: float:
        This is the time period for the animation.
    '''
    if type(progress_indicator) == str:
        progress_indicator_list = progress_indicator_dict[progress_indicator]
    else: progress_indicator_list = progress_indicator
    sleep_value = time_period/len(progress_indicator_list)
    
    progress_position = 0
    
    time_run = 0
    start = time.time()
    
    while time_run < total_time:
        progress_position = progress_position%len(progress_indicator_list)
        sys.stdout.write('\r')
        sys.stdout.write("{} {}".format(statement, progress_indicator_list[progress_position]))
        sys.stdout.flush()
        
        time.sleep(sleep_value)
        progress_position += 1
        
        time_run = time.time() - start
    
    if new_line_after: sys.stdout.write('\n')
    
    
    returnFunctions
- def delete_dir(dirpath)
- 
Expand source codedef delete_dir(dirpath): if os.path.exists(dirpath) and os.path.isdir(dirpath): print('Deleting existing directory: ', dirpath) shutil.rmtree(dirpath)
- def progress_spinner(total_time, statement, new_line_after=True, progress_indicator='rocket', time_period=1)
- 
This function prints a spinning wheel for the length of time given in total_time. This is useful when it is required to wait for some amount of time but the user wants to print something. For example, when waiting for a server to complete its job.Arguments- 
total_time: int: This is the total number of seconds this function will animate for. 
- 
statement: string: This is the statement to print before the spinning wheel 
- 
new_line_after: bool: This dictates whether to print a new line when the progress bar is finished. 
- 
progress_indicator: string or list: If string, make sure it is either 'spinning_wheel'or'rocket'. If it is a list, the values in the list will be the frames of the animation.
- 
time_period: float: This is the time period for the animation. 
 Expand source codedef progress_spinner(total_time, statement, new_line_after = True, progress_indicator = 'rocket', time_period = 1): ''' This function prints a spinning wheel for the length of time given in ```total_time```. This is useful when it is required to wait for some amount of time but the user wants to print something. For example, when waiting for a server to complete its job. Arguments --------- - total_time: int: This is the total number of seconds this function will animate for. - statement: string: This is the statement to print before the spinning wheel - new_line_after: bool: This dictates whether to print a new line when the progress bar is finished. - progress_indicator: string or list: If string, make sure it is either ```'spinning_wheel'``` or ```'rocket'```. If it is a list, the values in the list will be the frames of the animation. - time_period: float: This is the time period for the animation. ''' if type(progress_indicator) == str: progress_indicator_list = progress_indicator_dict[progress_indicator] else: progress_indicator_list = progress_indicator sleep_value = time_period/len(progress_indicator_list) progress_position = 0 time_run = 0 start = time.time() while time_run < total_time: progress_position = progress_position%len(progress_indicator_list) sys.stdout.write('\r') sys.stdout.write("{} {}".format(statement, progress_indicator_list[progress_position])) sys.stdout.flush() time.sleep(sleep_value) progress_position += 1 time_run = time.time() - start if new_line_after: sys.stdout.write('\n') return
- 
- def reformat_path(path)
- 
Expand source codedef reformat_path(path): if isinstance(path, list): path = os.path.join(*path) if 'mac' in platform.platform().lower(): return path elif 'windows' in platform.platform().lower(): return str(path).replace(os.sep, ntpath.sep) elif 'unix' in platform.platform().lower(): return path.replace(os.sep, posixpath.sep)
- def save_mkdir(path)
- 
Expand source codedef save_mkdir(path): Path(path).mkdir(parents=True, exist_ok=True)