pmotools.utils.small_utils module
- class pmotools.utils.small_utils.Utils[source]
Bases:
objectA small utility class to hold static methods for various 1-off tools
- static appendStrAsNeeded(input: str, ending: str)[source]
if a string doesn’t end with a specific ending, append it, this is useful for ensuring file extensions are in output names without accidentally doubling it
- Parameters:
input – the string to be appended
ending – the desired ending
- Returns:
the string with eh ending appended if it doesn’t already end with it
- static appendStrAsNeededDoubleEnding(input: str, ending1: str, ending2: str)[source]
if a string doesn’t end with a specific combination of endings (e.g. if ending of input does not equal ending1 + ending2), append it, this is useful for ensuring file extensions plus .gz for zipped files are in output names without accidentally doubling it
- Parameters:
input – the string to be appended
ending1 – the first part of the desired ending
ending2 – the second part of the desired ending
- Returns:
the string with the ending1 + ending2 appended if it doesn’t already end with it
- static get_file_if_size_diff(url, d)[source]
only download the file if it’s needed, not completely fail proof since it is just a size check but fairly likely not to be the same for a difference
- static inputOutputFileCheck(inputFile: str, outputFile: str, overwrite: bool = False)[source]
Check to see if an input file exists and if the output file exists if overwrite is turned on or not
- Parameters:
inputFile – the file that will be read in
outputFile – the output file that will be written to
overwrite – whether or not the output file can be overwritten
- Returns:
None
- static inputOutputFileCheckFromArgParse(args)[source]
Check if input file exists and if output file exist check if –overwrite flag is set
- Parameters:
args – parsed arguments from argparse, expecting the fields ‘file’, ‘output’, and ‘overwrite’
- Returns:
- static outputfile_check(output_file: str, overwrite: bool = False)[source]
Check to see if the output file exists if overwrite is turned on or not
- Parameters:
output_file – the output file that will be written to
overwrite – whether or not the output file can be overwritten
- Returns:
None
- static parse_delimited_input_or_file(input_args: str, delim: str = ',') list[str][source]
If the input is a file name then read in each line of the file for the argument, otherwise return a list of items delimited by delimiter
- Parameters:
input_args – the argument to parse or a name of a file to read in
delim – the delimiter to split on
- Returns:
a list of strings
- static process_delimiter_and_output_extension(delim: str, output_extension: str = '.txt', gzip: bool = False) tuple[str, str][source]
Process delimiter and extension, this allows for delim to be listed as tab or comma and it will replace appropriately the
- Parameters:
delim – the delimiter to process
output_extension – the output extension
gzip – whether or not to add .gz to the output extension as well
- Returns:
delimiter, extension
- static smart_open_read_autodetect(filename)[source]
Context manager for reading a file, using magic number autodetection of compression type.
Supports gzip, bzip2, lzma/xz, or plain text regardless of extension.
- Parameters:
filename (str) – “STDIN” or a file path
- Yields:
file object – Readable file-like object.
- static smart_open_read_by_ext(filename)[source]
Context manager for reading a file, using extension-based detection.
- Parameters:
filename (str) – “STDIN”, or a filename ending in .gz, .bz2, .xz, .lzma, or uncompressed.
- Yields:
file object – Readable file-like object.