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 behavior 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 behavior.
Attributes:
Name | Type | Description |
---|---|---|
parser |
ConfigParser
|
Parser for command-line arguments. |
frozen |
bool
|
If |
Examples:
>>> c = Config(**{"f.n": "chang"})
>>> c.i.d = 1013
>>> c.i.d
1013
>>> c.d.i
Config(<class 'chanfig.config.Config'>, )
>>> c.freeze().dict()
{'f': {'n': 'chang'}, 'i': {'d': 1013}, 'd': {'i': {}}}
>>> c.d.i = 1013
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': 1013}}
Source code in chanfig/config.py
Python | |
---|---|
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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
|
post()
¶
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
does nothing and returns self
.
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: chanfig.Config.boot
Returns:
Name | Type | Description |
---|---|---|
self |
Config
|
Examples:
>>> 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()
¶
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: chanfig.Config.post
Returns:
Name | Type | Description |
---|---|---|
self |
Config
|
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(args=None, default_config=None, no_default_config_action='raise')
¶
Parse command-line arguments with ConfigParser
.
This function internally calls Config.post
.
See Also: chanfig.ConfigParser.parse
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
add_argument(*args, **kwargs)
¶
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(recursive=True)
¶
Freeze Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive |
bool
|
True
|
Alias:
lock
Examples:
>>> c = Config(**{'i.d': 1013})
>>> c.getattr('frozen')
False
>>> c.freeze(recursive=False).dict()
{'i': {'d': 1013}}
>>> c.getattr('frozen')
True
>>> c.i.getattr('frozen')
False
>>> c.lock().dict() # alias
{'i': {'d': 1013}}
>>> c.i.getattr('frozen')
True
Source code in chanfig/config.py
lock(recursive=True)
¶
locked()
¶
Context manager which temporarily locks Config
.
Examples:
>>> c = Config()
>>> with c.locked():
... c['i.d'] = 1013
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.i.d = 1013
>>> c.dict()
{'i': {'d': 1013}}
Source code in chanfig/config.py
defrost(recursive=True)
¶
Defrost Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive |
bool
|
True
|
Alias:
unlock
Examples:
>>> c = Config(**{'i.d': 1013})
>>> c.getattr('frozen')
False
>>> c.freeze().dict()
{'i': {'d': 1013}}
>>> c.getattr('frozen')
True
>>> c.defrost(recursive=False).dict()
{'i': {'d': 1013}}
>>> c.getattr('frozen')
False
>>> c.i.getattr('frozen')
True
>>> c.unlock().dict() # alias
{'i': {'d': 1013}}
>>> c.i.getattr('frozen')
False
Source code in chanfig/config.py
unlock(recursive=True)
¶
unlocked()
¶
Context manager which temporarily unlocks Config
.
Examples:
>>> c = Config()
>>> c.freeze().dict()
{}
>>> with c.unlocked():
... c['i.d'] = 1013
>>> c.defrost().dict()
{'i': {'d': 1013}}
Source code in chanfig/config.py
get(name, default=Null)
¶
Get value from Config
.
Note that default
has higher priority than default_factory
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
required | |
default |
Any
|
Null
|
Returns:
Name | Type | Description |
---|---|---|
value |
Any
|
If |
Raises:
Type | Description |
---|---|
KeyError
|
If |
Examples:
>>> d = Config(**{"i.d": 1013})
>>> d.get('i.d')
1013
>>> d['i.d']
1013
>>> d.i.d
1013
>>> 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'): 1013
)
)
>>> 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(name, value, convert_mapping=None)
¶
Set value of Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
required | |
value |
Any
|
required |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Examples:
>>> c = Config()
>>> c['i.d'] = 1013
>>> c.i.d
1013
>>> c.freeze().dict()
{'i': {'d': 1013}}
>>> c['i.d'] = 1013
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.defrost().dict()
{'i': {'d': 1013}}
>>> c['i.d'] = 1013
>>> c.i.d
1013
Source code in chanfig/config.py
delete(name)
¶
Delete value from Config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Any
|
required |
Examples:
>>> d = Config(**{"i.d": 1013, "f.n": "chang"})
>>> d.i.d
1013
>>> 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(name, default=Null)
¶
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'] = 1013
>>> c.pop('i.d')
1013
>>> c.pop('i.d', True)
True
>>> c.freeze().dict()
{'i': {}}
>>> c['i.d'] = 1013
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'] = 1013
>>> c.pop('i.d')
1013