Is it ‘allowed’ to get parameters like this
Cecil Westerhof
Cecil at decebal.nl
Thu Aug 11 09:23:40 EDT 2016
It has been a while since I worked with Python. I wanted to get some
stats about the idle time of my computer, so that was a good moment to
pick up Python again. ;-)
As I understood it getopt is the way to get the parameters for your
script. But at the moment it looks a bit like overkill, my script does
not have much parameters as parameter_error shows:
def parameter_error():
print('Error: {0} [ --5 | --10 | --20 | --25 ] --all | --this-month | --this-year | --today'
.format(basename(sys.argv[0])))
sys.exit(1)
So at the moment this is my init function:
def init():
global conn
global cursor
global vmstat_params
arg_length = len(sys.argv)
if (arg_length == 1):
period = '--today'
slice_length = 20
elif (arg_length == 2):
period = sys.argv[1]
slice_length = 20
elif (arg_length == 3):
period = sys.argv[2]
if (sys.argv[1] in ['--5', '--10', '--20', '--25']):
slice_length = int(sys.argv[1][2:])
else:
parameter_error()
else:
parameter_error()
conn = connect(expanduser('~/Databases/general.sqlite'))
cursor = conn.cursor()
all_data = '%'
today = cursor.execute('SELECT CURRENT_DATE').fetchone()[0]
this_month = today[0:8] + '%'
this_year = today[0:5] + '%'
if (period == '--today'):
vmstat_params = [today, slice_length]
elif (period == '--this-month'):
vmstat_params = [this_month, slice_length]
elif (period == '--this-year'):
vmstat_params = [this_year, slice_length]
elif (period == '--all'):
vmstat_params = [all_data, slice_length]
else:
parameter_error()
Is this acceptable, or is it a big no-no?
By the way: the reason I fetch today from the database is that I work
with UTC dates.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
More information about the Python-list
mailing list