| 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 a config file using ConfigDict. |
| 16 | |
| 17 | The idea of this configuration file is to show a typical use case of ConfigDict, |
| 18 | as well as its limitations. This also exemplifies a self-referencing ConfigDict. |
| 19 | """ |
| 20 | |
| 21 | import copy |
| 22 | from ml_collections import config_dict |
| 23 | |
| 24 | |
| 25 | def _get_flat_config(): |
| 26 | """Helper to generate simple config without references.""" |
| 27 | |
| 28 | # The suggested way to create a ConfigDict() is to call its constructor |
| 29 | # and assign all relevant fields. |
| 30 | config = config_dict.ConfigDict() |
| 31 | |
| 32 | # In order to add new attributes you can just use . notation, like with any |
| 33 | # python object. They will be tracked by ConfigDict, and you get type checking |
| 34 | # etc. for free. |
| 35 | config.integer = 23 |
| 36 | config.float = 2.34 |
| 37 | config.string = 'james' |
| 38 | config.bool = True |
| 39 | |
| 40 | # It is possible to assign dictionaries to ConfigDict and they will be |
| 41 | # automatically and recursively wrapped with ConfigDict. However, make sure |
| 42 | # that the dict you are assigning does not use internal references/cycles as |
| 43 | # this is not supported. Instead, create the dicts explicitly as demonstrated |
| 44 | # by get_config(). But note that this operation makes an element-by-element |
| 45 | # copy of your original dict. |
| 46 | |
| 47 | # Also note that the recursive wrapping on input dictionaries with ConfigDict |
| 48 | # does not extend through non-dictionary types (including basic Python types |
| 49 | # and custom classes). This causes unexpected behavior most commonly if a |
| 50 | # value is a list of dictionaries, so avoid giving ConfigDict such inputs. |
| 51 | config.dict = { |
| 52 | 'integer': 1, |
| 53 | 'float': 3.14, |
| 54 | 'string': 'mark', |
| 55 | 'bool': False, |
| 56 | 'dict': { |
| 57 | 'float': 5 |
| 58 | } |
| 59 | } |
| 60 | return config |
| 61 | |
| 62 | |
| 63 | def get_config(): |
| 64 | """Returns a ConfigDict instance describing a complex config. |
| 65 | |
| 66 | Returns: |
| 67 | A ConfigDict instance with the structure: |
| 68 | |
| 69 | ``` |
| 70 | CONFIG-+-- integer |
| 71 | |-- float |
| 72 | |-- string |
| 73 | |-- bool |
| 74 | |-- dict +-- integer |
| 75 | | |-- float |
| 76 | | |-- string |
| 77 | | |-- bool |
| 78 | | |-- dict +-- float |
| 79 | | |
| 80 | |-- object +-- integer |
| 81 | | |-- float |
| 82 | | |-- string |
| 83 | | |-- bool |
| 84 | | |-- dict +-- integer |
| 85 | | |-- float |
| 86 | | |-- string |
| 87 | | |-- bool |
| 88 | | |-- dict +-- float |
| 89 | | |
| 90 | |-- object_copy +-- integer |
| 91 | | |-- float |
| 92 | | |-- string |
| 93 | | |-- bool |
| 94 | | |-- dict +-- integer |
| 95 | | |-- float |
| 96 | | |-- string |
| 97 | | |-- bool |
| 98 | | |-- dict +-- float |
| 99 | | |
| 100 | |-- object_reference [reference pointing to CONFIG-+--object] |
| 101 | ``` |
| 102 | """ |
| 103 | config = _get_flat_config() |
| 104 | config.object = _get_flat_config() |
| 105 | |
| 106 | # References work just fine, so you will be able to override both |
| 107 | # values at the same time. The rule is the same as for python objects, |
| 108 | # everything that is mutable is passed as a reference, thus it will not work |
| 109 | # with assigning integers or strings, but will work just fine with |
| 110 | # ConfigDicts. |
| 111 | # WARNING: Each time you assign a dictionary as a value it will create a new |
| 112 | # instance of ConfigDict in memory, thus it will be a copy of the original |
| 113 | # dict and not a reference to the original. |
| 114 | config.object_reference = config.object |
| 115 | |
| 116 | # ConfigDict supports deepcopying. |
| 117 | config.object_copy = copy.deepcopy(config.object) |
| 118 | |
| 119 | return config |