find_sds
find_sds copied to clipboard
use with argparse
Hello! This issue is concerning your comments on using argparse. I haven't run your code, but here are a few comments.
I see you using global debug
a lot. This should be necessary. The variable debug
as declared should already be "global" (ie readable inside of the functions). Since you are only reading it, I believe you do not need debug. This should be true when the file is run as a script whether or not it is in __main__
. However, if the definition is in __main__
and you run with pytest (or run functions from an import), debug
will not be defined, so tests will fail.
You might try putting everything with argparse
in __main__
and adding a default argument (debug=False
) to all of your function definitions which can be overridden with the value from argparse
when the script is run. This way, debug is defined automatically to be False. When you use it, you will call it with the appropriate values (in __main__
, this would be debug=args.debug
).
Hi @janash ,
You are absolutely right on the global debug
. I told myself to remove that and I keep forgetting.
I thought of your suggestion of adding default argument of debug=False
but hesitated because it would required some more rewriting. However, it might be my last option for what I want the code to do.
Thank you very much for your time and comments!!
@janash : One note: I did want to have a global debug
here in find_sds()
function because I was hoping that maybe by passing a debug
value of True
into this function via argparse
, I can modify the global debug
.
find_sds()
function basically calls download_sds()
in the try
clause, which in turn calls all others functions. However, after a bunch of testing, even though global debug == True
at find_sds()
(by passing argparse in '__main__'
or by sys.argv
), all other functions still read global debug
as False. This is a weird behavior that I am not sure why but at this point, I assume it has something to do with the map
and/or partial
functions I used. Perhaps, map()
and partial()
create some closure and trap debug
as False from the global variable.
https://github.com/khoivan88/find_sds/blob/179178815ee1a2095b4967a43886a7fc9eb77543/find_sds/find_sds.py#L33-L122
Anyway, all of this probably comes from my poor planning for this code. It has evolved over time with more functions added.
Thank you for your time again!