Difference between revisions of "OPS435 Python3 Assignment 2P"

From CDOT Wiki
Jump to: navigation, search
(Tests and Test results)
(Programmer-defined object type: Date)
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:OPS435-Python]][[Category:rchan]]
 
[[Category:OPS435-Python]][[Category:rchan]]
 
= Overview =
 
= 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.
+
You have successfully designed an algorithm to compute the date which is '''n''' day before or after a given date. You have also successfully implemented your algorithm using the Python language by identifying the functions that are needed to perform the computation.
  
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 "YYYY/MM/DD" 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.
+
In this assignment, you are going to re-implement the algorithm you have for assignment 1 using python class. You are going to create a new programmer-define object called Date with the appropriate data attributes and function attributes to support the necessary manipulation of '''date''' object needed to implement your algorithm for assignment 1.  
  
== Coding Standard ==
 
 
Your python script must follow the following coding guide:
 
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 ==
 
* Name your python script as a1_[student_id].py, where [student_id] is your Seneca email user name.
 
* Your python script must support two mandatory command line arguments : (1) any valid date in YYYY/MM/DD format, (2) number of days before or after the given date, and one option flag: "--step"
 
* If there are less or more than two data items provided at the command line, your script should display the correct usage message and exit.
 
  
 
= Class Requirements =
 
= Class Requirements =
Line 23: Line 18:
 
** __init__(self): Date object constructor
 
** __init__(self): Date object constructor
 
** __repr__(self): return date object as a string in "yyyy-mm-dd" format
 
** __repr__(self): return date object as a string in "yyyy-mm-dd" format
** __str__(self): return date object as a string in "yyyy:mm:dd" format
+
** __str__(self): return date object as a string in "yyyy/mm/dd" format
 
* Supporting function:
 
* Supporting function:
 
** days_to_time(): convert an integer which is n days from epoch (Jan 1, 1970) to a corresponding date object.
 
** days_to_time(): convert an integer which is n days from epoch (Jan 1, 1970) to a corresponding date object.
Line 29: Line 24:
 
== Required Modules and Functions ==
 
== 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 functions.</font></b>
 
<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 functions.</font></b>
 
Based on the algorithm you have designed for this assignment, you should at least have the following three methods defined in your class file for Date (see later section on the purpose of each function) in order to get a passing grade for this assignment:
 
* tomorrow()
 
* yesterday()
 
* sum_date()
 
* diff_date()
 
 
You can also create additional functions to improved the re-usability of your python code by adding the following functions to earn the maximum possible mark for this assignment:
 
* days_in_mon()
 
* leap_year()
 
* valid_date()
 
* usage()
 
  
 
== Documentation ==
 
== Documentation ==
* Please use python's docstring to document your python script (script level documentation) and each of the functions (function level documentation) you created for this assignment. The docstring should describe 'what' the function does, not 'how' it does.
+
* Please use python's docstring to document your new python class, class functions and external functions. The docstring should describe 'what' the class is for, what does each class function do, what each data attribute is for.
* The following shows the docstring that was added to the after() function which provides the following information when called with help(after) in the python interactive shell:
 
 
 
  
 
== Authorship Declaration ==
 
== 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 part of the docstring</u></b> in your Python source code file (replace "Student Name" with your own name):
+
Your Python code for the Date class and its associated functions 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 part of the docstring</u></b> in your Python source code file (replace "Student Name" with your own name):
 
<source>OPS435 Assignment 2P - Fall 2019
 
<source>OPS435 Assignment 2P - Fall 2019
 
Program: a1_[student_id].py (replace student_id with your Seneca User name)
 
Program: a1_[student_id].py (replace student_id with your Seneca User name)
Line 62: Line 43:
  
 
== Tests and Test results ==
 
== Tests and Test results ==
You must name your class definition python script for Date as <code>a2_class.py</code>. The following test scripts are for testing your class definition. The test scripts can be used to test the date objects created by using your Date class.  
+
You must name your class definition python script for Date as <code>a2_class.py</code>. The following tests in an interactive python sessions are for testing your class definition. The assignment test script called "checkA2P.py" should be used once your Date class passes all the interactive tests.  
  
 
Please review those tests that failed and try to fix it in your class definition to address any bugs you may have.
 
Please review those tests that failed and try to fix it in your class definition to address any bugs you may have.
 
 
=== Test for tomorrow and yesterday methods ===
 
=== Test for tomorrow and yesterday methods ===
 
: Start up an interactive Python session and issue the following python statments:<source lang="python">
 
