小编Del*_*ted的帖子

Python argparse:很多选择导致丑陋的帮助输出

我有这个代码,我通常很高兴:

import argparse

servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer",
            "SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer", "SovServer" ]

parser = argparse.ArgumentParser(description="A program to update components on servers.")
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--list', dest="update", action='store_false', default=False, help='list server components')
group.add_argument('-u', '--updatepom', dest="update", action='store_true', help='update server components')
parser.add_argument('-o', '--only', nargs='*', choices=servers, help='Space separated list of case sensitive server names to process')
parser.add_argument('-s', '--skip', nargs='*', choices=servers, help='Space separated list of case sensitive server names to exclude from processing')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

我喜欢选择=服务器为我验证输入中的服务器名称,所以我不必这样做.但是,有这么多有效的选择会使帮助输出看起来很糟糕:

usage: …
Run Code Online (Sandbox Code Playgroud)

python argparse

48
推荐指数
5
解决办法
3万
查看次数

Python:ElementTree,获取Element的命名空间字符串

此XML文件命名为example.xml:

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>14.0.0</modelVersion>
  <groupId>.com.foobar.flubber</groupId>
  <artifactId>uberportalconf</artifactId>
  <version>13-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Environment for UberPortalConf</name>
  <description>This is the description</description>    
  <properties>
      <birduberportal.version>11</birduberportal.version>
      <promotiondevice.version>9</promotiondevice.version>
      <foobarportal.version>6</foobarportal.version>
      <eventuberdevice.version>2</eventuberdevice.version>
  </properties>
  <!-- A lot more here, but as it is irrelevant for the problem I have removed it -->
</project>
Run Code Online (Sandbox Code Playgroud)

如果我加载example.xml并使用ElementTree解析它,我可以看到它的命名空间http://maven.apache.org/POM/4.0.0.

>>> from xml.etree import ElementTree
>>> tree = ElementTree.parse('example.xml')
>>> print tree.getroot()
<Element '{http://maven.apache.org/POM/4.0.0}project' at 0x26ee0f0>
Run Code Online (Sandbox Code Playgroud)

我还没有找到一种方法来调用从而Element无需解析str(an_element)元素来获取命名空间.似乎必须有更好的方法.

python elementtree

20
推荐指数
4
解决办法
3万
查看次数

如何性能分析GNU Make文件

我们有很多GNU Make-files.我想计算构建期间使用的每个目标,以确定任何性能瓶颈.是否有工具或技术以方便和自动的方式执行此操作?

我可能想解析这些结果,以便在构建发生变化和增长时密切关注性能因素(但它已经非常庞大和复杂).

makefile gnu-make

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

转换一个列表进行设置,但如果为空则使用默认值

如果这样的列表不为空,我正在寻找一种更好的方法来为列表的内容分配一个集合,否则应该使用另一个列表.

如果有可能我想要一个更好的方式来写这个(或者为什么这是最好的方式的论据):

if args.onlyTheseServers:
    only = set(args.onlyTheseServers)
else:
    only = set(availableServers)
Run Code Online (Sandbox Code Playgroud)

python list set

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

Python:从字符串中解析ISO 8601日期和时间(使用标准模块)

我想解析SVN给出的条目的日期:

svn list --xml https://subversion:8765/svn/Foo/tags/
Run Code Online (Sandbox Code Playgroud)

如果我没有弄错,则使用ISO 8601标准.一个例子是:

dateString = "2012-02-14T11:22:34.593750Z"
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 2.7,我正在寻找最好的处理方法,仅使用标准模块.我想我会用正则表达式解析它:^---是一个匹配组,A表示我会假设它们总是存在(这可能是一个弱点)

dateString = "2012-02-14T11:22:34.593750Z"
              ^--- ^- ^-A^- ^- ^--------A
                        |               |
                        |  I think it always gives the date
                        |  as UTC, which Z means in ISO 8601.
                        |
                 Just a separator
Run Code Online (Sandbox Code Playgroud)

解析之后,我只需从中创建一个日期时间.

有没有更好的方法使用Python 2.7只有标准模块?

python python-2.7

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

Python argparse:强制列表项唯一

能够使用choices=servers下面的方法来验证列表项是很好的。

servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "SovServer" ]
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--only', nargs='*', choices=servers, help='Space separated list of case sensitive server names to process')
Run Code Online (Sandbox Code Playgroud)

是否可以强制列表中的项目是唯一的,从而不允许重复?

python argparse

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

标签 统计

python ×5

argparse ×2

elementtree ×1

gnu-make ×1

list ×1

makefile ×1

python-2.7 ×1

set ×1