久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <tfoot id='Jnup4'></tfoot>
      • <bdo id='Jnup4'></bdo><ul id='Jnup4'></ul>
      <i id='Jnup4'><tr id='Jnup4'><dt id='Jnup4'><q id='Jnup4'><span id='Jnup4'><b id='Jnup4'><form id='Jnup4'><ins id='Jnup4'></ins><ul id='Jnup4'></ul><sub id='Jnup4'></sub></form><legend id='Jnup4'></legend><bdo id='Jnup4'><pre id='Jnup4'><center id='Jnup4'></center></pre></bdo></b><th id='Jnup4'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Jnup4'><tfoot id='Jnup4'></tfoot><dl id='Jnup4'><fieldset id='Jnup4'></fieldset></dl></div>

      1. <small id='Jnup4'></small><noframes id='Jnup4'>

        <legend id='Jnup4'><style id='Jnup4'><dir id='Jnup4'><q id='Jnup4'></q></dir></style></legend>
      2. 帶有嵌套命名空間的 argparse 子命令

        argparse subcommands with nested namespaces(帶有嵌套命名空間的 argparse 子命令)

          • <bdo id='iKSU1'></bdo><ul id='iKSU1'></ul>

            <i id='iKSU1'><tr id='iKSU1'><dt id='iKSU1'><q id='iKSU1'><span id='iKSU1'><b id='iKSU1'><form id='iKSU1'><ins id='iKSU1'></ins><ul id='iKSU1'></ul><sub id='iKSU1'></sub></form><legend id='iKSU1'></legend><bdo id='iKSU1'><pre id='iKSU1'><center id='iKSU1'></center></pre></bdo></b><th id='iKSU1'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='iKSU1'><tfoot id='iKSU1'></tfoot><dl id='iKSU1'><fieldset id='iKSU1'></fieldset></dl></div>
          • <small id='iKSU1'></small><noframes id='iKSU1'>

            <legend id='iKSU1'><style id='iKSU1'><dir id='iKSU1'><q id='iKSU1'></q></dir></style></legend>
              <tbody id='iKSU1'></tbody>

              <tfoot id='iKSU1'></tfoot>
                • 本文介紹了帶有嵌套命名空間的 argparse 子命令的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  argparse 是否提供內(nèi)置的工具來讓它解析組或解析器進入他們自己的命名空間?我覺得我一定是在某個地方遺漏了一個選項.

                  Does argparse provide built-in facilities for having it parse groups or parsers into their own namespaces? I feel like I must be missing an option somewhere.

                  編輯:這個例子可能不完全是我應(yīng)該做的來構(gòu)建解析器來實現(xiàn)我的目標(biāo),但它是我到目前為止的工作.我的具體目標(biāo)是能夠為子解析器提供解析為命名空間字段的選項組.我對父母的想法只是為了同樣的目的使用通用選項.

                  Edit: This example is probably not exactly what I should be doing to structure the parser to meet my goal, but it was what I worked out so far. My specific goal is to be able to give subparsers groups of options that are parsed into namespace fields. The idea I had with parent was simply to use common options for this same purpose.

                  例子:

                  import argparse
                  
                  # Main parser
                  main_parser = argparse.ArgumentParser()
                  main_parser.add_argument("-common")
                  
                  # filter parser
                  filter_parser = argparse.ArgumentParser(add_help=False)
                  filter_parser.add_argument("-filter1")
                  filter_parser.add_argument("-filter2")
                  
                  # sub commands
                  subparsers = main_parser.add_subparsers(help='sub-command help')
                  
                  parser_a = subparsers.add_parser('command_a', help="command_a help", parents=[filter_parser])
                  parser_a.add_argument("-foo")
                  parser_a.add_argument("-bar")
                  
                  parser_b = subparsers.add_parser('command_b', help="command_b help", parents=[filter_parser])
                  parser_b.add_argument("-biz")
                  parser_b.add_argument("-baz")
                  
                  # parse
                  namespace = main_parser.parse_args()
                  print namespace
                  

                  這顯然是我得到的:

                  $ python test.py command_a -foo bar -filter1 val
                  Namespace(bar=None, common=None, filter1='val', filter2=None, foo='bar')
                  

                  但這才是我真正追求的:

                  But this is what I am really after:

                  Namespace(bar=None, common=None, foo='bar', 
                            filter=Namespace(filter1='val', filter2=None))
                  

                  然后更多的選項組已經(jīng)解析到命名空間中:

                  And then even more groups of options already parsed into namespaces:

                  Namespace(common=None, 
                            foo='bar', bar=None,  
                            filter=Namespace(filter1='val', filter2=None),
                            anotherGroup=Namespace(bazers='val'),
                            anotherGroup2=Namespace(fooers='val'),
                            )
                  

                  我發(fā)現(xiàn)了一個 相關(guān)問題這里,但它涉及一些自定義解析,似乎只涵蓋了一個非常具體的情況.

                  I've found a related question here but it involves some custom parsing and seems to only covers a really specific circumstance.

                  是否有一個選項可以告訴 argparse 將某些組解析為命名空間字段?

                  Is there an option somewhere to tell argparse to parse certain groups into namespaced fields?

                  推薦答案

                  如果重點只是將選定的參數(shù)放在自己的 namespace 中,并且子解析器(和父級)的使用是偶然的這個問題,這個自定義操作可能會解決問題.

                  If the focus is on just putting selected arguments in their own namespace, and the use of subparsers (and parents) is incidental to the issue, this custom action might do the trick.

                  class GroupedAction(argparse.Action):    
                      def __call__(self, parser, namespace, values, option_string=None):
                          group,dest = self.dest.split('.',2)
                          groupspace = getattr(namespace, group, argparse.Namespace())
                          setattr(groupspace, dest, values)
                          setattr(namespace, group, groupspace)
                  

                  有多種方法可以指定group 名稱.它可以在定義動作時作為參數(shù)傳遞.它可以作為參數(shù)添加.這里我選擇從dest解析(這樣namespace.filter.filter1可以得到filter.filter1的值.

                  There are various ways of specifying the group name. It could be passed as an argument when defining the Action. It could be added as parameter. Here I chose to parse it from the dest (so namespace.filter.filter1 can get the value of filter.filter1.

                  # Main parser
                  main_parser = argparse.ArgumentParser()
                  main_parser.add_argument("-common")
                  
                  filter_parser = argparse.ArgumentParser(add_help=False)
                  filter_parser.add_argument("--filter1", action=GroupedAction, dest='filter.filter1', default=argparse.SUPPRESS)
                  filter_parser.add_argument("--filter2", action=GroupedAction, dest='filter.filter2', default=argparse.SUPPRESS)
                  
                  subparsers = main_parser.add_subparsers(help='sub-command help')
                  
                  parser_a = subparsers.add_parser('command_a', help="command_a help", parents=[filter_parser])
                  parser_a.add_argument("--foo")
                  parser_a.add_argument("--bar")
                  parser_a.add_argument("--bazers", action=GroupedAction, dest='anotherGroup.bazers', default=argparse.SUPPRESS)
                  ...
                  namespace = main_parser.parse_args()
                  print namespace
                  

                  我必須添加 default=argparse.SUPPRESS 以便 bazers=None 條目不會出現(xiàn)在主命名空間中.

                  I had to add default=argparse.SUPPRESS so a bazers=None entry does not appear in the main namespace.

                  結(jié)果:

                  >>> python PROG command_a --foo bar --filter1 val --bazers val
                  Namespace(anotherGroup=Namespace(bazers='val'), 
                      bar=None, common=None, 
                      filter=Namespace(filter1='val'), 
                      foo='bar')
                  

                  如果您需要嵌套命名空間中的默認條目,您可以事先定義命名空間:

                  If you need default entries in the nested namespaces, you could define the namespace before hand:

                  filter_namespace = argparse.Namespace(filter1=None, filter2=None)
                  namespace = argparse.Namespace(filter=filter_namespace)
                  namespace = main_parser.parse_args(namespace=namespace)
                  

                  結(jié)果和以前一樣,除了:

                  result as before, except for:

                  filter=Namespace(filter1='val', filter2=None)
                  

                  這篇關(guān)于帶有嵌套命名空間的 argparse 子命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復(fù)導(dǎo)入頂級名稱的情況下構(gòu)造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發(fā)帶有已編譯動態(tài)共享庫的 Python 包)

                  • <legend id='vfCJr'><style id='vfCJr'><dir id='vfCJr'><q id='vfCJr'></q></dir></style></legend>

                    <small id='vfCJr'></small><noframes id='vfCJr'>

                  • <i id='vfCJr'><tr id='vfCJr'><dt id='vfCJr'><q id='vfCJr'><span id='vfCJr'><b id='vfCJr'><form id='vfCJr'><ins id='vfCJr'></ins><ul id='vfCJr'></ul><sub id='vfCJr'></sub></form><legend id='vfCJr'></legend><bdo id='vfCJr'><pre id='vfCJr'><center id='vfCJr'></center></pre></bdo></b><th id='vfCJr'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vfCJr'><tfoot id='vfCJr'></tfoot><dl id='vfCJr'><fieldset id='vfCJr'></fieldset></dl></div>

                      1. <tfoot id='vfCJr'></tfoot>

                          <tbody id='vfCJr'></tbody>

                          • <bdo id='vfCJr'></bdo><ul id='vfCJr'></ul>
                            主站蜘蛛池模板: 成人免费影院 | 久久五月婷 | 成人精品一区 | 国产一区在线视频 | 久久精品亚洲精品国产欧美 | av毛片| 日本一区二区三区免费观看 | 99视频久 | 人人干人人玩 | 日韩插插 | 91国产视频在线 | 亚洲国产精品视频 | 国产精品免费观看视频 | 日韩欧美国产一区二区三区 | 国产九九精品视频 | 中文字幕 欧美 日韩 | 久久精品综合 | 久久91| 精品一区二区三区在线观看 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 黄色一级大片在线免费看产 | 日韩免费视频一区二区 | 99亚洲综合 | 国产精品明星裸体写真集 | 亚洲国产成人精品女人久久久 | 国产精品欧美一区二区 | 性高湖久久久久久久久aaaaa | 国产精品资源在线 | 精品久久久久久亚洲精品 | 午夜免费看 | 欧美激情国产精品 | 亚洲一区在线日韩在线深爱 | 国产在线视频一区二区董小宛性色 | 欧美一级片在线看 | 91一区二区三区 | 亚洲精品视频播放 | 亚洲天堂精品久久 | 成人精品一区亚洲午夜久久久 | 国产精品视频网 | 欧美黄色一区 | 99reav |