: Start up an interactive Python session and issue the following python statments:<source lang="python">
Line 98: Line 78:
 
2019-01-01
 
2019-01-01
 
</source>
 
</source>
 
 
 
=== Test for operator overloading '+' and '-' ===
 
=== Test for operator overloading '+' and '-' ===
 +
: <source lang="python">
 +
>>> d1
 +
2019-11-07
  
=== Test for day of the week method ===
+
>>> d2
 +
2019-11-14
  
== Script structure and sample template ==
+
>>> d1 - d2
Your code should all be in a single python file with at least the functions mentioned above: dbda(), after(), and before(). To earn the maximum mark, you should also create additional functions into your algorithm, e.g.: leap_year(), days_in_mon, valid_date(), usage(), etc
+
-7
  
The following is a brief description of each function:
+
>>> d2 - d1
 +
7
  
* The dbda() function should be the main function of your script. The dbda() function will take a date in "YYYY/MM/DD" 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. Your dbda() function should delegate the actual calculation of the target date to either the after() function or the before() function.
+
>>> d2 = Date(2019,2,28)
* The before() function will take a date in "YYYY/MM/DD" format and return the date of the previous day in the same format.
+
>>> d2
* The after() function will take a date in "YYYY/MM/DD" format and return the date of the next day in the same format. Next paragraph is a sample python code for the after() function. To earn the maximum possible mark for the assignment, you should modify the sample after() function to make use of the days_in_mon() function.
+
2019-02-28
* The leap_year() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
 
* The valid_date() function will take a date in "YYYY/MM/DD" format, and return True if the given date is a valid date, otherwise return False plus an appropriate status message. The valid_date() function should make use of the days_in_mon() function.
 
* The days_in_mon() function will take a year in "YYYY" format, and return a dictionary object which contains the total number of days in each month for the given year. The days_in_mon() function should make use of the leap_year() function.
 
* The usage() function will take no argument and return a string describing the usage of the script.
 
<pre>
 
    #!/usr/sbin/env python3
 
    ''' docstring
 
    '''
 
    import ...
 
  
    def after(today):
+
>>> d2 + 1
        ....
+
2019-03-01
        return next_day
 
  
    def before(today):
+
>>> d2 + 2
        ....
+
2019-03-02
        return previous_day
 
  
    ....
+
>>> d3
 +
2016-02-28
  
    def dbda(date,days):
+
>>> d3 + 1
        ...
+
2016-02-29
        setup loop:
 
          call after() or before() as appropriate
 
        return target_day
 
  
    if __name__ == "__main__":
+
>>> (d3 + 1) - 1
 +
2016-02-28
  
    .. processing command line arguments ..
+
>>> d3 - 1
    .. call dbda()
+
2016-02-27
    ...
 
    .. output the result
 
  
</pre>    
+
>>> d4
+
2018-12-31
  
=== Sample code for the after() function ===
+
>>> d4 + 365
<pre>
+
2019-12-31
# Return the date in YYYY/MM/DD after the given day
 
#
 
def after(today):
 
    if len(today) != 10:
 
      return '0000/00/00'
 
    else:
 
      str_year, str_month, str_day = today.split('/')
 
      year = int(str_year)
 
      month = int(str_month)
 
      day = int(str_day)
 
  
      lyear = year % 4
+
>>> d4 - 365
      if lyear == 0:
+
2017-12-31
          feb_max = 29 # this is a leap year
+
</source>
      else:
 
          feb_max = 28 # this is not a leap year
 
  
      lyear = year % 100
+
=== Test for day of the week method ===
      if lyear == 0:
+
: The day of week on Jan 1, 1970 is Thursday. The date.day_of_week() method should return the day of week for the give date in numeric form.
          feb_max = 28 # this is not a leap year
+
:: 0 - Sun,  
 
+
:: 1 - Mon,  
      lyear = year % 400
+
:: 2 - Tue,  
      if lyear == 0:
+
:: 3 - Wed,  
          feb_max = 29 # this is a leap year
+
:: 4 - Thu,  
 
+
:: 5 - Friday, and
      tmp_day = day + 1 # next day
+
:: 6 - Saturday <source lang="python">
 
+
>>> d1 = Date(2019,11,7)
      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}
+
>>> d1
      if tmp_day > mon_max[month]:
+
2019-11-07
          to_day = tmp_day % mon_max[month] # if tmp_day > this month's max, reset to 1
+
>>> d1.day_of_week()
          tmp_month = month + 1
+
4
      else:
+
>>> d2 = d1 + 7
          to_day = tmp_day
+
>>> d2
          tmp_month = month + 0
