0

I'm writing a tool using Python 2 and click which reads/writes registers in hardware. I have two functions that accept exactly the same options. The difference is that they process input and direct output to a different devices.

Here is what I have so far:

@cli.command()
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def mydevice1(r0, r1, r2):
    # Handle inputs for device 1
    click.echo('myfunc1')

@cli.command()   
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def mydevice2(r0, r1, r2):
    # Handle inputs for device 2
    click.echo('myfunc2')

Both of the functions will handle inputs in the same way, the only difference is that they will pass handled info to different devices. In other words what I'd like to have is

@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def handle_common_options(r0, r1, r2):
    # Handle common options
    pass

@cli.command()
def mydevice1():
    handle_common_options()
    # pass processed options to device 1

@cli.command()
def mydevice2():
    handle_common_options()
    # pass processed options to device 2

Is this possible?

1 Answer 1

2

sure.

@decorator
def f():
    pass

means

def f():
    pass
f = decorator(f)

so:

decorator0 = cli.command()
decorator1 = click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
decorator2 = click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
decorator3 = click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)

common_decorator = lambda f: decorator0(decorator1(decorator2(decorator3(f))))

@common_decorator
def mydevice1(r0, r1, r2):
    click.echo('myfunc1')

@common_decorator
def mydevice2(r0, r1, r2):
    click.echo('myfunc2')

without a lambda:

def common_decorator(f):
    return decorator0(decorator1(decorator2(decorator3(f))))
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to do it without a lambda?
Would you mind showing me how? I've been at it for several hours and still can't figure out it.
the proposed answer was edited too. def f(x): return g(x) is equivalent to f = lambda x: g(x). Also please accept the proposed answer if you consider it good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.