Defaults
toolbox_python.defaults
🔗
Summary
The defaults
module is used how to set and control default values for our various Python processes.
toolbox_python.defaults.Defaults
🔗
Summary
When we create and use Python variables, it is sometimes handy to add a default value for that variable. This class will handle that process.
Examples
Set up data for examples | |
---|---|
1 2 |
|
Example 1: Call direct from class | |
---|---|
1 |
|
"this"
Conclusion: Successfully printed default value direct from class.
Example 2: Call from instantiated class | |
---|---|
1 |
|
"that"
Conclusion: Successfully printed default value from instantiated class.
Example 3: Cast to `bool` | |
---|---|
1 |
|
True
Conclusion: Successfully casted to bool
.
Example 4: Cast to `int` | |
---|---|
1 |
|
1
Conclusion: Successfully casted to int
.
Example 5: Cast to `str` | |
---|---|
1 |
|
"1"
Conclusion: Successfully casted to str
.
Example 6: Cast to string `'str'` | |
---|---|
1 |
|
"1"
Conclusion: Successfully casted to str
.
Note: The only difference between this and the previous example is the type of the cast
parameter. Here, it is a string representation of the type, whereas in the previous example, we parse'ed in the actual str
class.
Example 7: Invalid cast type | |
---|---|
1 |
|
AttributeError: The value for `type` is invalid: `bad_type`.
Must be a valid type: ['bool', 'dict', 'int', 'float', 'list', 'str', 'tuple']
Conclusion: Invalid cast type.
Example 8: All blank values | |
---|---|
1 |
|
AttributeError: Both `value` and `default` are blank: 'None', 'None'.
If `value` is blank, then `default` cannot be blank.
Conclusion: Both value
and default
are blank.
Credit
Inspiration from:
https://github.com/henriquebastos/python-decouple/
Source code in src/toolbox_python/defaults.py
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
__init__
🔗
__init__() -> None
Summary
Nothing is initialised when this class is instantiated.
Use the __call__()
method instead.
See Also
Source code in src/toolbox_python/defaults.py
175 176 177 178 179 180 181 182 183 184 |
|
__call__
🔗
__call__(*args, **kwargs) -> Any
Summary
When this class is called, it will pass through all parameters to the internal .get()
method.
See Also
Source code in src/toolbox_python/defaults.py
186 187 188 189 190 191 192 193 194 |
|
get
🔗
get(
value: Any,
default: Any | None = None,
cast: str | type | None = None,
) -> Any
Summary
From the value that is parsed in to the value
parameter, convert it to default
if value
is None
, and convert it to cast
if cast
is not None
.
Details
The detailed steps will be:
- Validate the input (using the internal
._validate_value_and_default()
&._validate_type()
methods), - If
value
isNone
, then assigndefault
tovalue
. - If
cast
is notNone
, then castvalue
to the data type incast
.- Note,
cast
can be either the actual type to convert to, or a string representation of the type.
- Note,
- Return the updated/defaulted/casted
value
back to the user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The value to check. |
required |
default
|
Optional[Any]
|
The default value for |
None
|
cast
|
Optional[Union[str, type]]
|
The data type to convert to. |
None
|
Returns:
Name | Type | Description |
---|---|---|
value |
Any
|
The updated/defaulted/casted value. |
Source code in src/toolbox_python/defaults.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
_validate_value_and_default
🔗
_validate_value_and_default(
value: Any | None = None, default: Any | None = None
) -> Defaults
Summary
Validate to ensure that value
and default
are not both None
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Optional[Any]
|
The |
None
|
default
|
Optional[Any]
|
The |
None
|
Raises:
Type | Description |
---|---|
AttributeError
|
If both |
Returns:
Name | Type | Description |
---|---|---|
self |
Defaults
|
If both |
See Also
Source code in src/toolbox_python/defaults.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
_validate_type
🔗
_validate_type(
check_type: str | type | None = None,
) -> Defaults
Summary
Check to ensure that check_type
is a valid Python type.
Must be one of: ["bool", "dict", "int", "float", "list", "str", "tuple"]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
check_type
|
Optional[Union[str, type]]
|
The type to check against. Can either be an actual Python type, or it's string representation. |
None
|
Raises:
Type | Description |
---|---|
AttributeError
|
If |
Returns:
Name | Type | Description |
---|---|---|
self |
Defaults
|
If the type is valid, return |
See Also
Source code in src/toolbox_python/defaults.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|