小编Mik*_*oll的帖子

如何在Python中重构100个类方法?

我正在研究一些遗留代码(由喜欢意大利面条代码的人创建),它有超过150个吸气剂和超过150个安装者.吸气剂看起来像这样:

def GetLoadFee(self):
    r_str = ""
    if len(self._LoadFee) > 20:
        r_str = self._LoadFee[:20]
    else:
        r_str = self._LoadFee.strip()
    return r_str.strip()

def GetCurrency(self):
    r_str = ""
    if len(self._Currency) > 3:
        r_str = self._Currency[:3]
    else:
        r_str = self._Currency.strip()
    return r_str.strip()
Run Code Online (Sandbox Code Playgroud)

我希望获取每个Getter的内容,并将它们放入装饰器/闭包或其他一些方法,以使这些代码更容易维护.Setters都是一个衬垫,因此它们并不重要.但它们基本上都是一样的.有什么想法可以减轻痛苦吗?

注意:我仍然需要原始的Getter名称,因为它们在其他程序中使用,因为这个讨厌的脚本被用在许多其他遗留代码中.

python

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

Python lxml - 如何删除空重复的标记

我有一些由脚本生成的XML,可能有也可能没有空元素.有人告诉我,现在我们不能在XML中拥有空元素.这是一个例子:

<customer>  
    <govId>
       <id>@</id>
       <idType>SSN</idType>
           <issueDate/>
           <expireDate/>
           <dob/>
           <state/>
           <county/>
           <country/>
    </govId>
    <govId>
        <id/>
        <idType/>
        <issueDate/>
        <expireDate/>
        <dob/>
        <state/>
        <county/>
        <country/>
    </govId>
</customer>
Run Code Online (Sandbox Code Playgroud)

输出应如下所示:

<customer>  
    <govId>
       <id>@</id>
       <idType>SSN</idType>        
    </govId>        
</customer>
Run Code Online (Sandbox Code Playgroud)

我需要删除所有空元素.你会注意到我的代码在"govId"子元素中删除了空的东西,但是在第二个元素中没有取出任何东西.我目前正在使用lxml.objectify.

这基本上就是我在做什么:

root = objectify.fromstring(xml)
for customer in root.customers.iterchildren():
    for e in customer.govId.iterchildren():
        if not e.text:
            customer.govId.remove(e)
Run Code Online (Sandbox Code Playgroud)

有没有人知道用lxml objectify做这个的方法还是有一个更简单的方法期?如果它的所有元素都是空的,我还想完整地删除第二个"govId"元素.

python xml lxml

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

我不知道Python中的[[ - - 1]是什么

我一直看到这个:s[::-1]在Python中,我不知道它做了什么.很抱歉,如果这是一个问题,但我是python和一般编程的新手.

python

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

如何使用带有Python的计时器一次读取键盘输入一个字符

我试图弄清楚如何创建一个可以采用以下参数的小脚本:

  1. 提示 - 字符串
  2. 等待整数的时间
  3. 停止前的字符数

最后一个是在程序停止接受字符并开始处理输入之前我可以输入的字符数.我见过有些人使用Python的select.select 方法,但这并没有考虑到第3项.我倾向于诅咒,虽然我不知道它是否支持超时让我想到线程.任何见解都会很棒!这将在Linux上使用Python 2.6运行.

python linux input

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

标签 统计

python ×4

input ×1

linux ×1

lxml ×1

xml ×1