strictyaml icon indicating copy to clipboard operation
strictyaml copied to clipboard

Add support for writing out optional defaults

Open marxide opened this issue 4 years ago • 2 comments

Thanks for the library!

I'm considering using this to handle user-provided configurations for a scientific data processing pipeline. Most of the config parameters have sane defaults, but these defaults themselves are configurable by the system admin who deployed the pipeline software.

I'd really like to be able to have default config parameters that users can leave out of their config files, but I'd like to write out a copy of the full config spec including those parameters that weren't explicitly supplied by the user. This would give us a copy of the config used for an execution of the pipeline that records every config parameter and its value whether the user supplied it or not. This is important for us as we want to keep records of the configs used for reproducibility and we anticipate that different deployments of the software will have different defaults. Ideally, I'd also insert a comment next to the parameters where the default value was used, but that's less important.

I was thinking perhaps this could be implemented by adding a kwarg to Map.to_yaml, e.g. Map.to_yaml(drop_defaults=True) where setting it to False would include the optional keys with their default values in the output.

For example:

from strictyaml import load, Map, Optional, Int

yaml_snippet = """
a: 1
"""

schema = Map({
    "a": Int(),
    Optional("b", default=5): Int(),
})

yaml = load(yaml_snippet, schema)

print(yaml.as_yaml())
print(yaml.as_yaml(drop_defaults=False))

would output

a: 1

a: 1
b: 5  # default

Do you think such a feature is warranted? I'm happy to contribute.

marxide avatar Mar 23 '21 21:03 marxide

My instinctive reaction is that I like this idea, yes.

crdoconnor avatar Mar 23 '21 22:03 crdoconnor

The only concerns are

  • I have is that I was aiming for serialize(parse(x)) to be an idempotent operation. However, I don't have that yet now and I'm fairly sure this feature is compatible with that goal.
  • I'm not sure about "drop_defaults" as the name.

If you'd like to contribute the change, please feel free. I've been pretty busy lately!

crdoconnor avatar Mar 23 '21 22:03 crdoconnor