小编use*_*227的帖子

如何在python argparse中使参数可选

我想对myprog工作进行这些调用,而不是其他人.

$ python3 myprog.py -i infile -o outfile
$ python3 myprog.py -o outfile
$ python3 myprog.py -o
$ python3 myprog.py 
Run Code Online (Sandbox Code Playgroud)

特别是我想指定infile而不是outfile是非法的.

在第三种情况下,假定outfile的默认名称为"out.json".在第二种,第三种和第四种情况下,假定输入文件的默认名称为"file.n.json",其中n是整数版本号.在第四种情况下,输出文件将是"file.n + 1.json",其中n + 1是比输入文件上的版本号大1的版本号.我的代码的相关部分是:

import argparse

parser = argparse.ArgumentParser(description="first python version")
parser.add_argument('-i', '--infile', nargs=1, type=argparse.FileType('r'), help='input file, in JSON format')
parser.add_argument('-o', '--outfile', nargs='?', type=argparse.FileType('w'), default='out.json', help='output file, in JSON format')

args = parser.parse_args()

print("Here's what we saw on the command line: ")
print("args.infile",args.infile)
print("args.outfile",args.outfile)

if args.infile and not args.outfile:
    parser.error("dont specify an infile without specifying an outfile")
elif …
Run Code Online (Sandbox Code Playgroud)

python argparse

26
推荐指数
2
解决办法
3万
查看次数

Pip列表与AssertionError崩溃

可能是下面的问题?看起来pip中有一个bug.我昨天使用brew安装了pip.在此之前,我已经安装了大多数python包$ python setup.py install

steves-MacBook-Pro:server steve$ pip -V
pip 1.4.1 from /Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg (python 2.7)
steves-MacBook-Pro:server steve$ pip list
altgraph (0.9)
bdist-mpkg (0.4.4)
... 
...(a bunch of python packages omitted here for brevity)
...
...
requests (2.0.0)
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/basecommand.py", line 134, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/commands/list.py", line 80, in run
    self.run_listing(options)
  File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/commands/list.py", line 127, in run_listing
    self.output_package_listing(installed_packages)
  File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/commands/list.py", line 136, in output_package_listing
    if dist_is_editable(dist):
  File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/util.py", line 347, in …
Run Code Online (Sandbox Code Playgroud)

python pip list

8
推荐指数
1
解决办法
2061
查看次数

在Bootstrap内联表单中控制<input>框大小

我有一个bootstrap v3内联形式:

<div id="searchsection" hidden>
  <form id="searchform" class="form-inline" role="form">
    <label>Find:</label> 
    <select id="field" class="form-control">
      <optgroup label="Strings">
        <option value="authors" selected="selected">Authors</option>
        <option value="title">Title</option>
        <option value="pub">Publication Name</option>
        <option value="keywords">Keywords</option>
        <option value="physloc">Physical Location</option>
        <option value="comment">Comment</option>
      </optgroup>
      <optgroup label="Dates">
        <option value="datepub">Publication Date</option>
        <option value="dateread">Date Read</option>
      </optgroup>
    </select>  

    <input type="text" id="narrowon1" class="form-control"/>
    <label for="narrowon2" class="form-control"></label> 
    <input type="text" id="narrowon2" class="form-control" placeholder="YYYYMM" hidden/>
    <input type="button" id="narrower" name="narrower" value="Narrow" class="form-control btn btn-primary"/>
    <input type="button" id="widener" name="widener" value="Widen" class="form-control btn btn-primary"/>
  </form>
</div> <!-- end of searchsection -->
Run Code Online (Sandbox Code Playgroud)

当optgroup是Dates时,我想减小narrowon1和narrowon2的宽度,这样它们每个只能容纳六位数; 我在考虑设置size ="6".但是,当我只是通过诸如的语句在jQuery中添加这些属性时 …

html javascript twitter-bootstrap

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

argparse ×1

html ×1

javascript ×1

list ×1

pip ×1

twitter-bootstrap ×1