Debian Codegraph @

ml_collections/config_dict/examples/config_dict_basic.py

raw symbols SCIP
1 # Copyright 2024 The ML Collections Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Example of basic ConfigDict usage.
16
17 This example shows the most basic usage of ConfigDict, including type safety.
18 For examples of more features, see example_advanced.
19 """
20
21 from absl import app
22 from ml_collections import config_dict
23
24
25 def main(_):
26 cfg = config_dict.ConfigDict()
27 cfg.float_field = 12.6
28 cfg.integer_field = 123
29 cfg.another_integer_field = 234
30 cfg.nested = config_dict.ConfigDict()
31 cfg.nested.string_field = 'tom'
32
33 print(cfg.integer_field) # Prints 123.
34 print(cfg['integer_field']) # Prints 123 as well.
35
36 try:
37 cfg.integer_field = 'tom' # Raises TypeError as this field is an integer.
38 except TypeError as e:
39 print(e)
40
41 cfg.float_field = 12 # Works: `int` types can be assigned to `float`.
42 cfg.nested.string_field = u'bob' # `String` fields can store Unicode strings.
43
44 print(cfg)
45
46
47 if __name__ == '__main__':
48 app.run(main)