Config¶
chanfig.config
¶
Config
¶
Bases: NestedDict
Config
is an extension of NestedDict
.
The differences between Config
and NestedDict
lies in 3 aspects:
Config
hasdefault_factory
set toConfig
andconvert_mapping
set toTrue
by default.Config
has afrozen
attribute, which can be toggled withfreeze
(lock
) &defrost
(unlock
) or temporarily changed withlocked
&unlocked
.Config
has aConfigParser
built-in, and supportsadd_argument
andparse
.
Config also features a post
method and a boot
method to support lazy-initilisation.
This is useful when you want to perform some post-processing on the config.
For example, some values may be a combination of other values, and you may define them in post
.
boot
is introduced to call all post
methods in the nested structure of Config
object.
By default, boot
will be called to after Config
is parsed.
You could also manually call boot
if you you don’t parse command-line arguments.
Notes
Since Config
has default_factory
set to Config
,
accessing anything that does not exist will create a new empty Config sub-attribute.
A frozen Config
does not have this behaviour and
will raises KeyError
when accessing anything that does not exist.
It is recommended to call config.freeze()
or config.to(NestedDict)
to avoid this behaviour.
Attributes:
Name | Type | Description |
---|---|---|
parser |
ConfigParser
|
Parser for command-line arguments. |
frozen |
bool
|
If |
Examples:
>>> c = Config(**{"f.n": "chang"})
>>> c.i.d = 1016
>>> c.i.d
1016
>>> c.d.i
Config(<class 'chanfig.config.Config'>, )
>>> c.freeze().dict()
{'f': {'n': 'chang'}, 'i': {'d': 1016}, 'd': {'i': {}}}
>>> c.d.i = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.d.e
Traceback (most recent call last):
AttributeError: 'Config' object has no attribute 'e'
>>> with c.unlocked():
... del c.d
>>> c.dict()
{'f': {'n': 'chang'}, 'i': {'d': 1016}}
Source code in chanfig/config.py
Python | |
---|---|
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 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
post
¶
Python | |
---|---|
Post process of Config
.
Some Config
may need to do some post process after Config
is initialised.
post
is provided for this lazy-initialisation purpose.
By default, post
calls interpolate
to perform variable interpolation.
Note that you should always call boot
to apply post
rather than calling post
directly,
as boot
recursively call post
on sub-configs.
See Also
Returns:
Name | Type | Description |
---|---|---|
self |
Self | None
|
|
Examples:
>>> c = Config()
>>> c.dne
Config(<class 'chanfig.config.Config'>, )
>>> c.post()
Config(
('dne'): Config()
)
>>> c.dne2
Traceback (most recent call last):
AttributeError: 'Config' object has no attribute 'dne2'
>>> class PostConfig(Config):
... def post(self):
... if isinstance(self.data, str):
... self.data = Config(feature=self.data, label=self.data)
... return self
>>> c = PostConfig(data="path")
>>> c.post()
PostConfig(<class 'chanfig.config.Config'>,
('data'): Config(<class 'chanfig.config.Config'>,
('feature'): 'path'
('label'): 'path'
)
)
Source code in chanfig/config.py
boot
¶
Python | |
---|---|
Apply post
recursively.
Sub-config may have their own post
method.
boot
is provided to apply post
recursively.
By default, boot
is called after Config
is parsed.
If you don’t need to parse command-line arguments, you should call boot
manually.
See Also
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
|
Examples:
>>> class DataConfig(Config):
... def post(self):
... if isinstance(self.path, str):
... self.path = Config(feature=self.path, label=self.path)
... return self
>>> class BootConfig(Config):
... def __init__(self, *args, **kwargs):
... super().__init__(*args, **kwargs)
... self.dataset = DataConfig(path="path")
... def post(self):
... if isinstance(self.id, str):
... self.id += "_id"
... return self
>>> c = BootConfig(id="boot")
>>> c.boot()
BootConfig(<class 'chanfig.config.Config'>,
('id'): 'boot_id'
('dataset'): DataConfig(<class 'chanfig.config.Config'>,
('path'): Config(<class 'chanfig.config.Config'>,
('feature'): 'path'
('label'): 'path'
)
)
)
Source code in chanfig/config.py
parse
¶
Python | |
---|---|
Parse command-line arguments with ConfigParser
.
parse
will try to parse all command-line arguments,
you don’t need to pre-define them but typos may cause trouble.
By default, this method internally calls Config.boot()
.
To disable this behaviour, set boot
to False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Iterable[str] | None
|
Command-line arguments. Defaults to |
None
|
default_config |
str | None
|
Path to default config file. Defaults to |
None
|
no_default_config_action |
str
|
Action when |
'raise'
|
boot |
bool
|
If |
True
|
See Also
chanfig.ConfigParser.parse
: Implementation of parse
.
parse_config
: Only parse valid config arguments.
Examples:
>>> c = Config(a=0)
>>> c.dict()
{'a': 0}
>>> c.parse(['--a', '1', '--b', '2', '--c', '3']).dict()
{'a': 1, 'b': 2, 'c': 3}
Source code in chanfig/config.py
parse_config
¶
Python | |
---|---|
Parse command-line arguments with ConfigParser
.
parse_config
only parse command-line arguments that is in defined in Config
.
By default, this method internally calls Config.boot()
.
To disable this behaviour, set boot
to False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Iterable[str] | None
|
Command-line arguments. Defaults to |
None
|
default_config |
str | None
|
Path to default config file. Defaults to |
None
|
no_default_config_action |
str
|
Action when |
'raise'
|
boot |
bool
|
If |
True
|
See Also
chanfig.ConfigParser.parse_config
: Implementation of parse_config
.
parse
: Parse all command-line arguments.
Examples:
>>> c = Config(a=0, b=0, c=0)
>>> c.dict()
{'a': 0, 'b': 0, 'c': 0}
>>> c.parse_config(['--a', '1', '--b', '2', '--c', '3']).dict()
{'a': 1, 'b': 2, 'c': 3}
Source code in chanfig/config.py
add_argument
¶
Add an argument to ConfigParser
.
Note that value defined in Config
will override the default value defined in add_argument
.
Examples:
>>> c = Config(a=0, c=1)
>>> arg = c.add_argument("--a", type=int, default=1)
>>> arg = c.add_argument("--b", type=int, default=2)
>>> c.parse(['--c', '4']).dict()
{'a': 1, 'c': 4, 'b': 2}
Source code in chanfig/config.py
freeze
¶
Freeze Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive |
bool
|
|
True
|
Alias:
lock
Examples:
>>> c = Config(**{'i.d': 1016})
>>> c.getattr('frozen')
False
>>> c.freeze(recursive=False).dict()
{'i': {'d': 1016}}
>>> c.getattr('frozen')
True
>>> c.i.getattr('frozen')
False
>>> c.lock().dict() # alias
{'i': {'d': 1016}}
>>> c.i.getattr('frozen')
True
Source code in chanfig/config.py
lock
¶
locked
¶
Python | |
---|---|
Context manager which temporarily locks Config
.
Examples:
>>> c = Config()
>>> with c.locked():
... c['i.d'] = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.i.d = 1016
>>> c.dict()
{'i': {'d': 1016}}
Source code in chanfig/config.py
defrost
¶
Defrost Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive |
bool
|
|
True
|
Alias:
unlock
Examples:
>>> c = Config(**{'i.d': 1016})
>>> c.getattr('frozen')
False
>>> c.freeze().dict()
{'i': {'d': 1016}}
>>> c.getattr('frozen')
True
>>> c.defrost(recursive=False).dict()
{'i': {'d': 1016}}
>>> c.getattr('frozen')
False
>>> c.i.getattr('frozen')
True
>>> c.unlock().dict() # alias
{'i': {'d': 1016}}
>>> c.i.getattr('frozen')
False
Source code in chanfig/config.py
unlock
¶
unlocked
¶
Python | |
---|---|
Context manager which temporarily unlocks Config
.
Examples:
>>> c = Config()
>>> c.freeze().dict()
{}
>>> with c.unlocked():
... c['i.d'] = 1016
>>> c.defrost().dict()
{'i': {'d': 1016}}
Source code in chanfig/config.py
get
¶
Get value from Config
.
Note that default
has higher priority than default_factory
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
|
required |
default |
Any
|
|
None
|
Returns:
Name | Type | Description |
---|---|---|
value |
Any
|
If |
Raises:
Type | Description |
---|---|
KeyError
|
If |
Examples:
>>> d = Config(**{"i.d": 1016})
>>> d.get('i.d')
1016
>>> d['i.d']
1016
>>> d.i.d
1016
>>> d.get('f', 2)
2
>>> d.f
Config(<class 'chanfig.config.Config'>, )
>>> del d.f
>>> d.freeze()
Config(<class 'chanfig.config.Config'>,
('i'): Config(<class 'chanfig.config.Config'>,
('d'): 1016
)
)
>>> d.f
Traceback (most recent call last):
AttributeError: 'Config' object has no attribute 'f'
>>> d["f.n"]
Traceback (most recent call last):
KeyError: 'f.n'
Source code in chanfig/config.py
set
¶
Set value of Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
|
required |
value |
Any
|
|
required |
convert_mapping |
bool | None
|
Whether to convert |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
Examples:
>>> c = Config()
>>> c['i.d'] = 1016
>>> c.i.d
1016
>>> c.freeze().dict()
{'i': {'d': 1016}}
>>> c['i.d'] = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.defrost().dict()
{'i': {'d': 1016}}
>>> c['i.d'] = 1016
>>> c.i.d
1016
Source code in chanfig/config.py
delete
¶
Delete value from Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
|
required |
Examples:
>>> d = Config(**{"i.d": 1016, "f.n": "chang"})
>>> d.i.d
1016
>>> d.f.n
'chang'
>>> d.delete('i.d')
>>> "i.d" in d
False
>>> d.i.d
Config(<class 'chanfig.config.Config'>, )
>>> "i.d" in d
True
>>> del d.f.n
>>> d.f.n
Config(<class 'chanfig.config.Config'>, )
>>> del d.c
Traceback (most recent call last):
AttributeError: 'Config' object has no attribute 'c'
Source code in chanfig/config.py
pop
¶
Pop value from Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
|
required |
default |
Any
|
|
Null
|
Returns:
Name | Type | Description |
---|---|---|
value |
Any
|
If |
Examples:
>>> c = Config()
>>> c['i.d'] = 1016
>>> c.pop('i.d')
1016
>>> c.pop('i.d', True)
True
>>> c.freeze().dict()
{'i': {}}
>>> c['i.d'] = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.defrost().dict()
{'i': {}}
>>> c['i.d'] = 1016
>>> c.pop('i.d')
1016
Source code in chanfig/config.py
frozen_check
¶
Decorator check if the object is frozen.