當前位置: 首頁>>代碼示例>>Python>>正文


Python ArgParser.parse_known_args方法代碼示例

本文整理匯總了Python中configargparse.ArgParser.parse_known_args方法的典型用法代碼示例。如果您正苦於以下問題:Python ArgParser.parse_known_args方法的具體用法?Python ArgParser.parse_known_args怎麽用?Python ArgParser.parse_known_args使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在configargparse.ArgParser的用法示例。


在下文中一共展示了ArgParser.parse_known_args方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: go_2

# 需要導入模塊: from configargparse import ArgParser [as 別名]
# 或者: from configargparse.ArgParser import parse_known_args [as 別名]
 def go_2(p, current_prefix, current_ns):
     if isinstance(p, BaseParser):
         new_p = ArgParser(default_config_files=config_files)
         for a in p.argparser._actions:
             new_a = copy.copy(a)
             ss = copy.deepcopy(new_a.option_strings)
             for ix, s in enumerate(new_a.option_strings):
                 if s.startswith("--"):
                     ss[ix] = "-" + current_prefix + "-" + s[2:]
                 else:
                     raise NotImplementedError
                 new_a.option_strings = ss
             new_p._add_action(new_a)
         _used_args, _rest = new_p.parse_known_args(args, namespace=current_ns)
         # add a "_flags" field to each object so we know what flags caused a certain option to be set:
         # (however, note that post-parsing we may munge around ...)
         flags_dict = defaultdict(set)
         for action in new_p._actions:
             for opt in action.option_strings:
                 flags_dict[action.dest].add(opt)
         current_ns.flags_ = Namespace(**flags_dict)
         # TODO: could continue parsing from `_rest` instead of original `args`
     elif isinstance(p, CompoundParser):
         current_ns.flags_ = set()  # could also check for the CompoundParser case and not set flags there,
                                    # since there will never be any
         for q in p.parsers:
             ns = Namespace()
             if q.namespace in current_ns.__dict__:
                 raise ValueError("Namespace field '%s' already in use" % q.namespace)
                 # TODO could also allow, say, a None
             else:
                 # gross but how to write n-ary identity fn that behaves sensibly on single arg??
                 current_ns.__dict__[q.namespace] = ns
                 # FIXME this casting doesn't work for configurations with positional arguments,
                 # which aren't unpacked correctly -- better to use a namedtuple
                 # (making all arguments keyword-only also works, but then you have to supply
                 # often meaningless defaults in the __init__)
             go_2(q.parser, current_prefix=current_prefix + (('-' + q.prefix) if q.prefix is not None else ''),
                  current_ns=ns)
             # If a cast function is provided, apply it to the namespace, possibly doing dynamic type checking
             # and also allowing the checker to provide hinting for the types of the fields
             flags = ns.flags_
             del ns.flags_
             fixed = (q.cast(current_ns.__dict__[q.namespace]) #(q.cast(**vars(current_ns.__dict__[q.namespace]))
                                                 if q.cast else current_ns.__dict__[q.namespace])
             if isinstance(fixed, tuple):
                 fixed = fixed.replace(flags_=flags)
             elif isinstance(fixed, Namespace):
                 setattr(fixed, "flags_", flags)
             else:
                 raise ValueError("currently only Namespace and NamedTuple objects are supported return types from "
                                  "parsing; got %s (a %s)" % (fixed, type(fixed)))
             current_ns.__dict__[q.namespace] = fixed
             # TODO current_ns or current_namespace or ns or namespace?
     else:
         raise TypeError("parser %s wasn't a %s (%s or %s) but a %s" %
                         (p, Parser, BaseParser, CompoundParser, p.__class__))
開發者ID:Mouse-Imaging-Centre,項目名稱:pydpiper,代碼行數:59,代碼來源:arguments.py

示例2: main

# 需要導入模塊: from configargparse import ArgParser [as 別名]
# 或者: from configargparse.ArgParser import parse_known_args [as 別名]
def main():
    # command line option handling
    # use an environment variable to look for a default config file
    # Alternately, we could use a default location for the file
    # (say `files = ['/etc/pydpiper.cfg', '~/pydpiper.cfg', './pydpiper.cfg']`)
    # TODO this logic is duplicated in application.py
    #if "PYDPIPER_CONFIG_FILE" in os.environ:
    default_config_file = os.getenv("PYDPIPER_CONFIG_FILE")
    if default_config_file is not None:
        try:
            with open(PYDPIPER_CONFIG_FILE):
                pass
        except:
            warnings.warn(f"PYDPIPER_CONFIG_FILE is set to '{default_config_file}', which can't be opened.")
    if default_config_file is not None:
        files = [default_config_file]
    else:
        files = []

    from pydpiper.core.arguments import _mk_execution_parser
    parser = ArgParser(default_config_files=files)
    _mk_execution_parser(parser)

    # using parse_known_args instead of parse_args is a hack since we
    # currently send ALL arguments from the main program to the executor.
    # Alternately, we could keep a copy of the executor parser around
    # when constructing the executor shell command
    options, _ = parser.parse_known_args()

    ensure_exec_specified(options.num_exec)

    def local_launch(options):
        pe = pipelineExecutor(options=options, uri_file=options.urifile, pipeline_name="anon-executor")  # didn't parse application options so don't have a --pipeline-name
        # FIXME - I doubt missing the other options even works, otherwise we could change the executor interface!!
        # executors don't use any shared-memory constructs, so OK to copy
        ps = [Process(target=launchExecutor, args=(pe,))
              for _ in range(options.num_exec)]
        for p in ps:
            p.start()
        for p in ps:
            p.join()

    if options.local:
        local_launch(options)
    elif options.submit_server:
        roq = q.runOnQueueingSystem(options, sysArgs=sys.argv)
        for i in range(options.num_exec):
            roq.createAndSubmitExecutorJobFile(i, after=None,
                                               time=q.timestr_to_secs(options.time))
    elif options.queue_type is not None:
        for i in range(options.num_exec):
            pe = pipelineExecutor(options=options, uri_file=options.urifile, pipeline_name="anon-executor")
            pe.submitToQueue(1)  # TODO is there a reason why we have logic for submitting `i` executors again here?
    else:
        local_launch(options)
