使用命令行实用程序执行所需的操作没有错。如果您在服务周围包装好包装,则可以根据需要实现内部结构!例如:
class Whois(object):
_whois_by_query_cache = {}
def __init__(self, query):
"""Initializes the instance variables to defaults. See :meth:`lookup`
for details on how to submit the query."""
self.query = query
self.domain = None
# ... other fields.
def lookup(self):
"""Submits the `whois` query and stores results internally."""
# ... implementation
现在,无论您是否使用urllib进行滚动,环绕命令行实用程序(如您所做的事情)还是导入第三方库并使用它,此接口都保持不变。
通常根本不认为这种方法很丑陋- 。如果速度最终成为瓶颈,那么您的抽象将使切换到本地Python实现的过程对您的客户端代码透明。
实用性胜过纯洁-这就是Pythonic。:)