control_framework_base module¶
- class ControlFrameworkBase(time_units, applies_to_subset, conn)¶
Bases:
objectBase class for running control checks.
Used for setting up controls with minimal boilerplate code required by the user. See Example for how to inherit from it.
Example
- class MyOwnControls(ControlFrameworkBase):
- def __init__(
self, time_units, applies_to_subset, conn,
- ) -> None:
super().__init__(time_units, applies_to_subset, conn)
- Parameters:
time_units (list[str])
applies_to_subset (dict[str, Any])
conn (object)
- execute_controls()¶
Executes all control methods found in the class.
- Return type:
None
- find_control_methods()¶
Method for finding all control methods defined in the class.
Be aware that it loops through all attributes in the class and adds any that has the ‘_control_meta’ attribute to the list of controls.
- Return type:
None
- generate_update_query(df_updates)¶
Generates a SQL UPDATE query for updating rows in ‘kontrollutslag’.
- Parameters:
df_updates (pd.DataFrame) – DataFrame with updates to apply.
- Returns:
SQL query string.
- Return type:
str
- get_current_kontroller()¶
Gets the current contents of the ‘kontroller’ table.
- Return type:
DataFrame|None
- get_current_kontrollutslag(specific_control=None)¶
Method to get current content of the kontrollutslag table.
- Parameters:
specific_control (
str|None) – Gets the current content of kontrollutslag table for this control. Defaults to None, which returns the data for all controls.- Return type:
DataFrame|None- Returns:
pd.DataFrame containing the current kontrollutslag table for all controls or just the specified one or None if table empty.
- Raises:
NotImplementedError – If connection is not EimerDBInstance or Ibis connection.
- insert_new_records(control_results)¶
Inserts new records that are not found in the current contents of the ‘kontrollutslag’ table.
- Return type:
None- Parameters:
control_results (DataFrame)
- register_all_controls()¶
Registers all controls found to the ‘kontroller’ table.
- Return type:
None
- register_control(control)¶
This method registers a given control in the ‘kontroller’ table.
- Parameters:
control (
str) – The name of the control method to register.- Return type:
None- Returns:
None
- Raises:
NotImplementedError – If connection is not EimerDBInstance or Ibis connection.
- run_all_controls()¶
Runs all controls found in the class.
- Return type:
DataFrame
- run_control(control)¶
Runs a single control.
- Parameters:
control (
str) – Name of a control method to run implemented in the supplied control class built upon ControlFrameworkBase.- Returns:
Dataframe containing results from the control.
- Return type:
pd.Dataframe
- Raises:
TypeError – If the control method does not return a pd.DataFrame.
ValueError – If a required column from _required_kontrollutslag_columns is missing.
ValueError – If the result contains extra columns that are not allowed.
ValueError – If the control is not registered in self.controls.
ValueError – If any key in applies_to_subset has too many unique values.
ValueError – If any value in applies_to_subset does not match the expected period.
ValueError – If the results contain more than one unique kontrollid.
ValueError – If the kontrollid in the results does not match the registered _control_meta.
ValueError – If there are duplicated rows in the results based on time_units, ident, and refnr.
- update_existing_records(control_results)¶
Updates the ‘kontrollutslag’ table based on results from new run of the method.
- Return type:
None- Parameters:
control_results (DataFrame)
- register_control(kontrollid, kontrolltype, beskrivelse, kontrollerte_variabler, sorteringsvariabel=None, sortering=None, **kwargs)¶
Decorator used to attach required metadata to control methods.
It is recommended to have the name of your control method / function start with the prefix ‘control_’.
- Parameters:
kontrollid (str) – The id of the control, preferably a code or shortened name. Must be unique. Note that in the control module controls are sorted alphabetically based on kontrollid, meaning you can add numbers as prefix to control the sorting order in the app.
kontrolltype (str) – The type of control. Must be ‘H’ (Hard control), ‘S’ (Soft control) or ‘I’ (Informative)
beskrivelse (str) – A description of the control.
kontrollerte_variabler (list[str]) – A list of variables that are covered / relevant to the control.
sorteringsvariabel (str | None) – Variable to sort the values on.
sortering (str | None) – Controls if the sorting is ascending (ASC) or descending (DESC). Defaults to DESC
kwargs (Any) – These will be added to the _control_meta dict attached to the method as additional key and value pairs.
- Raises:
TypeError – If kontrollerte_variabler is not a list of strings.
ValueError – If kontrolltype is not one of ‘H’, ‘S’, or ‘I’.
ValueError – If sortering is not one of ‘ASC’ or ‘DESC’.
ValueError – If required keys are missing from the metadata dictionary.
- Returns:
The decorated function with added attribute ‘_control_meta’ containing a dictionary with metadata.
- Return type:
Callable[…, Any]
Example
- @register_control(
kontrollid=”001_error”, kontrolltype=”H”, beskrivelse=”Finds erroronous data.”, kontrollerte_variabler=[“revenue”], sorteringsvariabel=”revenue”, sortering=”ASC”,
) def control_an_important_check(self):
…
Notes
Some fields are required for future use with statlog-model: - kontrollid - kontrolltype - beskrivelse - kontrollerte_variabler