coqpit-config 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,129 @@
1
+ uv.lock
2
+
3
+ WadaSNR/
4
+ .idea/
5
+ *.pyc
6
+ .DS_Store
7
+ ./__init__.py
8
+ # Byte-compiled / optimized / DLL files
9
+ __pycache__/
10
+ *.py[cod]
11
+ *$py.class
12
+
13
+ # C extensions
14
+ *.so
15
+
16
+ # Distribution / packaging
17
+ .Python
18
+ build/
19
+ develop-eggs/
20
+ dist/
21
+ downloads/
22
+ eggs/
23
+ .eggs/
24
+ lib/
25
+ lib64/
26
+ parts/
27
+ sdist/
28
+ var/
29
+ wheels/
30
+ *.egg-info/
31
+ .installed.cfg
32
+ *.egg
33
+ MANIFEST
34
+
35
+ # PyInstaller
36
+ # Usually these files are written by a python script from a template
37
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
38
+ *.manifest
39
+ *.spec
40
+
41
+ # Installer logs
42
+ pip-log.txt
43
+ pip-delete-this-directory.txt
44
+
45
+ # Unit test / coverage reports
46
+ htmlcov/
47
+ .tox/
48
+ .coverage
49
+ .coverage.*
50
+ .cache
51
+ nosetests.xml
52
+ coverage.xml
53
+ *.cover
54
+ .hypothesis/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ .static_storage/
63
+ .media/
64
+ local_settings.py
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # pyenv
83
+ .python-version
84
+
85
+ # celery beat schedule file
86
+ celerybeat-schedule
87
+
88
+ # SageMath parsed files
89
+ *.sage.py
90
+
91
+ # Environments
92
+ .env
93
+ .venv
94
+ env/
95
+ venv/
96
+ ENV/
97
+ env.bak/
98
+ venv.bak/
99
+
100
+ # Spyder project settings
101
+ .spyderproject
102
+ .spyproject
103
+
104
+ # Rope project settings
105
+ .ropeproject
106
+
107
+ # mkdocs documentation
108
+ /site
109
+
110
+ # mypy
111
+ .mypy_cache/
112
+
113
+ # vim
114
+ *.swp
115
+ *.swm
116
+ *.swn
117
+ *.swo
118
+
119
+ # pytorch models
120
+ *.pth.tar
121
+ result/
122
+
123
+ # setup.py
124
+ version.py
125
+
126
+ # jupyter dummy files
127
+ core
128
+
129
+ tests/example_config.json
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Coqui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,327 @@
1
+ Metadata-Version: 2.3
2
+ Name: coqpit-config
3
+ Version: 0.1.0
4
+ Summary: Simple (maybe too simple), light-weight config management through python data-classes.
5
+ Project-URL: Repository, https://github.com/idiap/coqui-ai-coqpit
6
+ Project-URL: Issues, https://github.com/idiap/coqui-ai-coqpit/issues
7
+ Author-email: Eren GΓΆlge <egolge@coqui.ai>
8
+ Maintainer-email: Enno Hermann <enno.hermann@gmail.com>
9
+ License: MIT
10
+ License-File: LICENSE.txt
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: MacOS
14
+ Classifier: Operating System :: Microsoft :: Windows
15
+ Classifier: Operating System :: POSIX :: Linux
16
+ Classifier: Programming Language :: Python
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Requires-Python: >=3.9
24
+ Requires-Dist: typing-extensions>=4.10
25
+ Description-Content-Type: text/markdown
26
+
27
+ # πŸ‘©β€βœˆοΈ Coqpit
28
+
29
+ [![CI](https://github.com/idiap/coqui-ai-coqpit/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/idiap/coqui-ai-coqpit/actions/workflows/main.yml)
30
+
31
+ Simple, light-weight and no dependency config handling through python data
32
+ classes with to/from JSON serialization/deserialization.
33
+
34
+ Fork of the [original, unmaintained repository](https://github.com/coqui-ai/coqpit). New PyPI package: [coqpit-config](https://pypi.org/project/coqpit-config)
35
+
36
+ Currently it is being used by [coqui-tts](https://github.com/idiap/coqui-ai-TTS).
37
+
38
+ ## ❔ Why I need this
39
+ What I need from a ML configuration library...
40
+
41
+ 1. Fixing a general config schema in Python to guide users about expected values.
42
+
43
+ Python is good but not universal. Sometimes you train a ML model and use it on a different platform. So, you
44
+ need your model configuration file importable by other programming languages.
45
+
46
+ 2. Simple dynamic value and type checking with default values.
47
+
48
+ If you are a beginner in a ML project, it is hard to guess the right values for your ML experiment. Therefore it is important
49
+ to have some default values and know what range and type of input are expected for each field.
50
+
51
+ 4. Ability to decompose large configs.
52
+
53
+ As you define more fields for the training dataset, data preprocessing, model parameters, etc., your config file tends
54
+ to get quite large but in most cases, they can be decomposed, enabling flexibility and readability.
55
+
56
+ 5. Inheritance and nested configurations.
57
+
58
+ Simply helps to keep configurations consistent and easier to maintain.
59
+
60
+ 6. Ability to override values from the command line when necessary.
61
+
62
+ For instance, you might need to define a path for your dataset, and this changes for almost every run. Then the user
63
+ should be able to override this value easily over the command line.
64
+
65
+ It also allows easy hyper-parameter search without changing your original code. Basically, you can run different models
66
+ with different parameters just using command line arguments.
67
+
68
+ 7. Defining dynamic or conditional config values.
69
+
70
+ Sometimes you need to define certain values depending on the other values. Using python helps to define the underlying
71
+ logic for such config values.
72
+
73
+ 8. No dependencies
74
+
75
+ You don't want to install a ton of libraries for just configuration management. If you install one, then it
76
+ is better to be just native python.
77
+
78
+ ## 🚫 Limitations
79
+ - `Union` type dataclass fields cannot be parsed from console arguments due to the type ambiguity.
80
+ - `JSON` is the only supported serialization format, although the others can be easily integrated.
81
+ - `List`type with multiple item type annotations are not supported. (e.g. `List[int, str]`).
82
+ - `dict` fields are parsed from console arguments as JSON str without type checking. (e.g `--val_dict '{"a":10, "b":100}'`).
83
+ - `MISSING` fields cannot be avoided when parsing console arguments.
84
+
85
+ ## πŸ” Examples
86
+
87
+ ### πŸ‘‰ Simple Coqpit
88
+ ```python
89
+ import os
90
+ from dataclasses import asdict, dataclass, field
91
+ from typing import List, Union
92
+ from coqpit import MISSING, Coqpit, check_argument
93
+
94
+
95
+ @dataclass
96
+ class SimpleConfig(Coqpit):
97
+ val_a: int = 10
98
+ val_b: int = None
99
+ val_d: float = 10.21
100
+ val_c: str = "Coqpit is great!"
101
+ # mandatory field
102
+ # raise an error when accessing the value if it is not changed. It is a way to define
103
+ val_k: int = MISSING
104
+ # optional field
105
+ val_dict: dict = field(default_factory=lambda: {"val_aa": 10, "val_ss": "This is in a dict."})
106
+ # list of list
107
+ val_listoflist: List[List] = field(default_factory=lambda: [[1, 2], [3, 4]])
108
+ val_listofunion: List[List[Union[str,int]]] = field(default_factory=lambda: [[1, 3], [1, "Hi!"]])
109
+
110
+ def check_values(
111
+ self,
112
+ ): # you can define explicit constraints on the fields using `check_argument()`
113
+ """Check config fields"""
114
+ c = asdict(self)
115
+ check_argument("val_a", c, restricted=True, min_val=10, max_val=2056)
116
+ check_argument("val_b", c, restricted=True, min_val=128, max_val=4058, allow_none=True)
117
+ check_argument("val_c", c, restricted=True)
118
+
119
+
120
+ if __name__ == "__main__":
121
+ file_path = os.path.dirname(os.path.abspath(__file__))
122
+ config = SimpleConfig()
123
+
124
+ # try MISSING class argument
125
+ try:
126
+ k = config.val_k
127
+ except AttributeError:
128
+ print(" val_k needs a different value before accessing it.")
129
+ config.val_k = 1000
130
+
131
+ # try serialization and deserialization
132
+ print(config.serialize())
133
+ print(config.to_json())
134
+ config.save_json(os.path.join(file_path, "example_config.json"))
135
+ config.load_json(os.path.join(file_path, "example_config.json"))
136
+ print(config.pprint())
137
+
138
+ # try `dict` interface
139
+ print(*config)
140
+ print(dict(**config))
141
+
142
+ # value assignment by mapping
143
+ config["val_a"] = -999
144
+ print(config["val_a"])
145
+ assert config.val_a == -999
146
+ ```
147
+ ### πŸ‘‰ Serialization
148
+ ```python
149
+ import os
150
+ from dataclasses import asdict, dataclass, field
151
+ from coqpit import Coqpit, check_argument
152
+ from typing import List, Union
153
+
154
+
155
+ @dataclass
156
+ class SimpleConfig(Coqpit):
157
+ val_a: int = 10
158
+ val_b: int = None
159
+ val_c: str = "Coqpit is great!"
160
+
161
+ def check_values(self,):
162
+ '''Check config fields'''
163
+ c = asdict(self)
164
+ check_argument('val_a', c, restricted=True, min_val=10, max_val=2056)
165
+ check_argument('val_b', c, restricted=True, min_val=128, max_val=4058, allow_none=True)
166
+ check_argument('val_c', c, restricted=True)
167
+
168
+
169
+ @dataclass
170
+ class NestedConfig(Coqpit):
171
+ val_d: int = 10
172
+ val_e: int = None
173
+ val_f: str = "Coqpit is great!"
174
+ sc_list: List[SimpleConfig] = None
175
+ sc: SimpleConfig = SimpleConfig()
176
+ union_var: Union[List[SimpleConfig], SimpleConfig] = field(default_factory=lambda: [SimpleConfig(),SimpleConfig()])
177
+
178
+ def check_values(self,):
179
+ '''Check config fields'''
180
+ c = asdict(self)
181
+ check_argument('val_d', c, restricted=True, min_val=10, max_val=2056)
182
+ check_argument('val_e', c, restricted=True, min_val=128, max_val=4058, allow_none=True)
183
+ check_argument('val_f', c, restricted=True)
184
+ check_argument('sc_list', c, restricted=True, allow_none=True)
185
+ check_argument('sc', c, restricted=True, allow_none=True)
186
+
187
+
188
+ if __name__ == '__main__':
189
+ file_path = os.path.dirname(os.path.abspath(__file__))
190
+ # init 🐸 dataclass
191
+ config = NestedConfig()
192
+
193
+ # save to a json file
194
+ config.save_json(os.path.join(file_path, 'example_config.json'))
195
+ # load a json file
196
+ config2 = NestedConfig(val_d=None, val_e=500, val_f=None, sc_list=None, sc=None, union_var=None)
197
+ # update the config with the json file.
198
+ config2.load_json(os.path.join(file_path, 'example_config.json'))
199
+ # now they should be having the same values.
200
+ assert config == config2
201
+
202
+ # pretty print the dataclass
203
+ print(config.pprint())
204
+
205
+ # export values to a dict
206
+ config_dict = config.to_dict()
207
+ # crate a new config with different values than the defaults
208
+ config2 = NestedConfig(val_d=None, val_e=500, val_f=None, sc_list=None, sc=None, union_var=None)
209
+ # update the config with the exported valuess from the previous config.
210
+ config2.from_dict(config_dict)
211
+ # now they should be having the same values.
212
+ assert config == config2
213
+ ```
214
+
215
+
216
+ ### πŸ‘‰ ```argparse``` handling and parsing.
217
+ ```python
218
+ import argparse
219
+ import os
220
+ from dataclasses import asdict, dataclass, field
221
+ from typing import List
222
+
223
+ from coqpit import Coqpit, check_argument
224
+ import sys
225
+
226
+
227
+ @dataclass
228
+ class SimplerConfig(Coqpit):
229
+ val_a: int = field(default=None, metadata={'help': 'this is val_a'})
230
+
231
+
232
+ @dataclass
233
+ class SimpleConfig(Coqpit):
234
+ val_req: str # required field
235
+ val_a: int = field(default=10,
236
+ metadata={'help': 'this is val_a of SimpleConfig'})
237
+ val_b: int = field(default=None, metadata={'help': 'this is val_b'})
238
+ nested_config: SimplerConfig = SimplerConfig()
239
+ mylist_with_default: List[SimplerConfig] = field(
240
+ default_factory=lambda:
241
+ [SimplerConfig(val_a=100),
242
+ SimplerConfig(val_a=999)],
243
+ metadata={'help': 'list of SimplerConfig'})
244
+
245
+ # mylist_without_default: List[SimplerConfig] = field(default=None, metadata={'help': 'list of SimplerConfig'}) # NOT SUPPORTED YET!
246
+
247
+ def check_values(self, ):
248
+ '''Check config fields'''
249
+ c = asdict(self)
250
+ check_argument('val_a', c, restricted=True, min_val=10, max_val=2056)
251
+ check_argument('val_b',
252
+ c,
253
+ restricted=True,
254
+ min_val=128,
255
+ max_val=4058,
256
+ allow_none=True)
257
+ check_argument('val_req', c, restricted=True)
258
+
259
+
260
+ def main():
261
+ # reference config that we like to match with the one parsed from argparse
262
+ config_ref = SimpleConfig(val_req='this is different',
263
+ val_a=222,
264
+ val_b=999,
265
+ nested_config=SimplerConfig(val_a=333),
266
+ mylist_with_default=[
267
+ SimplerConfig(val_a=222),
268
+ SimplerConfig(val_a=111)
269
+ ])
270
+
271
+ # create new config object from CLI inputs
272
+ parsed = SimpleConfig.init_from_argparse()
273
+ parsed.pprint()
274
+
275
+ # check the parsed config with the reference config
276
+ assert parsed == config_ref
277
+
278
+
279
+ if __name__ == '__main__':
280
+ sys.argv.extend(['--coqpit.val_req', 'this is different'])
281
+ sys.argv.extend(['--coqpit.val_a', '222'])
282
+ sys.argv.extend(['--coqpit.val_b', '999'])
283
+ sys.argv.extend(['--coqpit.nested_config.val_a', '333'])
284
+ sys.argv.extend(['--coqpit.mylist_with_default.0.val_a', '222'])
285
+ sys.argv.extend(['--coqpit.mylist_with_default.1.val_a', '111'])
286
+ main()
287
+ ```
288
+
289
+ ### πŸ€Έβ€β™€οΈ Merging coqpits
290
+ ```python
291
+ import os
292
+ from dataclasses import dataclass
293
+ from coqpit import Coqpit, check_argument
294
+
295
+
296
+ @dataclass
297
+ class CoqpitA(Coqpit):
298
+ val_a: int = 10
299
+ val_b: int = None
300
+ val_d: float = 10.21
301
+ val_c: str = "Coqpit is great!"
302
+
303
+
304
+ @dataclass
305
+ class CoqpitB(Coqpit):
306
+ val_d: int = 25
307
+ val_e: int = 257
308
+ val_f: float = -10.21
309
+ val_g: str = "Coqpit is really great!"
310
+
311
+
312
+ if __name__ == '__main__':
313
+ file_path = os.path.dirname(os.path.abspath(__file__))
314
+ coqpita = CoqpitA()
315
+ coqpitb = CoqpitB()
316
+ coqpitb.merge(coqpita)
317
+ print(coqpitb.val_a)
318
+ print(coqpitb.pprint())
319
+ ```
320
+
321
+ ## Development
322
+
323
+ Install the pre-commit hook to automatically check your commits for style and hinting issues:
324
+
325
+ ```bash
326
+ uv run pre-commit install
327
+ ```