abseil/abseil-py

How to get the flags (key, value) pairs into a json object?

Closed this issue · 2 comments

Hi dev team,

How do we get the (key, value) mapping from FLAGS if we want to save them?

yilei commented

There is some lightweight serialization / deserialization support:

  • To save, use FLAGS.append_flags_into_file() or FLAGS.flags_into_string() for serialization of all flags
  • To restore, specify --flagfile=<somefile> on the command line pointing to a file containing the above content, or call FLAGS.read_flags_from_files(['--flagfile=<somefile>'])

If you really need json, you'll need to do it manually. Take a look at

module_flags = sorted(self.flags_by_module_dict().items())
s = ''
for unused_module_name, flags in module_flags:
flags = sorted(flags, key=lambda f: f.name)
for flag in flags:
if flag.value is not None:
s += flag.serialize() + '\n'
return s
how to get all flags and their serialized value.

Hope this helps.

yilei commented

Closing due to inactivity. Feel free to reopen if you still have questions.