pmotools.utils.small_utils module

class pmotools.utils.small_utils.Utils[source]

Bases: object

A 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 clear_dir(d)[source]

forcibly delete directory and then re-make it

static connectedInternet()[source]
static getStrFromStrOrList(inputArg)[source]
static get_file(url, d)[source]

get file from url and put it into directory d, return new name

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 hasProgram(program)[source]
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 isMac()[source]
static mkdir(d)[source]

mkdir if it doesn’t already exist

static num_cores()[source]
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 rm_rf(d)[source]

remove directory forcibly

static run(cmd)[source]
static runAndCapture(cmd)[source]
static run_in_dir(cmd, d)[source]
static shellquote(s)[source]
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.

static smart_open_write(filename)[source]

Context manager for writing to a file, stdout, or a gzip-compressed file.

Parameters:

filename (str) – Output filename, “STDOUT” for standard output, or a filename ending in “.gz” for gzip compression.

Yields:

file object – A writable file-like object.

static untar(fnp, d)[source]

un pack compressed file, guessing format based on extension

static which(program)[source]