+
2019-11-14
 
+
>>> d2.day_of_week()
      if tmp_month > 12:
+
4
          to_month = 1
+
>>>
          year = year + 1
+
</source>
      else:
 
          to_month = tmp_month + 0
 
 
 
      next_date = str(year)+"/"+str(to_month).zfill(2)+"/"+str(to_day).zfill(2)
 
   
 
      return next_date
 
</pre>
 
 
 
= Normal task =
 
Your script must be able to take two dates both in "YYYY/MM/DD" format and output the number of days between the given two dates.
 
e.g.
 
* Calculate the number of days between "2018/03/01" and "2019/03/01"
 
  python3 a1_rchan.py 2018/03/01 2019/03/01
 
  365
 
* Calculate the number of days between "2019/03/01" and "2018/03/01"
 
  python3 a1_rchan.py 2019/03/01 2018/03/01
 
  365
 
  
 
= Deliverable =
 
= Deliverable =
 
== Create a private repository on github.com under your account ==
 
== Create a private repository on github.com under your account ==
* name the repository as 'ops435-a1'
+
* name the repository as 'ops435-a2p
* invite 'rayfreeping' as one of the collaborator to your 'ops435-a1' repository
+
* invite 'rayfreeping' as one of the collaborator to your 'ops435-a2p' repository
 
* use this repository for developing the and keeping track of the following text/source code files:
 
* use this repository for developing the and keeping track of the following text/source code files:
** the algorithm for assignment 1 named "a1_algorithm.txt"
+
** The readme.md file to show your progress. Add entry whenever you update any files in this repository.
** the python script for assignment 1 named "a1_[seneca-id].py"
+
** the python class definition named "a2_class.py"
** the test results produce by the assignment checking script "checkA1.py". Name it as a1_results.txt
+
** the test results produce by the assignment checking script "checkA2P.py". Name it as a2p_results.txt
  
 
= Rubric =
 
= Rubric =
Line 222: Line 162:
 
| Program Authorship Declaration ||5 ||
 
| Program Authorship Declaration ||5 ||
 
|-
 
|-
| Program usage || 5 ||
+
| class block || 5 ||
|-
 
| Program Options --step || 5 ||
 
 
|-
 
|-
| after() function || 5 ||
+
| __init__() method || 5 ||
 
|-
 
|-
| before() function || 15 ||
+
| __str__() method || 5 ||
 
|-
 
|-
| dbda() function || 10 ||
+
| __repr() method || 5 ||
 
|-
 
|-
| script level docstring || 5 ||
+
| '+' operator || 10 ||
 
|-
 
|-
| leap_year() function || 5 ||
+
| '-' operator || 10 ||
 
|-
 
|-
| valid_date() function || 5 ||
+
| tomorrow() method || 10 ||
 
|-
 
|-
| days_in_mon() function || 5 ||
+
| yesterday() method || 10 ||
 
|-
 
|-
| usage() function || 5 ||
+
| day_of_week() function || 5||
 
|-
 
|-
| Algorithm ||15||
+
| docstring ||15||
 
|-
 
|-
 
| github.com repository||15||
 
| github.com repository||15||
Line 255: Line 193:
  
 
Please submit the following files to blackboard by the due date:
 
Please submit the following files to blackboard by the due date:
* your algorithm (step-by-step instruction for solving the computation problem for this assignment in the English language), name the file as 'a1_algorithm.txt'
+
* your python script, name the file as 'a2_class.py'
* your python script, name the file as 'a1_[seneca-id].py'
+
* the output of the checking script checkA2P.py, name the file as 'a2p_results.txt'
* the output of the checking script checkA1.py, name the file as 'a1_results.txt'
+
* the 'git log' output for your own repository 'ops435-a2p' on github.com, name the file as 'a2p_gitlog.txt'
* the 'git log' output for your own repository 'ops435-a1' on github.com, name the file as 'a1_gitlog.txt'
 

Latest revision as of 19:02, 11 November 2019

Overview

You have successfully designed an algorithm to compute the date which is n day before or after a given date. You have also successfully implemented your algorithm using the Python language by identifying the functions that are needed to perform the computation.

In this assignment, you are going to re-implement the algorithm you have for assignment 1 using python class. You are going to create a new programmer-define object called Date with the appropriate data attributes and function attributes to support the necessary manipulation of date object needed to implement your algorithm for assignment 1.

Your python script must follow the following coding guide:

Class Requirements

