Difference between revisions of "OPS435 Python Assignment 1 2018 Fall"

From CDOT Wiki
Jump to: navigation, search
(Program structure)
(Documentation)
 
(24 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
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.
 
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 with appropriate functions 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.
+
The computational task for this assignment is to design an algorithm and write a python script according to your algorithm with appropriate functions. The script should take a date in the "YYYYMMDD" format and the number of days before or after the given date as the command line arguments, calculate and output to the standard output data channel the targeted date which is the number of day before or after the given date in the same format.
  
= Coding Standard =
+
== Coding Standard ==
 +
Your python script must follow the following coding guide:
 
* [https://www.python.org/dev/peps/pep-0008/ PEP-8 -- Style Guide for writing Python Code]
 
* [https://www.python.org/dev/peps/pep-0008/ PEP-8 -- Style Guide for writing Python Code]
 
== Command Line Argument to be supported ==
 
== Command Line Argument to be supported ==
== Required Functions ==
+
* Name your python script as a1_[student_id].py, where [student_id] is your Seneca email user name.
You must have at least the following functions defined in your python program:
+
* Your python script must support two command line arguments: (1) any valid date in YYYYMMDD format, (2) number of days before or after the given date.
 +
* If there are less or more than two command line arguments given on the command line, your script should display the correct usage message and exit.
 +
 
 +
= Computation Requirements =
 +
== Algorithm Design ==
 +
* Write a step-by-step instructions in English on how to figure out a date which is n days before or after a given date.
 +
* While you are working on the step-by-step instructions, note that there are different number of days in each month and some years have 365 days and some years have 366 days.
 +
* You should also do some research to find out when we started using the Calendar in the current form. (This will pose a limit on the validity of your algorithm.)
 +
 
 +
== Required Modules and Functions ==
 +
<b><font color='blue'>Your python script is allowed to import only the <u>os and sys</u> modules from the standard library and all the built-in modules.</font></b>
 +
 
 +
Based on your algorithm designed for this assignment, you should at least have the following three functions defined in your python script (see later section on the purpose of each function):
 +
* dbda()
 +
* tomorrow()
 +
* yesterday()
 +
You can also create additional functions to improved the re-usability of your python code by adding the following functions:
 
* leapyear()
 
* leapyear()
* main()
 
* tomorrow()
 
 
* validdate()
 
* validdate()
* yesterday()
+
* usage()
  
 
== Documentation ==
 
== Documentation ==
* Please use python's docstring to document the functions you created for this assignment.
+
* Please use python's docstring to document your python script and each of the functions you created for this assignment. The docstring should describle 'what' the function does, not 'how' it does.
* Please add docstring to the tomorrow() function to provide the following information when call with help(tomorrow) in the python interactive shell:
+
* The following shows the docstring that was added to the tomorrow() function which provides the following information when called with help(tomorrow) in the python interactive shell:
 
<pre>
 
<pre>
 
Help on function tomorrow in module rchan:
 
Help on function tomorrow in module rchan:
Line 31: Line 46:
 
</pre>
 
</pre>
  
= Due Date =
+
== Authorship Declaration ==
 
+
All your Python code for this assignment must be placed in a <font color='red'><b><u>single source python file</u></b></font>. Please include the following declaration <b><u>as the docstring</u></b> in your Python source code file (replace "Student Name" with your own name):
This Assignment is due on Sunday October 14, 2018. Submit on blackboard under the assignments section.
 
 
 
All your Python code for this program must be placed in a <font color='red'><b><u>single source file</u></b></font>. Please include the following declaration by <b><u>you</u></b> as a comment in your Python source code file (replace "Student Name" with your own name):
 
 
<source>OPS435 Assignment 1 - Fall 2018
 
<source>OPS435 Assignment 1 - Fall 2018
 
Program: [student_id].py (replace student_id with your Seneca User name)
 
Program: [student_id].py (replace student_id with your Seneca User name)
Line 41: Line 53:
 
The python code in this file ([Student_id].py) is original work written by
 
The python code in this file ([Student_id].py) is original work written by
 
"Student Name". No code in this file is copied from any other source  
 
"Student Name". No code in this file is copied from any other source  
except those provided by the course instructor, including any person, textbook,  
+
except those provided by the course instructor, including any person,  
or on-line resource. I have not shared this python program with anyone or  
+
textbook, or on-line resource. I have not shared this python script
anything except for submission for grading.   
+
with anyone or anything except for submission for grading.   
I understand that the Academic Honesty Policy will be enforced and violators  
+
I understand that the Academic Honesty Policy will be enforced and  
will be reported and appropriate action will be taken.
+
violators will be reported and appropriate action will be taken.
 
</source>
 
</source>
  
= Instruction =
+
== Tests and Test results ==
== Program Name and valid command line arguments ==
+
You must name your python 3 script as <code>a1_[Student_id].py</code>. The following examples assumes that the student_id is rchan.The script should accept two command line arguments, the first one is the date in "YYYYMMDD" format, and the second one is the number of day from the given date, a positive 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 target date. If the "YYYYMMDD" format is broken give an appropriate error message. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate error messages. For examples:
Name your python3 program as <code>[Student_id].py</code>. The following examples assumes that the student_id is rchan.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 positive 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. If the "YYYYMMDD" format is broken give an appropriate error message. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate error messages. For examples:
+
* <b><code>python3 a1_rchan.py 20180101 1</code></b>, and the output should be<br />
* <b><code>python3 rchan.py 20180101 1</code></b>, and the output should be<br />
 
 
     20180102
 
     20180102
* <b><code>python3 rchan.py 20180101 -1</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py 20180101 -1</code></b>, and the output should be<br />
 
     20171231
 
     20171231
* <b><code>python3 rchan.py 20180101 2</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py 20180101 2</code></b>, and the output should be<br />
 
     20180103
 
     20180103
* <b><code>python3 rchan.py --step 20180101 3</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py --step 20180101 3</code></b>, and the output should be<br />
 
     20180102
 
     20180102
 
     20180103
 
     20180103
 
     20180104
 
     20180104
* <b><code>python3 rchan.py 20180701 500</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py 20180701 500</code></b>, and the output should be<br />
 
     20191113
 
     20191113
* <b><code>python3 rchan.py 20189901 2</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py 20189901 2</code></b>, and the output should be<br />
 
     Error: wrong month entered
 
     Error: wrong month entered
* <b><code>python3 rchan.py 20180199 2</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py 20180199 2</code></b>, and the output should be<br />
 
     Error: wrong day entered
 
     Error: wrong day entered
* <b><code>python3 rchan.py 2018 2</code></b>, and the output should be<br />
+
* <b><code>python3 a1_rchan.py 2018 2</code></b>, and the output should be<br />
 
     Error: wrong date entered
 
     Error: wrong date 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 arguments given, display the proper usage.
  
== Program structure ==
+
== Script structure and sample template ==
Your program code should all be in a single python file with at least the functions mentioned above: leapyear(), main(), tomorrow(), validdate(), yesterday():
+
Your code should all be in a single python file with at least the functions mentioned above: dbda(), tomorrow(), and yesterday(). You can also add additional functions based on your algorithm, e.g.: leapyear(), validdate(), usage(), etc
  
 +
* The dbda() function will take a date in "YYYYMMDD" format, a positive or negative integer, and return a date either before or after the given date according to the value of the given integer in the same format.
 +
* 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.
 
* The leapyear() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
 
* The leapyear() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
* The main() function will take a date in "YYYYMMDD" format, a positive or negative integer, and return a date either before or after the given date according to the value of the given integer in the same format.
 
 
* The validdate() function will take a date in "YYYYMMDD" format, and return True if the given date is a valid date, otherwise return False.
 
* The validdate() function will take a date in "YYYYMMDD" format, and return True if the given date is a valid date, otherwise return False.
* The yesterday() function will take a date in "YYYYMMDD" format and return the date of the previous day in the same format.
+
* The usage() function will take no argument and return a string describing the usage of the script.
 
 
* 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.
 
 
 
 
<pre>
 
<pre>
 
     #!/usr/sbin/env python3
 
     #!/usr/sbin/env python3
Line 96: Line 106:
 
     ....
 
     ....
  
     if __name__ == __main__:
+
    def dbda(yyyymmdd,days):
 +
        ...
 +
        setup loop
 +
          call tomorrow or yesterday as appropriate
 +
        return target_day
 +
 
 +
     if __name__ == "__main__":
  
 
     .. processing command line arguments ..
 
     .. processing command line arguments ..
    .. set loop based on the number of days given
+
     .. call dbda()  
     .. call tomorrow() or yesterday() as appropriate
 
 
     ...
 
     ...
 
     output the expected date
 
     output the expected date
Line 153: Line 168:
 
       return next_date
 
       return next_date
 
</pre>
 
</pre>
 +
 +
= Rubric =
 +
 +
{| class="wikitable" border="1"
 +
! Task !!  Maximum mark !! Actual mark
 +
|-
 +
| Program Authorship Declaration ||5 ||
 +
|-
 +
| Program usage || 5 ||
 +
|-
 +
| Program Options --step || 5 ||
 +
|-
 +
| tomorrow() function || 5 ||
 +
|-
 +
| yesterday() function || 15 ||
 +
|-
 +
| dbda() function || 10 ||
 +
|-
 +
| script level docsting || 5 ||
 +
|-
 +
| leapyear() function || 15 ||
 +
|-
 +
| validdate() function || 15 ||
 +
|-
 +
| usage() function || 5 ||
 +
|-
 +
| Algorithm ||15||
 +
|-
 +
|'''Total''' || 100 ||
 +
 +
|}
 +
 +
= Assignment Due Date and submission procedure =
 +
 +
This Assignment is due on Sunday October 14, 2018 before mid-night. Please submit your algorithm (step-by-step instruction for solving the computation problem for this assignment in the English language), your python script, required test and test results to blackboard under the assignments section. Please refer to the detail submission instruction given on Blackboard.

Latest revision as of 11:21, 18 December 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 computational task for this assignment is to design an algorithm and write a python script according to your algorithm with appropriate functions. The script should take a date in the "YYYYMMDD" format and the number of days before or after the given date as the command line arguments, calculate and output to the standard output data channel the targeted date which is the number of day before or after the given date in the same format.

Coding Standard

Your python script must follow the following coding guide:

Command Line Argument to be supported

  • Name your python script as a1_[student_id].py, where [student_id] is your Seneca email user name.
  • Your python script must support two command line arguments: (1) any valid date in YYYYMMDD format, (2) number of days before or after the given date.
  • If there are less or more than two command line arguments given on the command line, your script should display the correct usage message and exit.

Computation Requirements

Algorithm Design

  • Write a step-by-step instructions in English on how to figure out a date which is n days before or after a given date.
  • While you are working on the step-by-step instructions, note that there are different number of days in each month and some years have 365 days and some years have 366 days.
  • You should also do some research to find out when we started using the Calendar in the current form. (This will pose a limit on the validity of your algorithm.)

Required Modules and Functions

Your python script is allowed to import only the os and sys modules from the standard library and all the built-in modules.

Based on your algorithm designed for this assignment, you should at least have the following three functions defined in your python script (see later section on the purpose of each function):

  • dbda()
  • tomorrow()
  • yesterday()

You can also create additional functions to improved the re-usability of your python code by adding the following functions:

  • leapyear()
  • validdate()
  • usage()

Documentation

  • Please use python's docstring to document your python script and each of the functions you created for this assignment. The docstring should describle 'what' the function does, not 'how' it does.
  • The following shows the docstring that was added to the tomorrow() function which provides the following information when called with help(tomorrow) in the python interactive shell:
Help on function tomorrow in module rchan:

tomorrow(today) -> str
    tomorrow() takes a valid date string in 'YYYYMMDD' format and return a 
    date string for the next day in 'YYYYMMDD' format.
    e.g. tomorrow('20171231') -> '20180101'
         tomorrow('20180131') -> '20180201'
         tomorrow('20180228') -> '20180301'
(END)

Authorship Declaration

All your Python code for this assignment must be placed in a single source python file. Please include the following declaration as the docstring in your Python source code file (replace "Student Name" with your own name):

OPS435 Assignment 1 - Fall 2018
Program: [student_id].py (replace student_id with your Seneca User name)
Author: "Student Name"
The python code in this file ([Student_id].py) is original work written by
"Student Name". No code in this file is copied from any other source 
except those provided by the course instructor, including any person, 
textbook, or on-line resource. I have not shared this python script 
with anyone or anything except for submission for grading.  
I understand that the Academic Honesty Policy will be enforced and 
violators will be reported and appropriate action will be taken.

Tests and Test results

You must name your python 3 script as a1_[Student_id].py. The following examples assumes that the student_id is rchan.The script should accept two command line arguments, the first one is the date in "YYYYMMDD" format, and the second one is the number of day from the given date, a positive 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 target date. If the "YYYYMMDD" format is broken give an appropriate error message. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate error messages. For examples:

  • python3 a1_rchan.py 20180101 1, and the output should be
    20180102
  • python3 a1_rchan.py 20180101 -1, and the output should be
    20171231
  • python3 a1_rchan.py 20180101 2, and the output should be
    20180103
  • python3 a1_rchan.py --step 20180101 3, and the output should be
    20180102
    20180103
    20180104
  • python3 a1_rchan.py 20180701 500, and the output should be
    20191113
  • python3 a1_rchan.py 20189901 2, and the output should be
    Error: wrong month entered
  • python3 a1_rchan.py 20180199 2, and the output should be
    Error: wrong day entered
  • python3 a1_rchan.py 2018 2, and the output should be
    Error: wrong date entered

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

Script structure and sample template

Your code should all be in a single python file with at least the functions mentioned above: dbda(), tomorrow(), and yesterday(). You can also add additional functions based on your algorithm, e.g.: leapyear(), validdate(), usage(), etc

  • The dbda() function will take a date in "YYYYMMDD" format, a positive or negative integer, and return a date either before or after the given date according to the value of the given integer in the same format.
  • 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.
  • The leapyear() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
  • The validdate() function will take a date in "YYYYMMDD" format, and return True if the given date is a valid date, otherwise return False.
  • The usage() function will take no argument and return a string describing the usage of the script.
     #!/usr/sbin/env python3
     import ...

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

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

     ....

     def dbda(yyyymmdd,days):
        ...
        setup loop
           call tomorrow or yesterday as appropriate
        return target_day

     if __name__ == "__main__":

     .. processing command line arguments ..
     .. call dbda() 
     ...
     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

Rubric

Task Maximum mark Actual mark
Program Authorship Declaration 5
Program usage 5
Program Options --step 5
tomorrow() function 5
yesterday() function 15
dbda() function 10
script level docsting 5
leapyear() function 15
validdate() function 15
usage() function 5
Algorithm 15
Total 100

Assignment Due Date and submission procedure

This Assignment is due on Sunday October 14, 2018 before mid-night. Please submit your algorithm (step-by-step instruction for solving the computation problem for this assignment in the English language), your python script, required test and test results to blackboard under the assignments section. Please refer to the detail submission instruction given on Blackboard.