MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / FlexibleArgumentParser

Class FlexibleArgumentParser

fastdeploy/utils.py:497–549  ·  view source on GitHub ↗

Extend argparse.ArgumentParser to support loading parameters from YAML files.

Source from the content-addressed store, hash-verified

495
496
497class FlexibleArgumentParser(argparse.ArgumentParser):
498 """
499 Extend argparse.ArgumentParser to support loading parameters from YAML files.
500 """
501
502 def __init__(self, *args, config_arg="--config", sep="_", **kwargs):
503 super().__init__(*args, **kwargs)
504 self.sep = sep
505
506 # Create parser to prase yaml file
507 self.tmp_parser = argparse.ArgumentParser(add_help=False)
508 self.tmp_parser.add_argument(config_arg, type=str, help="Path to YAML config file")
509
510 def parse_args(self, args=None, namespace=None):
511 tmp_ns, remaining_args = self.tmp_parser.parse_known_args(args=args)
512 config_path = tmp_ns.config
513
514 config = {}
515 if config_path:
516 with open(config_path, "r") as f:
517 loaded_config = yaml.safe_load(f)
518 config = loaded_config
519
520 # Get declared parameters
521 defined_actions = {action.dest: action for action in self._actions}
522 filtered_config = {k: v for k, v in config.items() if k in defined_actions}
523
524 # Set parameters
525 if namespace is None:
526 namespace = argparse.Namespace()
527 for key, value in filtered_config.items():
528 action = defined_actions[key]
529 if action.type is not None and isinstance(value, (str, int, float)):
530 try:
531 str_value = str(value).strip()
532 if str_value == "":
533 converted = None
534 else:
535 converted = action.type(str_value)
536 value = converted
537 except Exception as e:
538 llm_logger.error(f"Error converting '{key}' with value '{value}': {e}")
539 setattr(namespace, key, value)
540 args = super().parse_args(args=remaining_args, namespace=namespace)
541
542 # Args correction
543 for config_name, flag_name in ARGS_CORRECTION_LIST:
544 if hasattr(args, config_name) and hasattr(args, flag_name):
545 # config is a dict
546 config = getattr(args, config_name, None)
547 if config is not None and flag_name in config.keys():
548 setattr(args, flag_name, config[flag_name])
549 return args
550
551
552def resolve_obj_from_strname(strname: str):

Callers 7

mainFunction · 0.90
mainFunction · 0.90
parse_argsFunction · 0.90
api_server.pyFile · 0.90
mainFunction · 0.90
quick_benchmark.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected