i got raspberry pi, , i'm building reddit api-based application it, using praw library. i'm executing python files using:
sudo python3 main.py
however, pass arguments file command line (so can run app silently passing argument silent
example), , know can sys.argv[0], sys.argv[1], etc..
.
my problem how while following dry -- don't repeat -- in referring configuration established options.
this portion of code:
def init(): if (len(sys.argv) >= 1): global semisilent global silent arg in sys.argv: if arg == "semisilent": semisilent = true if arg == "silent": silent = true print ("--------[ reddipi v0.1 ]--------\n") if silent: print (" ++running in silent mode++ \n") elif semisilent: print ("++running in semi-silent mode++ \n") else: print (" logging in reddit ") print (" .... ") global r r = oauth.login() if not silent: print (" login successful \n") if not silent , not semisilent: print (" connecting database ") print (" .... ") db.init(tablename) if not silent: print (" connection successful \n") if not silent , not semisilent: global sub_name q_sub = input(" use custom subreddit? \n") if (q_sub[0]=="y"): q_sub = input(" enter custom subreddit: \n") if ((len(q_sub)==0) or (q_sub==" ")): print (" no valid input. using default \"" + sub_name + "\" \n") else: sub_name = q_sub print ("\r using subreddit \"" + sub_name + "\"\n") else: print (" using default \"" + sub_name + "\" \n")
i find myself making code hard read putting if not silent
, such before other pieces of code. i've thought having multiple methods same essential functions code left out if user had put in silent
or semisilent
, lead unnecessary code duplication.
is there / way change behaviour of methods without having make unreadable or rewrite code multiple times?
thanks lot help! - jeroen
define own custom "print" method, checks if variable (silent) set, , skips printing if value set. lines print('value') turn myprint('value'). using function names verbose() , debug() , having 2 log levels. in case, maybe call them "silent()" , "semisilent()" or something.
Comments
Post a Comment