Debian Codegraph @

ml_collections/config_dict/examples/frozen_config_dict.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 FrozenConfigDict usage.
16
17 This example shows the most basic usage of FrozenConfigDict, highlighting
18 the differences between FrozenConfigDict and ConfigDict and including
19 converting between the two.
20 """
21
22 from absl import app
23 from ml_collections import config_dict
24
25
26 def print_section(name):
27 print()
28 print()
29 print('-' * len(name))
30 print(name.upper())
31 print('-' * len(name))
32 print()
33
34
35 def main(_):
36 print_section('Attribute Types.')
37 cfg = config_dict.ConfigDict()
38 cfg.int = 1
39 cfg.list = [1, 2, 3]
40 cfg.tuple = (1, 2, 3)
41 cfg.set = {1, 2, 3}
42 cfg.frozenset = frozenset({1, 2, 3})
43 cfg.dict = {
44 'nested_int': 4,
45 'nested_list': [4, 5, 6],
46 'nested_tuple': ([4], 5, 6),
47 }
48
49 print('Types of cfg fields:')
50 print('list: ', type(cfg.list)) # List
51 print('set: ', type(cfg.set)) # Set
52 print('nested_list: ', type(cfg.dict.nested_list)) # List
53 print('nested_tuple[0]: ', type(cfg.dict.nested_tuple[0])) # List
54
55 frozen_cfg = config_dict.FrozenConfigDict(cfg)
56 print('\nTypes of FrozenConfigDict(cfg) fields:')
57 print('list: ', type(frozen_cfg.list)) # Tuple
58 print('set: ', type(frozen_cfg.set)) # Frozenset
59 print('nested_list: ', type(frozen_cfg.dict.nested_list)) # Tuple
60 print('nested_tuple[0]: ', type(frozen_cfg.dict.nested_tuple[0])) # Tuple
61
62 cfg_from_frozen = config_dict.ConfigDict(frozen_cfg)
63 print('\nTypes of ConfigDict(FrozenConfigDict(cfg)) fields:')
64 print('list: ', type(cfg_from_frozen.list)) # List
65 print('set: ', type(cfg_from_frozen.set)) # Set
66 print('nested_list: ', type(cfg_from_frozen.dict.nested_list)) # List
67 print('nested_tuple[0]: ', type(cfg_from_frozen.dict.nested_tuple[0])) # List
68
69 print('\nCan use FrozenConfigDict.as_configdict() to convert to ConfigDict:')
70 print(cfg_from_frozen == frozen_cfg.as_configdict()) # True
71
72 print_section('Immutability.')
73 try:
74 frozen_cfg.new_field = 1 # Raises AttributeError because of immutability.
75 except AttributeError as e:
76 print(e)
77
78 print_section('"==" and eq_as_configdict().')
79 # FrozenConfigDict.__eq__() is not type-invariant with respect to ConfigDict
80 print(frozen_cfg == cfg) # False
81 # FrozenConfigDict.eq_as_configdict() is type-invariant with respect to
82 # ConfigDict
83 print(frozen_cfg.eq_as_configdict(cfg)) # True
84 # .eq_as_congfigdict() is also a method of ConfigDict
85 print(cfg.eq_as_configdict(frozen_cfg)) # True
86
87
88 if __name__ == '__main__':
89 app.run(main)