Print the options parsed by ArgumentParser. Args: options: parsed options parser: the parser used to parse `options`. output_file: if given, will save a stdout into a txt file output_json_file: if given, will s
(
options,
parser: argparse.ArgumentParser = None,
output_file: str = None,
output_json_file: str = None,
output_yaml_file: str = None,
)
| 101 | |
| 102 | |
| 103 | def print_options( |
| 104 | options, |
| 105 | parser: argparse.ArgumentParser = None, |
| 106 | output_file: str = None, |
| 107 | output_json_file: str = None, |
| 108 | output_yaml_file: str = None, |
| 109 | ): |
| 110 | """ |
| 111 | Print the options parsed by ArgumentParser. |
| 112 | |
| 113 | Args: |
| 114 | options: |
| 115 | parsed options |
| 116 | parser: |
| 117 | the parser used to parse `options`. |
| 118 | output_file: |
| 119 | if given, will save a stdout into a txt file |
| 120 | output_json_file: |
| 121 | if given, will save the options into a json file. |
| 122 | output_yaml_file: |
| 123 | if given, will save the options into a yaml file. |
| 124 | |
| 125 | Notes: |
| 126 | The function will print both current options and |
| 127 | their default values (if parser is given and if they are different). |
| 128 | """ |
| 129 | |
| 130 | if not isinstance(options, dict): |
| 131 | options_dict = dict() |
| 132 | for k, v in sorted(vars(options).items()): |
| 133 | options_dict[k] = v |
| 134 | else: |
| 135 | options_dict = options |
| 136 | |
| 137 | message = "" |
| 138 | message += "----------------- Options ---------------\n" |
| 139 | |
| 140 | for k, v in sorted(options_dict.items()): |
| 141 | comment = "" |
| 142 | if parser is not None: |
| 143 | default = parser.get_default(k) |
| 144 | if v != default: |
| 145 | comment = "\t[default: %s]" % str(default) |
| 146 | message += "{:>25}: {:<30}{}\n".format(str(k), str(v), comment) |
| 147 | message += "----------------- End -------------------" |
| 148 | print(message) |
| 149 | |
| 150 | # save to the disk |
| 151 | if output_file is not None and options_dict.get("rank", 0) == 0: |
| 152 | with open(output_file, "wt") as opt_file: |
| 153 | opt_file.write(message) |
| 154 | opt_file.write("\n") |
| 155 | |
| 156 | # save to the disk |
| 157 | if output_json_file is not None and options_dict.get("rank", 0) == 0: |
| 158 | with open(output_json_file, "w") as opt_file: |
| 159 | json.dump(options_dict, opt_file) |
| 160 |