Reference¶
Base class to create root configuration. |
|
Specify valid choices for a variable. |
|
Mark a variable as valid, only if the condition is hold. |
|
Mark a variable as required. |
|
Raised when the user tries to assign a non-valid variable to |
|
Raised if a user doesn’t set |
- class pipcs.Config(dictionary={})[source]¶
Base class to create root configuration.
- Parameters
dictionary (
Union[dict, Config], optional) – If it is apipcs.Config, it will inherit the base configuration.
- check_config()[source]¶
Check configuration if all of the variables are valid.
from pipcs import Config, Required, required config = Config() @config('example') class Example(): variable: Required[int] = required config.check_config() # Raises: pipcs.pipcs.RequiredError: variable is required!
- get_value(key, check=False)[source]¶
Return value of the variable.
- Parameters
check (bool) – If true, the variable will be checked if it is valid or not.
from pipcs import Config, Required, required config = Config() @config('example') class Example(): variable: Required[int] = required print(config.example.get_value('variable')) # <class 'pipcs.pipcs.required'> print(config.example.get_value('variable', check=True)) # pipcs.pipcs.RequiredError: variable is required!
- to_dict(check=False)[source]¶
Convert
pipcs.Configtodict. If thepipcs.Conditionholds for a variable it will be included in the dictionary.pipcs.Choicesvariables will be converted in to their default values.- Parameters
check (bool) – If true, the variables will be checked if they are valid or not.
- class pipcs.Choices(choices: List[pipcs.pipcs.T], default=<class 'pipcs.pipcs.required'>)[source]¶
- Specify valid choices for a variable.
pipcs.InvalidChoiceErrorerror will be raised when the usertries to set the variable to a non-valid choice in the inherited configuration.
- Parameters
choices (List[T]) – Valid choices for the configuration variable.
default (Required[T]) – If the variable is not set by user the default value will be returned.
from pipcs import Config, Choices config = Config() @config('example') class Example(): variable: Choices[int] = Choices([1, 2, 3]) user_config = Config(config) @user_config('example') class UserExample(): variable = 1 print(user_config.example.variable) # 1 user_config = Config(config) @user_config('example') class UserExample(): variable = 4 # Raises: pipcs.pipcs.InvalidChoiceError: 4 is not valid for variable, valid choices: [1, 2, 3]
- class pipcs.Condition(data: pipcs.pipcs.T, comp: pipcs.pipcs.Comparison)[source]¶
Mark a variable as valid, only if the condition is hold. It is used combined with
pipcs.Choices.- Parameters
data (T) – Value of the variable.
comp – Comparison function.
from pipcs import Config, Choices, Condition config = Config() @config('example') class Example(): variable: Choices[int] = Choices([1, 2, 3]) conditional_variable: Condition[int] = Condition(5, variable==2) # Example 1 user_config = Config(config) @user_config('example') class UserExample(): variable = 2 print(user_config.example.to_dict()) # {'variable': 2, 'conditional_variable': 5} # Example 2 user_config = Config(config) @user_config('example') class UserExample(): variable = 2 conditional_variable = 1 print(user_config.example.to_dict()) # {'variable': 2, 'conditional_variable': 1} # Example 3 user_config = Config(config) @user_config('example') class UserExample(): variable = 1 conditional_variable = 2 print(user_config.example.to_dict()) # {'variable': 1}
- class pipcs.InvalidChoiceError[source]¶
Raised when the user tries to assign a non-valid variable to
pipcs.Choicesvariable.
- class pipcs.RequiredError[source]¶
Raised if a user doesn’t set
pipcs.requiredvariable in the inherited config. It is also raised if apipcs.requiredvariable is not set duringpipcs.Config.check_config().