0

I have been tinkering with Invoke, but I came across something that is quite an odd case that there seems to be no real PEP guideline for it.

Invoke lets you define your own CLI arguments for whatever tasks you define, and you can optionally provide "help notes" to a task decorator. Specific example can be found here.

If there are more parameters, I could probably do it like this, but it feels kind of weird if there are many tasks. What coding style would you guys do?

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))

UPDATED

As requested, this is what 'too long' would probably look like.

@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))
Wannabe Coder
  • 1,457
  • 12
  • 21
  • Sorry, can you spell out how it would look when it is 'too long'? And the decorator expression is just like any other call, wrap it onto multiple lines within the `(...)`. – Martijn Pieters Sep 21 '16 at 15:21
  • Decorators with arguments are just function calls with an `@` prefix — so just follow the PEP8 guidelines for function calls. – martineau Sep 21 '16 at 16:20

1 Answers1

2

You'd just break up the decorator expression into many lines the same way you'd break up any other function call. One example could be:

@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))

... A codebase that I work on daily has quite a few @mock.patch.object written out this way.

Obviously, breaking out the dictionary into a separate variable can work in this case as you've done in the question (I might actually prefer it assuming that the variable is well named :-).

# 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))
mgilson
  • 300,191
  • 65
  • 633
  • 696