helpers
module¶
Helpful functions for PyFasma that don’t fit to another module.
- pyfasma.helpers.ask_dir_create(path: str, prompt_repeats=3)¶
Ask to create the directory at the specified path.
If the directory doesn’t exist, asks the user if the directory should be created. If the directory already exists, asks the user if the directory should be overwritten.
- Parameters:
path (str) – Path to directory.
prompt_repeats (int, optional) – Number of prompt repetitions if user enters invalid values.
- Returns:
True or False depending on user’s input.
- Return type:
bool
- Raises:
NotADirectoryError – Raised if path is not a directory.
- pyfasma.helpers.check_dflist_ncols(df_list: list, n=None) bool ¶
Check if all DataFrames in a list have the same or specified number of columns.
- Parameters:
df_list (list) – List containing DataFrames.
n (int or None) – The number of columns each DataFrame in the list must have. If None (default), the method checks if the DataFrames have the same number of columns.
- Returns:
True if all DataFrames in the list have the specified or same number of columns, else False.
- Return type:
bool
- pyfasma.helpers.delete_dir_contents(path: str)¶
Delete all contents of the specified directory.
- Parameters:
path (str) – Path to directory.
- Raises:
NotADirectoryError – Raised if path is not a directory.
- pyfasma.helpers.filename_no_ext(path: str) str ¶
Get the name of a file without the extension.
- Parameters:
path (str) – Path to file.
- Returns:
Filename without the extension.
- Return type:
str
- Raises:
FileNotFoundError – Raised if the entered path is not a file.
- pyfasma.helpers.flatten_list(nested_list: list) list ¶
Fully flatten a list that contains an arbitrary number of arbitrarily nested lists.
Using the copy.deepcopy function, the initial list remains unchanged.
This function is based on Wilfred’s flatten.py gist: https://gist.github.com/Wilfred/7889868
- Parameters:
nested_list (list) – List to be flattened. Can contain arbitrarily nested sublists.
- Returns:
Flattened list.
- Return type:
list
Example
>>> l = [1, 2, 3, [4, 5, 6, [7, 8, 9]]] >>> flatten_list(l) [1, 2, 3, 4, 5, 6, 7, 8, 9]
- pyfasma.helpers.group_vals_by_key(list_of_dicts: list) dict ¶
Groups values in a list of dictionaries by their keys.
- Parameters:
list_of_dicts (list) – List containing dictionaries with the same keys.
- Returns:
Dictionary with the keys of the dictionaries in the input list and their respective values in lists.
- Return type:
dict
- Raises:
ValueError – If list_of_dicts is not a list. If not all items in list_of_dicts are not dictionaries.
- pyfasma.helpers.is_all_numeric(lst: list) bool ¶
Check if all elements in a list are numeric (int or float).
- Parameters:
lst (list) – List to be checked.
- Returns:
True if all elements are numeric, False otherwise.
- Return type:
bool
- pyfasma.helpers.is_list_sorted(lst: list, sort_order='ascending') bool ¶
Check if a list is sorted.
The function returns True if the list is sorted in ascending, descending, or either ascending or descending order, depending on the sort_order value, which can be ‘ascending’, ‘descending’, or ‘either’, respectively.
- Parameters:
list (list) – List to be checked.
sort_order (str, optional) – One of ‘ascending’, ‘descending’ or ‘either’.
- Returns:
True if the list is sorted in ascending, descending, or either ascending or descending order, depending on the sort_order value, False otherwise.
- Return type:
bool
- Raises:
ValueError – Raised if an invalid argument is used for sort_order.
- pyfasma.helpers.list_files_with_ext(path: str, ext: str, case_sensitive=False, exclude_files=None, exclude_dirs=None, max_depth=None, sort=True, show_total=True) list ¶
Recursively list all files with the specified extension in a directory.
- Parameters:
path (str) – Path to directory to be recursed.
ext (str) – Extension of files to be listed. The dot must be included. For example, ‘.csv’ should be used for CSV files to be listed.
case_sensitive (bool, optional) – If True, list the files specified by the extension parameter taking into account the letter case.
exclude_files (list, optional) – Exclude the specified files so they aren’t included in the resulting list. Each filename must include the extension and must be specified as a string. The parameter can also include lists of files to be excluded.
exclude_dirs (list, optional) – Exclude the specified direcories so they aren’t included in the resulting list. Each directory name must be specified as a string. The parameter can also include lists of files to be excluded.
max_depth (int, optional) – Specify the max depth of the directory structure up to which files are listed. If None (default), the files are listed recursicely until the end of the directory structure.
sort (bool, optional) – If True (default), the returned list is sorted.
show_total (bool, optional) – If True (default), print the total number of files found that satisfy the set criteria.
- Returns:
List of files with specified extension.
- Return type:
list
- Raises:
NotADirectoryError – Raised if path is not a directory.
ValueError – Raised if exclude_files is not a list.
ValueError – Raised if exclude_dirs is not a list.
- pyfasma.helpers.rename_dflist_columns(df_list: list, columns: list) list ¶
Rename the columns of the DataFrames inside a list.
- Parameters:
df_list (list) – List containing pandas DataFrames.
columns (list) – List containing the new column names for the DataFrames. The length of the list must be equal to the total number of columns in the list containing the DataFrames.
- Returns:
The original list of DataFrames with renamed columns.
- Return type:
list
- Raises:
ValueError – Raised if the length of columns is not equal to the total number of columns in the list containing the DataFrames.
- pyfasma.helpers.yes_no(message: str, default_answer='no', nrepeats=3) bool ¶
Return True or False depending on user’s input.
- Parameters:
message (str) – Message to be displayed to the user.
default_answer (str, optional) – Sets the prompt depending on the default answer. The default value is presented capitalized. Accepts values ‘yes’, ‘no’, or None.
nrepeats (int, optional) – Number of prompt repetitions if user enters invalid values.
- Returns:
True or False depending on user’s input.
- Return type:
bool
- Raises:
ValueError – Raised if user enters an invalid default_answer.