Search code examples
pythonyamlabseil

What's the idiomatic way of performing conversions from python gflags to yaml and back?


I'm using absl-py's python gflags for my project.

They're a convenient way of specifying configurations, but I frequently like to run the same configuration, and saving command line invocations is not preferable because they are hard to read and maintain.

I'd like to instead be able to maintain YAML text files that specify flag values. For instance, a program that might be invoked as

python myapp.py --some_int_arg 3 --noboolean_value

could also be invoked as

python myapp.py --from_config config.yaml

where the contents of config.yaml are

some_int_arg: 3
boolean_value: false

and analogously it'd be nice to be able to construct the file above given the first invocation.

What is the most idiomatic way of

  1. accepting a configuration file path on the command line and having that override all absl.flags.FLAGS values?
  2. converting an absl.flags.FLAGS instance to a text representation (preferably YAML) that can then be reinvoked using (1)?

Edit. There seems to be an easy way to accept a configuration file, using --flagfile, which should just contain each flag's values on a new line. However, my question (2) still stands, how would I be able to recover such a file? Currently the only solution seems to be to crawl flags.FLAGS and stringify each flag value manually.


Solution

  • FLAGS.flags_into_string() should return a string that can be put in a flagfile. Or FLAGS.append_flags_into_file('path/to/flagfile.txt') will append the flags to that file.

    Does this work for you?