Utils
A collection of more general utility functions.
LoggerUtility
Utility class for setting up logging consistently.
Source code in maestrowf/utils.py
__init__(logger)
Initialize a new LoggerUtility class instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
An instance of a logger to configure. |
required |
add_file_handler(log_path, log_format, log_lvl=2)
Add a file handler to logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_path |
String containing the file path to store logging. |
required | |
log_format |
String containing the desired logging format. |
required | |
log_lvl |
Integer level (1-5) to set the logger to. |
2
|
Source code in maestrowf/utils.py
add_stream_handler(log_format, log_lvl=2)
Add a stream handler to logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_format |
String containing the desired logging format. |
required | |
log_lvl |
Integer level (1-5) to set the logger to. |
2
|
Source code in maestrowf/utils.py
configure(log_format, log_lvl=2, colors=True)
Configures the general logging facility.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_format |
String containing the desired logging format. |
required | |
log_lvl |
Integer level (1-5) to set the logger to. |
2
|
Source code in maestrowf/utils.py
map_level(log_lvl)
staticmethod
Map level 1-5 to their respective logging enumerations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_lvl |
Integer level (1-5) representing logging verbosity. |
required |
Source code in maestrowf/utils.py
apply_function(item, func)
Apply a function to items depending on type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
A Python primitive to apply a function to. |
required | |
func |
Function that returns takes item as a parameter and returns item modified in some way. |
required |
Source code in maestrowf/utils.py
create_dictionary(list_keyvalues, token=':')
Create a dictionary from a list of key-value pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
list_keyvalues |
List of token separates key-values. |
required | |
token |
The token to split each key-value by. |
':'
|
Returns:
Type | Description |
---|---|
A dictionary containing the key-value pairings in list_keyvalues. |
Source code in maestrowf/utils.py
create_parentdir(path)
Recursively create parent directories.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path to a directory to be created. |
required |
Source code in maestrowf/utils.py
csvtable_to_dict(fstream)
Convert a csv file stream into an in memory dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fstream |
An open file stream to a csv table (with header) |
required |
Returns:
Type | Description |
---|---|
A dictionary with a key for each column header and a list of column values for each key. |
Source code in maestrowf/utils.py
generate_filename(path, append_time=True)
Generate a non-conflicting file name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path to file. |
required | |
append_time |
Setting to append a timestamp. |
True
|
Source code in maestrowf/utils.py
get_duration(time_delta)
Convert durations to HH:MM:SS format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_delta |
A time difference in datatime format. |
required |
Returns:
Type | Description |
---|---|
A formatted string in HH:MM:SS |
Source code in maestrowf/utils.py
make_safe_path(base_path, *args)
Construct a subpath that is path safe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_path |
The base path to append args to. |
required | |
args |
Path components to join into a path. |
()
|
Returns:
Type | Description |
---|---|
A joined subpath with invalid characters stripped. |
Source code in maestrowf/utils.py
parse_version(version_string)
Attempts using pep440 compliant version format and then falls back to a semver format to handle things like flux's version formats which can be a combination of the two.
Note: only major/minor/patch returned from semver parser; additional modifiers currently ignored for comparison purposes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version_string |
version string to parse |
required |
Returns:
Type | Description |
---|---|
Version object, or None |
Source code in maestrowf/utils.py
ping_url(url)
Load a webpage to test that it is accessible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
URL string to be loaded. |
required |
Source code in maestrowf/utils.py
round_datetime_seconds(input_datetime)
Round datetime to the nearest whole second.
Solution referenced from: https://stackoverflow.com/questions/47792242/ rounding-time-off-to-the-nearest-second-python.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_datetime |
A datetime in datatime format. |
required |
Returns:
Type | Description |
---|---|
|
Source code in maestrowf/utils.py
start_process(cmd, cwd=None, env=None, shell=True)
Start a new process using a specified command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd |
A string or a list representing the command to be run. |
required | |
cwd |
Current working path that the process will be started in. |
None
|
|
env |
A dictionary containing the environment the process will use. |
None
|
|
shell |
Boolean that determines if the process will run a shell. |
True
|