| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Example of ConfigDict usage. |
| 16 |
|
| 17 |
This example includes loading a ConfigDict in FLAGS, locking it, type |
| 18 |
safety, iteration over fields, checking for a particular field, unpacking with |
| 19 |
`**`, and loading dictionary from string representation. |
| 20 |
""" |
| 21 |
|
| 22 |
from absl import app |
| 23 |
from ml_collections import config_flags |
| 24 |
import yaml |
| 25 |
|
| 26 |
_CONFIG = config_flags.DEFINE_config_file( |
| 27 |
'my_config', |
| 28 |
default='ml_collections/config_dict/examples/config.py') |
| 29 |
|
| 30 |
|
| 31 |
def hello_function(string, **unused_kwargs): |
| 32 |
return 'Hello {}'.format(string) |
| 33 |
|
| 34 |
|
| 35 |
def print_section(name): |
| 36 |
print() |
| 37 |
print() |
| 38 |
print('-' * len(name)) |
| 39 |
print(name.upper()) |
| 40 |
print('-' * len(name)) |
| 41 |
print() |
| 42 |
|
| 43 |
|
| 44 |
def main(_): |
| 45 |
|
| 46 |
|
| 47 |
config = _CONFIG.value |
| 48 |
|
| 49 |
print_section('Printing config.') |
| 50 |
print(config) |
| 51 |
|
| 52 |
|
| 53 |
print('Type of the config {}'.format(type(config))) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
print_section('Locking.') |
| 58 |
print('config.is_locked={}'.format(config.is_locked)) |
| 59 |
try: |
| 60 |
config.object.new_field = -3 |
| 61 |
except AttributeError as e: |
| 62 |
print(e) |
| 63 |
|
| 64 |
|
| 65 |
try: |
| 66 |
config.object.floet = -3. |
| 67 |
except AttributeError as e: |
| 68 |
print(e) |
| 69 |
|
| 70 |
|
| 71 |
print_section('Unlocking.') |
| 72 |
with config.unlocked(): |
| 73 |
config.object.new_field = -3 |
| 74 |
print('config.object.new_field={}'.format(config.object.new_field)) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
print_section('Type safety.') |
| 79 |
try: |
| 80 |
config.float = 'jerry' |
| 81 |
except TypeError as e: |
| 82 |
print(e) |
| 83 |
config.float = -1.2 |
| 84 |
print('config.float={}'.format(config.float)) |
| 85 |
|
| 86 |
|
| 87 |
config.float = None |
| 88 |
config.float = -1.2 |
| 89 |
|
| 90 |
|
| 91 |
with config.ignore_type(): |
| 92 |
config.float = 'tom' |
| 93 |
print('config.float={}'.format(config.float)) |
| 94 |
config.float = 2.3 |
| 95 |
print('config.float={}'.format(config.float)) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
print_section('Iteration over fields.') |
| 100 |
for field in config: |
| 101 |
print('config has field "{}"'.format(field)) |
| 102 |
|
| 103 |
|
| 104 |
print_section('Checking for a particular field.') |
| 105 |
for field in ('float', 'non_existing'): |
| 106 |
if field in config: |
| 107 |
print('"{}" is in config'.format(field)) |
| 108 |
else: |
| 109 |
print('"{}" is not in config'.format(field)) |
| 110 |
|
| 111 |
|
| 112 |
print_section('Unpacking with **') |
| 113 |
print(hello_function(**config)) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
print_section('Loading dictionary from string representation.') |
| 120 |
dictionary = yaml.load(repr(config), yaml.UnsafeLoader) |
| 121 |
print('dict["object_reference"]["dict"]["dict"]["float"]={}'.format( |
| 122 |
dictionary['object_reference']['dict']['dict']['float'])) |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == '__main__': |
| 126 |
app.run(main) |