Utility functions

Package-wide utility functions.

reemission.utils.add_version(fun: Callable) Callable[source]

Adds version of the tool to the help heading.

Parameters:

fun – Function to decorate.

Returns:

Decorated function.

Return type:

Callable

reemission.utils.clean_folder(folder_path: Path | str) None[source]

Removes all subfolders and files in a given folder.

Parameters:

folder_path – Path to the folder to be cleaned.

reemission.utils.debug_on_exception(func: Callable) Callable[source]

Wrapper for debugging functions that raise exceptions. Enters debug mode if an exception is raised.

Parameters:

func – Function to be wrapped.

Returns:

Wrapped function.

Return type:

Callable

reemission.utils.find_enum_index(enum: EnumType, to_find: Enum) int[source]

Finds index of an item in an Enum class corresponding to an item given in to_find.

Parameters:
  • enum – Enum object in which the item to find is stored.

  • to_find – Key in the enum object.

Returns:

Index of the item to find if the item exists in enum.

Return type:

int

Raises:

KeyError – If index could not be found.

reemission.utils.get_folder_size(folder_path: Path | str) float[source]

Calculates size in bytes for all files in a given folder.

Parameters:

folder_path – Path to the folder.

Returns:

Total size of the folder in bytes.

Return type:

float

reemission.utils.get_package_file(*folders: str) Path[source]

Imports package data using importlib functionality.

Parameters:

*folders – Comma-separated strings representing path to the packaged data file.

Returns:

A os-independent path of the data file.

Return type:

pathlib.Path

reemission.utils.is_directory(input_path: Path) bool[source]

Check if the path is a directory by checking if split_path returns file.

Parameters:

input_path – Path to be checked.

Returns:

True if the path is a directory, False otherwise.

Return type:

bool

reemission.utils.is_latex_installed() bool[source]

Checks if LaTeX is available as a command.

Returns:

True if LaTeX is installed, False otherwise.

Return type:

bool

reemission.utils.load_csv(path: Path) DataFrame[source]

Opens a csv file with tabular data and loads it into a pandas DataFrame.

Parameters:

path – Path to the csv file.

Returns:

A DataFrame containing the csv data.

Return type:

pd.DataFrame

reemission.utils.load_geojson(path: Path) GeoDataFrame[source]

Opens a geojson file using geopandas and returns a GeoDataFrame.

Parameters:

path – Path to the geojson file.

Returns:

A GeoDataFrame containing the geojson data.

Return type:

gpd.GeoDataFrame

reemission.utils.load_json(file_path: Path) Dict[source]

Load json file.

Parameters:

file_path – Path to the json file.

Returns:

A dictionary representation of the json file.

Return type:

Dict

reemission.utils.load_shape(path: Path) GeoDataFrame[source]

Opens a shape file using geopandas and returns a GeoDataFrame.

Parameters:

path – Path to the shape file.

Returns:

A GeoDataFrame containing the shape data.

Return type:

gpd.GeoDataFrame

reemission.utils.load_toml(file_path: Path) Dict[source]

Load toml file.

Parameters:

file_path – Path to the toml file.

Returns:

A dictionary representation of the toml file.

Return type:

Dict

reemission.utils.load_yaml(file_path: Path, schema_file: Path | None = None) Dict[source]

Conditional yaml file loader depending on the installed yaml package version.

Parameters:
  • file_path – Path to the yaml file.

  • schema_file – Path to json ‘jsonschema’ file (optional).

Returns:

A dictionary representation of the yaml file.

Return type:

Dict

reemission.utils.md5(file_name: str | Path, chunk_size: int = 4) str[source]

Generate MD5 checksum of a file.

Parameters:
  • file_name – Path to the file for which MD5 sum needs to be calculated.

  • chunk_size – Size of the file chunk to be read (in KB).

Returns:

MD5 checksum of the file.

Return type:

str

reemission.utils.read_config(file_path: str | Path) ConfigParser[source]

Reads the .ini file with global configuration parameters and returns the parsed config object.

Parameters:

file_path – Path to the .ini file.

Returns:

ConfigParser object of the .ini file.

Return type:

configparser.ConfigParser

reemission.utils.read_table(file_path: Path, schema_file: Path | None = None) Dict[source]

Reads yaml table from the given YAML file.

Parameters:
  • file_path – Path to the YAML file.

  • schema_file – Path to json ‘jsonschema’ file (optional).

Returns:

Dictionary representation of the yaml file if the file exists and no errors occurred while parsing the file.

Return type:

Dict

Raises:

TableNotReadException – If the table cannot be read.

reemission.utils.save_return(output: Dict, save_output: bool = True) Callable[source]

Decorator that saves the output of the decorated method to an output dict (usually a global var).

Used for saving internal variables to a global shared variable internals in shared_intern.py

Parameters:
  • output – Dictionary to save the output.

  • save_output – Whether to save the output or not (default is True).

Returns:

Decorated function.

Return type:

Callable

reemission.utils.save_to_json(output_path: str | Path, input_dict: Dict) int[source]

Save dictionary to JSON. If folder tree does not exist, create one before saving the file. Returns an exit status.

Parameters:
  • output_path – Path to save the JSON file.

  • input_dict – Dictionary to be saved.

Returns:

Exit status code.

Return type:

int

reemission.utils.split_path(input_path: str | Path) Tuple[Path, Path | None][source]

Split path into the parent directory tree and the file name.

Parameters:

input_path – Path to be split.

Returns:

Tuple containing the parent directory and the file name.

Return type:

SplitPath

reemission.utils.strip_double_quotes(input: str) str[source]

Strip double quotes from a string.

Parameters:

input – Input string.

Returns:

String with sinfgle quotes.

Return type:

str

reemission.utils.timeit(func: Callable) Callable[source]

Wrapper for timing executions of functions.

Parameters:

func – Function to be wrapped.

Returns:

Wrapped function.

Return type:

Callable

reemission.utils.validate(data: Any, schema: Dict) None[source]

Validate data in a dictionary format against schema with jsonschema.

Parameters:
  • data – Data to be validated.

  • schema – Schema against which the data is to be validated.

Raises:

jsonschema.exceptions.ValidationError – If validation fails.