開發者ID:Mouse-Imaging-Centre,項目名稱:pydpiper,代碼行數:57,代碼來源:pipeline_executor.py

示例3: go_2

# 需要導入模塊: from configargparse import ArgParser [as 別名]
# 或者: from configargparse.ArgParser import parse_known_args [as 別名]
 def go_2(p, current_prefix, current_ns):
     if isinstance(p, BaseParser):
         new_p = ArgParser(default_config_files=config_files)
         for a in p.argparser._actions:
             new_a = copy.copy(a)
             ss = copy.deepcopy(new_a.option_strings)
             for ix, s in enumerate(new_a.option_strings):
                 if s.startswith("--"):
                     ss[ix] = "-" + current_prefix + "-" + s[2:]
                 else:
                     raise NotImplementedError
                 new_a.option_strings = ss
             new_p._add_action(new_a)
         _used_args, _rest = new_p.parse_known_args(args, namespace=current_ns)
         # TODO: could continue parsing from `_rest` instead of original `args`
     elif isinstance(p, CompoundParser):
         for q in p.parsers:
             ns = Namespace()
             if q.namespace in current_ns.__dict__:
                 raise ValueError("Namespace field '%s' already in use" % q.namespace)
                 # TODO could also allow, say, a None
             else:
                 # gross but how to write n-ary identity fn that behaves sensibly on single arg??
                 current_ns.__dict__[q.namespace] = ns
                 # FIXME this casting doesn't work for configurations with positional arguments,
                 # which aren't unpacked correctly -- better to use a namedtuple
                 # (making all arguments keyword-only also works, but then you have to supply
                 # often meaningless defaults in the __init__)
             go_2(q.parser, current_prefix=current_prefix + (('-' + q.prefix) if q.prefix is not None else ''),
                  current_ns=ns)
             # If a cast function is provided, apply it to the namespace, possibly doing dynamic type checking
             # and also allowing the checker to provide hinting for the types of the fields
             current_ns.__dict__[q.namespace] = (q.cast(current_ns.__dict__[q.namespace]) #(q.cast(**vars(current_ns.__dict__[q.namespace]))
                                                 if q.cast else current_ns.__dict__[q.namespace])
             # TODO current_ns or current_namespace or ns or namespace?
     else:
         raise TypeError("parser %s wasn't a %s (%s or %s) but a %s" %
                         (p, Parser, BaseParser, CompoundParser, p.__class__))
開發者ID:psteadman,項目名稱:pydpiper,代碼行數:40,代碼來源:arguments.py

示例4: ArgParser

# 需要導入模塊: from configargparse import ArgParser [as 別名]
# 或者: from configargparse.ArgParser import parse_known_args [as 別名]
    if default_config_file is not None:
        files = [default_config_file]
    else:
        files = []
    parser = ArgParser(default_config_files=files)    

    rf.addGenRegArgumentGroup(parser) # just to get --pipeline-name
    addExecutorArgumentGroup(parser)

    # using parse_known_args instead of parse_args is a hack since we
    # currently send ALL arguments from the main program to the executor
    # on PBS queues (FIXME not yet true on SGE queues, but this is
    # not the best solution anyway).
    # Alternately, we could keep a copy of the executor parser around
    # when constructing the executor shell command
    options = parser.parse_known_args()[0]

    #Check to make sure some executors have been specified. 
    noExecSpecified(options.num_exec)

    def local_launch(options):
        pe = pipelineExecutor(options)
        # executors don't use any shared-memory constructs, so OK to copy
        ps = [Process(target=launchExecutor, args=(pe,))
              for _ in range(options.num_exec)]
        for p in ps:
            p.start()
        for p in ps:
            p.join()

    if options.local:
開發者ID:edeguzman,項目名稱:pydpiper,代碼行數:33,代碼來源:pipeline_executor.py


注:本文中的configargparse.ArgParser.parse_known_args方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。