currentInitial code
ideasIdeas how to handle args.assume
- replace
interactive.confirm
with another function dynamicallyreplace
interactive.confirm
with another function dynamically# insert this into cli.py if args.assume is not None: import interactive interactive.confirm = lambda _, __=None: args.assume
I do not like this because it changes a function on the fly. I like it because I can forget the
args.assume
thing in the rest of my application. It does not show up in the unit tests ofinteractive.confirm
. add an extra argument to all functions that call
interactive.confirm
and to the function itself:# in interactive.py def confirm(message: str, assume: bool|None, accept_enter_key: bool = True) -> bool: if assume is not None: return assume ... # in main.py def work(foo, assume): if interactive.confirm(f"do you want {foo}", assume): ... if __name__ == "__main__": args = cli.parse() work(args.foo, args.assume)
I don't like this because I have to change the signature of many functions in my program and the new argument has to be tested in the unittest of interactive.confirm.
save the value of
args.assume
to a global variable that is read byinteractive.confirm
:# insert this into cli.py if args.assume is not None: import interactive interactive.assume = args.assume # and in interactive.py def confirm(message: str, accept_enter_key: bool = True) -> bool: if assume is not None: return assume ...
This (nearly) has the advantage of (1) because it does not touch any other code but the command line parser and
confirm
. But it uses a global mutable variable to toggle program behaviour which I thought was bad style.
# insert this into cli.py
if args.assume is not None:
import interactive
interactive.confirm = lambda _, __=None: args.assume
I do not like this because it changes a function on the fly. I like it because I can forget the args.assume
thing in the rest of my application. It does not show up in the unit tests of interactive.confirm
.
- add an extra argument to all functions that call
interactive.confirm
and to the function itself:
# in interactive.py
def confirm(message: str, assume: bool|None, accept_enter_key: bool = True) -> bool:
if assume is not None:
return assume
...
# in main.py
def work(foo, assume):
if interactive.confirm(f"do you want {foo}", assume):
...
if __name__ == "__main__":
args = cli.parse()
work(args.foo, args.assume)
I don't like this because I have to change the signature of many functions in my program and the new argument has to be tested in the unittest of interactive.confirm.
- save the value of
args.assume
to a global variable that is read byinteractive.confirm
:
# insert this into cli.py
if args.assume is not None:
import interactive
interactive.assume = args.assume
# and in interactive.py
def confirm(message: str, accept_enter_key: bool = True) -> bool:
if assume is not None:
return assume
...
This (nearly) has the advantage of (1) because it does not touch any other code but the command line parser and confirm
. But it uses a global mutable variable to toggle program behaviour which I thought was bad style.