Dumps config into a file or returns a string representation of the config. If a file argument is given, saves the config to that file using the format defined by the file argument extension. Otherwise, returns a string representing the config. The formatting of
(self, file: str = None)
| 420 | return default |
| 421 | |
| 422 | def dump(self, file: str = None): |
| 423 | """Dumps config into a file or returns a string representation of the |
| 424 | config. |
| 425 | |
| 426 | If a file argument is given, saves the config to that file using the |
| 427 | format defined by the file argument extension. |
| 428 | |
| 429 | Otherwise, returns a string representing the config. The formatting of |
| 430 | this returned string is defined by the extension of `self.filename`. If |
| 431 | `self.filename` is not defined, returns a string representation of a |
| 432 | dict (lowercased and using ' for strings). |
| 433 | |
| 434 | Examples: |
| 435 | >>> cfg_dict = dict(item1=[1, 2], item2=dict(a=0), |
| 436 | ... item3=True, item4='test') |
| 437 | >>> cfg = Config(cfg_dict=cfg_dict) |
| 438 | >>> dump_file = "a.py" |
| 439 | >>> cfg.dump(dump_file) |
| 440 | |
| 441 | Args: |
| 442 | file (str, optional): Path of the output file where the config |
| 443 | will be dumped. Defaults to None. |
| 444 | """ |
| 445 | from modelscope.fileio import dump |
| 446 | cfg_dict = super(Config, self).__getattribute__('_cfg_dict').to_dict() |
| 447 | if file is None: |
| 448 | if self.filename is None or self.filename.endswith('.py'): |
| 449 | return self.pretty_text |
| 450 | else: |
| 451 | file_format = self.filename.split('.')[-1] |
| 452 | return dump(cfg_dict, file_format=file_format) |
| 453 | elif file.endswith('.py'): |
| 454 | with open(file, 'w', encoding='utf-8') as f: |
| 455 | f.write(self.pretty_text) |
| 456 | else: |
| 457 | file_format = file.split('.')[-1] |
| 458 | return dump(cfg_dict, file=file, file_format=file_format) |
| 459 | |
| 460 | def merge_from_dict(self, options, allow_list_keys=True, force=True): |
| 461 | """Merge dict into cfg_dict. |