Programmer-defined object type: Date

  • Data Attributes: year, month, and day as integer
  • Method attributes: tomorrow(), yesterday(), day_of_week()
  • Operator Overloading:
    • '+': perform addition for two Date() object, and one date object and an integer.
    • '-': perfome substraction for two Date() object, and one date object and an integer
  • Special methods:
    • __init__(self): Date object constructor
    • __repr__(self): return date object as a string in "yyyy-mm-dd" format
    • __str__(self): return date object as a string in "yyyy/mm/dd" format
  • Supporting function:
    • days_to_time(): convert an integer which is n days from epoch (Jan 1, 1970) to a corresponding date object.

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 functions.

Documentation

  • Please use python's docstring to document your new python class, class functions and external functions. The docstring should describe 'what' the class is for, what does each class function do, what each data attribute is for.

Authorship Declaration

Your Python code for the Date class and its associated functions must be placed in a single source python file. Please include the following declaration as part of the docstring in your Python source code file (replace "Student Name" with your own name):

OPS435 Assignment 2P - Fall 2019
Program: a1_[student_id].py (replace student_id with your Seneca User name)
Author: "Student Name"
The python code in this file (a1_[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 class definition python script for Date as a2_class.py. The following tests in an interactive python sessions are for testing your class definition. The assignment test script called "checkA2P.py" should be used once your Date class passes all the interactive tests.

Please review those tests that failed and try to fix it in your class definition to address any bugs you may have.

Test for tomorrow and yesterday methods

Start up an interactive Python session and issue the following python statments:
[raymond.chan@mtrx-node04pd a2p]$ python3
Python 3.6.8 (default, May  2 2019, 20:40:44) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from a2_class import *
>>> dir()
['Date', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'daysb4month', 'int_to_date']
>>> d1 = Date(2019,11,6)
>>> d1.tomorrow()
2019-11-07
>>> d1.yesterday()
2019-11-05
>>> print(d1.tomorrow())
2019/11/07
>>> print(d1.yesterday())
2019/11/05
>>> d2 = Date(2019,2,28)
>>> d2.tomorrow()
2019-03-01
>>> d2.yesterday()
2019-02-27
>>> d3 = Date(2016,2,28)
>>> d3.tomorrow()
2016-02-29
>>> d3.yesterday()
2016-02-27
>>> d4 = Date(2018,12,31)
>>> d4.tomorrow()
2019-01-01

Test for operator overloading '+' and '-'

>>> d1
2019-11-07

>>> d2
2019-11-14

>>> d1 - d2
-7

>>> d2 - d1
7

>>> d2 = Date(2019,2,28)
>>> d2
2019-02-28

>>> d2 + 1
2019-03-01

>>> d2 + 2
2019-03-02

>>> d3
2016-02-28

>>> d3 + 1
2016-02-29

>>> (d3 + 1) - 1
2016-02-28

>>> d3 - 1
2016-02-27

>>> d4
2018-12-31

>>> d4 + 365
2019-12-31

>>> d4 - 365
2017-12-31

Test for day of the week method

The day of week on Jan 1, 1970 is Thursday. The date.day_of_week() method should return the day of week for the give date in numeric form.
0 - Sun,
1 - Mon,
2 - Tue,
3 - Wed,
4 - Thu,
5 - Friday, and
6 - Saturday
>>> d1 = Date(2019,11,7)
>>> d1
2019-11-07
>>> d1.day_of_week()
4
>>> d2 = d1 + 7
>>> d2
2019-11-14
>>> d2.day_of_week()
4
>>>

Deliverable

Create a private repository on github.com under your account

  • name the repository as 'ops435-a2p
  • invite 'rayfreeping' as one of the collaborator to your 'ops435-a2p' repository
  • use this repository for developing the and keeping track of the following text/source code files:
    • The readme.md file to show your progress. Add entry whenever you update any files in this repository.
    • the python class definition named "a2_class.py"
    • the test results produce by the assignment checking script "checkA2P.py". Name it as a2p_results.txt

Rubric

Task Maximum mark Actual mark
Program Authorship Declaration 5
class block 5
__init__() method 5
__str__() method 5
__repr() method 5
'+' operator 10
'-' operator 10
tomorrow() method 10
yesterday() method 10
day_of_week() function 5
docstring 15
github.com repository 15
Total 100

Due Date and Final Submission requirement

Check with your professor for the due date for your section.

Please submit the following files to blackboard by the due date:

  • your python script, name the file as 'a2_class.py'
  • the output of the checking script checkA2P.py, name the file as 'a2p_results.txt'
  • the 'git log' output for your own repository 'ops435-a2p' on github.com, name the file as 'a2p_gitlog.txt'