Changes

Jump to: navigation, search

Rchan sandbox

3,438 bytes added, 18:09, 2 November 2019
no edit summary
</source>
:8. Before proceeding, make certain that you identify all errors in lab7a.py. When the checking script tells you everything is OK - proceed to the next step.
<br><br>
 
== Part 2 - Pure Function and Modifiers ==
In Part 1, the sum_times() function did not make change to any of the two time objects. It is called a pure function. In this part, we are going to create a function which changes the value (the three attributes) of a time object based on the integer value passes to the function. We call such type of function a modifier. The function we are going to add is called change_time(time, seconds), where time is the time object the function is going to modify, seconds is the given number of seconds to be added to the time object.
 
:1. Make a copy of lab7a.py and name it as lab7b.py in the ~/ops435/lab7 directory
:2. Add the following new function called change_time(time, seconds) after the sum_times() function:<source lang="python">
def change_time(time, seconds):
time.second += seconds
if valid_time(time) != True:
while time.second >= 60:
time.second -= 60
time.minute +=1
while time.minute >= 60:
time.minute -= 60
time.hour += 1
return None
</source>
:3. Save the file.
:4. Bring up an interact Python3 shell to test the new function.
::'''Testing change_time(time, seconds) with import'''<source lang="bash">
[rchan@centos7 wiki_labs]$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from lab7b import *
>>> time1 = Time(9,50,0)
>>> format_time(time1)
'09:50:00'
>>> seconds = 1800
>>> seconds
1800
>>> change_time(time1, seconds)
>>> format_time(time1)
'10:20:00'
</source>
:5 If you encounter any syntax error or exception, please fix the error before moving on to the next step.
:6 Now try set seconds to a negative value, i.e. -1800, and then call the change_time() function.
Will it change the time1 object back to '09:50:00'? Let's try it out:<source lang="bash">
>>> format_time(time1)
'10:20:00'
>>> change_time(time1,-1800)
>>> format_time(time1)
'10:20:-1800'
:7. It is not, so we have to modify and update the change_time() function so that the change_time() function can handle positive and negative value correctly. After the function change_time() has been updated, test it again in a Python interactive shell with import<source lang='bash'>
[rchan@centos7 wiki_labs]$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> format_time(time1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'format_time' is not defined
>>> from lab7b import *
>>> time1 = Time(9,50,0)
>>> format_time(time1)
'09:50:00'
>>> seconds = 1800
>>> change_time(time1, seconds)
>>> format_time(time1)
'10:20:00'
>>> seconds = -1800
>>> change_time(time1, seconds)
>>> format_time(time1)
'09:50:00'
</source>
:7. Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab7.py
python3 ./CheckLab7.py -f -v lab7b
</source>
:8. Before proceeding, make certain that you identify all errors in lab7b.py. When the checking script tells you everything is OK - proceed to the next step.
<br><br>
1,760
edits

Navigation menu