Configuration schema registry #68
Replies: 5 comments 6 replies
-
This follows on from the suggestion for the grid (#28) . Basically, a system we will use that also permits users to add a new module with configuration by including in @register_schema('new_module_name')
def module_schema():
json_schema = """JSON SCHEMA HERE"""
return json_schema And then [new_module_name.vital_config]
critical_value = 1 That might also make it easy (I think) to maintain and test parallel module implementations - you could have configs using |
Beta Was this translation helpful? Give feedback.
-
Another thing this should definitely do is be able to take a set of modules and write out a default basic config file using the JSON schemas for those modules. So at the command line:
|
Beta Was this translation helpful? Give feedback.
-
I've put some thoughts about this below, but the tl;dr of it is that I think you shouldn't be working on this feature right now. In essence, you don't need config files right now, because your code doesn't do anything, so there's nothing to configure! If I were you, I would work on getting something working on the simulation front first and foremost so that you have some idea of what the Python API will look like. Once you've done that, you'll know what your data structures look like from Python's perspective and that will inform how you want to represent them in some other format (json, toml etc.) -- that is, if you really need to do that at all, which I'm not convinced about. Although I'm obviously not as well versed in the project's goal as you guys, my guess is that the most sensible way to run your simulations, at least for the first while, will be programmatically from Python itself, e.g. you'll just write scripts like so: import vr
PARAMETER = 10
SOME_OTHER_PARAMETER = 5
vr.run_simulation(parameter=PARAMETER, some_other_parameter=SOME_OTHER_PARAMETER) You need to be able to configure and run your simulations from Python (even if this functionality is only an internal API in the end); you could run simulations from a command-line program which parses config files. Given that the second of these depends on the first, my advice would be to do the first one first and then consider doing the second. Expressing your parameters from within Python itself is much simpler and cleaner. Another advantage is that your linter/CI system can check for e.g. type errors for you; no schemas needed. If you desperately want to load parameters from an external config file, that's dead easy with vanilla Python: import json, vr
with open('config.json', 'r') as file:
config = json.load(file)
vr.run_simulation(**config) From the Zen of Python "Simple is better than complex.": https://peps.python.org/pep-0020/ Am I missing something here? |
Beta Was this translation helpful? Give feedback.
-
I think you have covered pretty much every point on both sides of the argument, so I don't think I can add much. Just in case, here you have my opinion:
|
Beta Was this translation helpful? Give feedback.
-
@davidorme Sorry about slow reply! Yes I think @dalonsoa has summarised it well. If we go back to your snippet though: def validate(config):
# clearly missing some hasattr() and getattr()
for paths in config:
if config[path] < schema[path]["minimum"]:
raise ValueError(f"Value {path} below minimum") My main concern is what would happen if you decided you wanted to add e.g. a maximum to your parameter as well. Presumably you'd have to update both the python code and the JSON schema, else you'd have a bug? Other than that, I'd personally be reluctant to introduce what is essentially a separate language (i.e. JSON schemas) just for checking parameters when you can express everything in python anyways (and in fact you can express plenty of things you couldn't with a JSON schema....). Just a thought though! |
Beta Was this translation helpful? Give feedback.
-
So, I am in the process of writing a system to read in and validate config files (see issue #63). I am currently wondering how best to set up modular loading of config files. This is needed so that we can still validate config files in cases where we wanted to run a limited simulation with only particular modules. It is also potentially important if we developed alternative module implementations which would likely require different configuration parameters and data.
I've discussed this already with @davidorme, and we came up with the following plan to address it. Each module would have a separate json schema defined in their respective
__init__.py
. These would then all be added to a registry and decorated, so that the specific module schema could be accessed as e.g.SCHEMA_REGISTRY['plant']
.The core module always has to be configured, and so has to contain a list of the other modules to be configured. We use this list to find the other schema to validate configuration input against.
We were basically wondering @alexdewar @dalonsoa whether this seems like a sensible approach to you?
Beta Was this translation helpful? Give feedback.
All reactions