eladrich / pyrallis

Pyrallis is a framework for structured configuration parsing from both cmd and files. Simply define your desired configuration structure as a dataclass and let pyrallis do the rest!
https://eladrich.github.io/pyrallis/
MIT License
201 stars 7 forks source link

TOML support #3

Closed bayerj closed 2 years ago

bayerj commented 2 years ago

Hello,

Is there a chance to support different configuration formats, e.g. TOML or JSON? I wonder how deeply yaml is integrated.

Best, -Justin

PS: Nice project. :)

eladrich commented 2 years ago

Hi Justin, That's a great question! The yaml integration is localized to the following parts of pyrallis

  1. The pyrallis.dump function (which uses the general pyrallis.encode)
  2. The pyrallis.load function (which uses the general pyrallis.decode)
  3. The loading mechanism where it is used to both load the config_path and parse all cmd strings (to ensure a uniform parsing syntax as explained here)

I've been thinking about what would be a good way to support other formats as well, but decided to first release the package as it is, and maybe add support in the future.

The thing is that for 1-2 I can always just add additional functions or arguments, the confusing part is the loading mechanism. Say someone wants to pass the following value to pyrallis {1: 2}, right now pyrallis uses only yaml for parsing strings so

python train.py --int_dict="{1: 2}"

and

int_dict: {1: 2}

would function the same way and would be parsed correctly.

Now, for jsons int keys in dictionaries are not allowed, that would mean that the console mode

python train.py --int_dict="{1: 2}"

should also fail when using jsons (otherwise we are in a confusing situation where it can be loaded from cmd but not from file). So what happens here that the functionally of pyrallis changes when a different format is used, and I want to make sure that there's a way to allow that without confusing users.

I would give it some more thought and would be happy to hear if you have any specific ideas on that matter.

eladrich commented 2 years ago

Added an option in MR #7 to globally change the configuration format

pyrallis.set_config_type('json')
# or
pyrallis.set_config_type(pyrallis.ConfigType.JSON)

Still need to review and update docs.