My problem is as follows. There is some function in my library:
def some_func(arg1=1):
pass
which I want to use in three ways:
Using import statements in other python scripts:
import my_library
my_library.some_func()
Expose using a CLI interface with click.
@click.group()
@click.option('--arg1', default=1)
def some_func(arg1):
pass
Expose using a web interface with Flask.
@app.route('/endpoint', defaults={'arg1': 1})
def some_func(arg1):
pass
But how can I structure this efficiently without duplicating too much code?
Is it possible to merge all three? I tried (variations of) the following, which fails:
@click.group()
@click.option('--arg1', default=1)
@app.route('/endpoint', defaults={'arg1': 1})
def some_func(arg1=1):
pass
Or do I really need the 3 different functions as defined above?
And if so, how should I go about the default values?
Is accessing a global variable at all places the best way to do this?