utility.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from argparse import ArgumentParser, RawDescriptionHelpFormatter
  2. import yaml
  3. class ArgsParser(ArgumentParser):
  4. def __init__(self):
  5. super(ArgsParser,
  6. self).__init__(formatter_class=RawDescriptionHelpFormatter)
  7. self.add_argument('-c', '--config', help='configuration file to use')
  8. self.add_argument('-o',
  9. '--opt',
  10. nargs='*',
  11. help='set configuration options')
  12. self.add_argument('--local_rank')
  13. self.add_argument('--local-rank')
  14. def parse_args(self, argv=None):
  15. args = super(ArgsParser, self).parse_args(argv)
  16. assert args.config is not None, 'Please specify --config=configure_file_path.'
  17. args.opt = self._parse_opt(args.opt)
  18. return args
  19. def _parse_opt(self, opts):
  20. config = {}
  21. if not opts:
  22. return config
  23. for s in opts:
  24. s = s.strip()
  25. k, v = s.split('=', 1)
  26. if '.' not in k:
  27. config[k] = yaml.load(v, Loader=yaml.Loader)
  28. else:
  29. keys = k.split('.')
  30. if keys[0] not in config:
  31. config[keys[0]] = {}
  32. cur = config[keys[0]]
  33. for idx, key in enumerate(keys[1:]):
  34. if idx == len(keys) - 2:
  35. cur[key] = yaml.load(v, Loader=yaml.Loader)
  36. else:
  37. cur[key] = {}
  38. cur = cur[key]
  39. return config