Extract default configuration values from configuration specifications.
This class processes a configuration list and extracts the default values
specified for each configuration item. It provides a simple way to get all
default values as a dictionary.
The class follows a straightforward process:
1. Stores the configuration list
2. Extracts the default value for each configuration item
3. Validates the default values against the configuration
Default values are the fallback values used when no other source (environment
variables, configuration files, or terminal arguments) provides a value.
| Attributes: |
-
config
(list)
–
List of configuration dictionaries containing name, type, and default keys.
-
values
(dict)
–
Dictionary of default values mapped by configuration name.
|
| Raises: |
-
ValueError
–
If configuration validation fails.
|
Example
config = [
... {"name": "host", "type": "string", "default": "localhost"},
... {"name": "port", "type": "int", "default": 8080},
... {"name": "debug", "type": "bool", "default": False}
... ]
parser = ParseFromDefault(config=config)
parser.get_values()
{'host': 'localhost', 'port': 8080, 'debug': False}
Source code in fejao_config_parser/src/parse_from/default.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 | class ParseFromDefault:
"""Extract default configuration values from configuration specifications.
This class processes a configuration list and extracts the default values
specified for each configuration item. It provides a simple way to get all
default values as a dictionary.
The class follows a straightforward process:
1. Stores the configuration list
2. Extracts the default value for each configuration item
3. Validates the default values against the configuration
Default values are the fallback values used when no other source (environment
variables, configuration files, or terminal arguments) provides a value.
Attributes:
config (list): List of configuration dictionaries containing name, type, and default keys.
values (dict): Dictionary of default values mapped by configuration name.
Raises:
ValueError: If configuration validation fails.
Example:
>>> config = [
... {"name": "host", "type": "string", "default": "localhost"},
... {"name": "port", "type": "int", "default": 8080},
... {"name": "debug", "type": "bool", "default": False}
... ]
>>> parser = ParseFromDefault(config=config)
>>> parser.get_values()
{'host': 'localhost', 'port': 8080, 'debug': False}
"""
def __init__(self, config: list) -> None:
"""Initialize the ParseFromDefault instance and extract default values.
This constructor initializes the parser with a configuration list and
immediately processes it to extract all default values. It performs
validation on the extracted values to ensure they conform to the
specified types.
Args:
config (list): List of configuration dictionaries. Each dictionary must contain:
- name (str): The configuration parameter name.
- type (str): The target type (string, bool, int, float, list, dict).
- default (any): The default value for this configuration.
Raises:
ValueError: If configuration validation fails.
Example:
>>> config = [
... {"name": "timeout", "type": "int", "default": 30},
... {"name": "retries", "type": "int", "default": 3}
... ]
>>> parser = ParseFromDefault(config=config)
"""
self.config = config
self.__set_values()
self.__check_values()
def __set_values(self) -> None:
"""Extract default values from the configuration.
This private method iterates through the configuration items and extracts
the default value for each one. The extracted values are stored in a
dictionary with configuration names as keys.
Process:
1. Initialize empty dictionary for results
2. Iterate through each configuration item
3. Extract the "default" value from the item
4. Store in results dictionary under the configuration name
5. Store results in self.values
Returns:
None
"""
ret_dict = {}
for item in self.config:
ret_dict[item["name"]] = item["default"]
self.values = ret_dict
def __check_values(self) -> None:
"""Validate the default values against the configuration.
This private method validates that all default values:
- Match their specified types
- Conform to the accepted type system
- Are valid for their configured types
The validation is performed by the ValuesCheck class which ensures type
safety and configuration compliance.
Raises:
ValueError: If any default value fails validation.
Returns:
None
"""
ValuesCheck(config=self.config, values_found=self.values, info_string="default values")
def get_values(self) -> dict:
"""Return the default configuration values as a dictionary.
This public method provides access to the extracted default values.
Returns all default values from the configuration as a dictionary.
Returns:
dict: Dictionary mapping configuration names to their default values.
Format: {config_name: default_value}.
Example:
>>> parser = ParseFromDefault(config=[
... {"name": "app_name", "type": "string", "default": "MyApp"},
... {"name": "workers", "type": "int", "default": 4}
... ])
>>> parser.get_values()
{'app_name': 'MyApp', 'workers': 4}
Returns:
dict: Dictionary of default values.
"""
return self.values
|
Methods:
__check_values
Validate the default values against the configuration.
This private method validates that all default values:
- Match their specified types
- Conform to the accepted type system
- Are valid for their configured types
The validation is performed by the ValuesCheck class which ensures type
safety and configuration compliance.
| Raises: |
-
ValueError
–
If any default value fails validation.
|
Source code in fejao_config_parser/src/parse_from/default.py
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 | def __check_values(self) -> None:
"""Validate the default values against the configuration.
This private method validates that all default values:
- Match their specified types
- Conform to the accepted type system
- Are valid for their configured types
The validation is performed by the ValuesCheck class which ensures type
safety and configuration compliance.
Raises:
ValueError: If any default value fails validation.
Returns:
None
"""
ValuesCheck(config=self.config, values_found=self.values, info_string="default values")
|
__init__
__init__(config: list) -> None
Initialize the ParseFromDefault instance and extract default values.
This constructor initializes the parser with a configuration list and
immediately processes it to extract all default values. It performs
validation on the extracted values to ensure they conform to the
specified types.
| Parameters: |
-
config
(list)
–
List of configuration dictionaries. Each dictionary must contain:
- name (str): The configuration parameter name.
- type (str): The target type (string, bool, int, float, list, dict).
- default (any): The default value for this configuration.
|
| Raises: |
-
ValueError
–
If configuration validation fails.
|
Example
config = [
... {"name": "timeout", "type": "int", "default": 30},
... {"name": "retries", "type": "int", "default": 3}
... ]
parser = ParseFromDefault(config=config)
Source code in fejao_config_parser/src/parse_from/default.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 | def __init__(self, config: list) -> None:
"""Initialize the ParseFromDefault instance and extract default values.
This constructor initializes the parser with a configuration list and
immediately processes it to extract all default values. It performs
validation on the extracted values to ensure they conform to the
specified types.
Args:
config (list): List of configuration dictionaries. Each dictionary must contain:
- name (str): The configuration parameter name.
- type (str): The target type (string, bool, int, float, list, dict).
- default (any): The default value for this configuration.
Raises:
ValueError: If configuration validation fails.
Example:
>>> config = [
... {"name": "timeout", "type": "int", "default": 30},
... {"name": "retries", "type": "int", "default": 3}
... ]
>>> parser = ParseFromDefault(config=config)
"""
self.config = config
self.__set_values()
self.__check_values()
|
__set_values
Extract default values from the configuration.
This private method iterates through the configuration items and extracts
the default value for each one. The extracted values are stored in a
dictionary with configuration names as keys.
Process
- Initialize empty dictionary for results
- Iterate through each configuration item
- Extract the "default" value from the item
- Store in results dictionary under the configuration name
- Store results in self.values
Source code in fejao_config_parser/src/parse_from/default.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 | def __set_values(self) -> None:
"""Extract default values from the configuration.
This private method iterates through the configuration items and extracts
the default value for each one. The extracted values are stored in a
dictionary with configuration names as keys.
Process:
1. Initialize empty dictionary for results
2. Iterate through each configuration item
3. Extract the "default" value from the item
4. Store in results dictionary under the configuration name
5. Store results in self.values
Returns:
None
"""
ret_dict = {}
for item in self.config:
ret_dict[item["name"]] = item["default"]
self.values = ret_dict
|
get_values
Return the default configuration values as a dictionary.
This public method provides access to the extracted default values.
Returns all default values from the configuration as a dictionary.
| Returns: |
-
dict( dict
) –
Dictionary mapping configuration names to their default values.
Format: {config_name: default_value}.
|
Example
parser = ParseFromDefault(config=[
... {"name": "app_name", "type": "string", "default": "MyApp"},
... {"name": "workers", "type": "int", "default": 4}
... ])
parser.get_values()
{'app_name': 'MyApp', 'workers': 4}
| Returns: |
-
dict( dict
) –
Dictionary of default values.
|
Source code in fejao_config_parser/src/parse_from/default.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 | def get_values(self) -> dict:
"""Return the default configuration values as a dictionary.
This public method provides access to the extracted default values.
Returns all default values from the configuration as a dictionary.
Returns:
dict: Dictionary mapping configuration names to their default values.
Format: {config_name: default_value}.
Example:
>>> parser = ParseFromDefault(config=[
... {"name": "app_name", "type": "string", "default": "MyApp"},
... {"name": "workers", "type": "int", "default": 4}
... ])
>>> parser.get_values()
{'app_name': 'MyApp', 'workers': 4}
Returns:
dict: Dictionary of default values.
"""
return self.values
|