超过推荐线长的装饰器的PEP8样式

时间:2016-09-21 15:20:05

标签: python python-decorators pep8 pyinvoke

我一直在修补Invoke,但我遇到了一个奇怪的例子,似乎没有真正的PEP指南。

Invoke允许您为您定义的任何任务定义自己的CLI参数,并且您可以选择提供"帮助说明"到一个task装饰者。可以找到具体示例here

如果有更多参数,我可能会这样做,但如果有很多任务,那会感觉很奇怪。你们会做什么编码风格?

help_for_hi = {
    'name1': 'Name of Person 1',
    'name2': 'Name of Person 2',
    'name3': 'Name of Person 3',
}

@task(help=help_for_hi)
def hi(ctx, name1, name2, name3):
    """Say hi to three people."""
    print("Hi %s, %s, and %s!" % (name1, name2, name3))

更新

根据要求,这是太久了#39;可能看起来像。

@task(help={'name1': 'Name of Person 1', 'name2': 'Name of Person 2', 'name3': 'Name of Person 3'})
def hi(ctx, name1, name2, name3):
    """Say hi to three people."""
    print("Hi %s, %s, and %s!" % (name1, name2, name3))

1 个答案:

答案 0 :(得分:2)

您只需将装饰器表达式拆分为多行,就像分解任何其他函数调用一样。一个例子可能是:

@task(help={
    'name1': 'Name of Person 1',
    'name2': 'Name of Person 2',
    'name3': 'Name of Person 3'})
def hi(ctx, name1, name2, name3):
    """Say hi to three people."""
    print("Hi %s, %s, and %s!" % (name1, name2, name3))

...我每天工作的代码库有很多@mock.patch.object以这种方式写出来。

显然,将字典分解为单独的变量可以在这种情况下工作,就像你在问题中所做的那样(我可能实际上更喜欢它假设变量名称很好: - )。

# Nothing wrong with this as far as I can tell ...
help_for_hi = {
    'name1': 'Name of Person 1',
    'name2': 'Name of Person 2',
    'name3': 'Name of Person 3',
}

@task(help=help_for_hi)
def hi(ctx, name1, name2, name3):
    """Say hi to three people."""
    print("Hi %s, %s, and %s!" % (name1, name2, name3))
相关问题