Python 如何子类化pyCLI';s cli.app.CommandLineApp?
对于CommandLineApp的子类化非常模糊,只提到了一个示例:Python 如何子类化pyCLI';s cli.app.CommandLineApp?,python,Python,对于CommandLineApp的子类化非常模糊,只提到了一个示例: class YourApp(cli.app.CommandLineApp): def main(self): do_stuff() 根据我找到的信息,我拼凑了以下代码: #!/usr/bin/env python import os import sys from cli.app import CommandLineApp # Append the parent folder to the pyt
class YourApp(cli.app.CommandLineApp):
def main(self):
do_stuff()
根据我找到的信息,我拼凑了以下代码:
#!/usr/bin/env python
import os
import sys
from cli.app import CommandLineApp
# Append the parent folder to the python path
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import tabulardata
from addrtools import extract_address
class SplitAddressApp(CommandLineApp):
def main(self):
"""
Split an address from one column to separate columns.
"""
table = tabulardata.from_file(self.params.file)
def for_each_row(i, item):
addr = extract_address(item['Address'])
print '%-3d %-75s %s' % (i, item['Address'], repr(addr))
table.each(for_each_row)
def setup(self):
self.add_param('file', metavar='FILE', help='The data file.')
self.add_param(
'cols', metavar='ADDRESS_COLUMN', nargs='+',
help='The name of the address column. If multiple names are ' + \
'passed, each column will be checked for an address in order'
)
if __name__ == '__main__':
SplitAddressApp().run()
这对我来说似乎是正确的。文档中没有给出在使用子类化时如何处理设置
方法或运行应用程序的示例。我得到一个错误:
Traceback (most recent call last):
File "bin/split_address_column", line 36, in
SplitAddressApp().run()
File "/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address_cleaner/lib/python2.7/site-packages/cli/app.py", line 440, in __init__
Application.__init__(self, main, **kwargs)
File "/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address_cleaner/lib/python2.7/site-packages/cli/app.py", line 129, in __init__
self.setup()
File "bin/split_address_column", line 28, in setup
self.add_param('file', metavar='FILE', help='The data file.')
File "/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address_cleaner/lib/python2.7/site-packages/cli/app.py", line 385, in add_param
action = self.argparser.add_argument(*args, **kwargs)
AttributeError: 'SplitAddressApp' object has no attribute 'argparser'
回溯(最近一次呼叫最后一次):
文件“bin/split\u address\u column”,第36行,在
SplitAddressApp().run()
文件“/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address\u cleaner/lib/python2.7/site packages/cli/app.py”,第440行,在u init中__
应用程序.\uuuu初始化(self、main、**kwargs)
文件“/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address\u cleaner/lib/python2.7/site packages/cli/app.py”,第129行,在__
self.setup()
文件“bin/split\u address\u column”,第28行,在设置中
添加参数('file',metavar='file',help='数据文件!')
文件“/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address\u cleaner/lib/python2.7/site packages/cli/app.py”,第385行,在add\u param中
action=self.argparser.add_参数(*args,**kwargs)
AttributeError:“SplitAddressApp”对象没有属性“argparser”
所以我大概做错了什么,但是什么呢?我发现了。阅读pyCLI的源代码后发现,
setup
函数对于整个库的功能非常重要,而我认为它只是一个可以放置设置代码的函数argparser
是在cli.app.CommandLineApp.setup
中创建的,这意味着我至少需要调用
cli.app.CommandLineApp.setup(self)
内部的设置功能,它甚至工作。现在代码工作得很好 很好的解决方案。不过,为了让它更具python风格,我建议:
super(SplitAddressApp,self).setup()
位于def setup(self):
函数的顶部