Difference between revisions of "OPS435 Python Assignment 2 A"

From CDOT Wiki
Jump to: navigation, search
(Sample code for the tomorrow() function)
(Program Name and valid command line arguments)
Line 7: Line 7:
 
= Instruction =
 
= Instruction =
 
== Program Name and valid command line arguments ==
 
== Program Name and valid command line arguments ==
Name your python3 program as <code>dbda.py</code>. The program should accept two command line parameters, the first one is the date in "YYYYMMDD" format, and the second one is the number of day from the given date, a position value indicates the number of days after the given date, and a negative value indicates the number of days before the given date. For examples:
+
Name your python3 program as <code>dbda.py</code>. The program should accept two command line parameters, the first one is the date in "YYYYMMDD" format, and the second one is the number of day from the given date, a position value indicates the number of days after the given date, and a negative value indicates the number of days before the given date. There is an option called --step that makes the program print out all dates until the final date. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate messages. For examples:
 
* <b><code>python3 dbda.py 20180101 1</code></b>, and the output should be<br />
 
* <b><code>python3 dbda.py 20180101 1</code></b>, and the output should be<br />
 
     20180102
 
     20180102
Line 14: Line 14:
 
* <b><code>python3 dbda.py 20180101 2</code></b>, and the output should be<br />
 
* <b><code>python3 dbda.py 20180101 2</code></b>, and the output should be<br />
 
     20180103
 
     20180103
 +
* <b><code>python3 dbda.py --step 20180101 3</code></b>, and the output should be<br />
 +
    20180102
 +
    20180103
 +
    20180104
 +
* <b><code>python3 dbda.py 20189901 2</code></b>, and the output should be<br />
 +
    Error: bad month entered
 +
* <b><code>python3 dbda.py 20180199 2</code></b>, and the output should be<br />
 +
    Error: bad day entered
 +
 
If there is too few or too many command line parameters given, display the proper usage.
 
If there is too few or too many command line parameters given, display the proper usage.
 +
 
== Program structure ==
 
== Program structure ==
 
Your program code should all be in a single python file with at least two functions, one called yesterday() and the other called tomorrow():
 
Your program code should all be in a single python file with at least two functions, one called yesterday() and the other called tomorrow():

Revision as of 09:34, 10 July 2018

Overview

When making back up of data files or log files, it is a very common practice to name the backup directories and/or files based on the date the backup was done. In order to restore or locate the directory/file, we often need to find out the backup date from today's date.

The task for this assignment is to write a python program which will take a date in the "YYYYMMDD" format and the number of day before or after the given date as the command line arguments, and output the actual date which is the number of day before or after the given date in the same format.

Instruction

Program Name and valid command line arguments

Name your python3 program as dbda.py. The program should accept two command line parameters, the first one is the date in "YYYYMMDD" format, and the second one is the number of day from the given date, a position value indicates the number of days after the given date, and a negative value indicates the number of days before the given date. There is an option called --step that makes the program print out all dates until the final date. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate messages. For examples:

  • python3 dbda.py 20180101 1, and the output should be
    20180102
  • python3 dbda.py 20180101 -1, and the output should be
    20171231
  • python3 dbda.py 20180101 2, and the output should be
    20180103
  • python3 dbda.py --step 20180101 3, and the output should be
    20180102
    20180103
    20180104
  • python3 dbda.py 20189901 2, and the output should be
    Error: bad month entered
  • python3 dbda.py 20180199 2, and the output should be
    Error: bad day entered

If there is too few or too many command line parameters given, display the proper usage.

Program structure

Your program code should all be in a single python file with at least two functions, one called yesterday() and the other called tomorrow():

  • The yesterday() function will take a date in "YYYYMMDD" format and return the date of the previous day in the same format.
  • The tomorrow() function will take a date in "YYYYMMDD" format and return the date of the next day in the same format. Next paragraph is a sample python code for the tomorrow() function.
     #!/usr/sbin/env python3
     import ...

     def tomorrow(today):
        ....
        return next_day

     def yesterday(today):
        ....
        return previous_day

     ....

     if __name__ == __main__:

     .. processing command line arguments ..
     .. set loop based on the number of days given
     .. call tomorrow() or yesterday() as appropriate
     ...
     output the expected date


Sample code for the tomorrow() function

# Return the date in YYYYMMDD after the given day
# 
def tomorrow(today):
    if len(today) != 8:
       return '00000000'
    else:
       year = int(today[0:4])
       month = int(today[4:6])
       day = int(today[6:])

       lyear = year % 4
       if lyear == 0:
          feb_max = 29 # this is a leap year
       else:
          feb_max = 28 # this is not a leap year

       lyear = year % 100
       if lyear == 0:
          feb_max = 28 # this is not a leap year

       lyear = year % 400
       if lyear == 0:
          feb_max = 29 # this is a leap year

       tmp_day = day + 1 # tomorrow's day

       mon_max = { 1:31, 2:feb_max, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
       if tmp_day > mon_max[month]:
          to_day = tmp_day % mon_max[month] # if tmp_day > this month's max, reset to 1
          tmp_month = month + 1
       else:
          to_day = tmp_day
          tmp_month = month + 0

       if tmp_month > 12:
           to_month = 1
           year = year + 1
       else:
           to_month = tmp_month + 0

       next_date = str(year)+str(to_month).zfill(2)+str(to_day).zfill(2)
     
       return next_date