384
edits
Changes
added double 02d to f-string: fix for new version of python
def format_time(t):
"""Return time object (t) as a formatted string"""
return f'%{t.2dhour:%.2d02d}:%.2d' % (t.hour, {t.minute, :02d}:{t.second):02d}'
def sum_times(t1, t2):
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7a
</source>
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7b
</source>
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7c
</source>
# Student ID: [seneca_id]
class Time:
def sec_to_time(seconds):
</source>
: Please notice that the function named sec_to_time() did not get moved under the class block. It remains as an external function.
:1. Create a new python file and name it as '''lab7d.py''', place the code listed above in it, and change '''sum_times()''' and '''change_time()''' functions function from lab7c.py to become object methods method for our Time object(refering to change_time() method above might be helpful).
:2. Save the file, and test the new time object in an interactive Python shell: <source lang="bash">
[rchan@centos7 lab7]$ python3
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7d
</source>
def __str__(self):
'''return a string representation for the object self'''
return f'%{self.2dhour:%.2d02d}:%.2d' % (self.hour, {self.minute, :02d}:{self.second) :02d}'
</source>
:1. Make a copy of lab7d.py and name it as lab7e.py. Add the function definition for __str__() after the __init__() function in lab7e.py. Make sure that the '''def __str__(self):''' line has the same indentation level as the __init__() function.
def __repr__(self):
'''return a string representation for the object self'''
</source>
:6. Add the function definition for __repr__() after the __str__() function in lab7e.py. Please note that we use the '.' instead of ':' in the formatting string. Make sure that the '''def __repr__(self):''' line has the same indentation level as the __init__() function.
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7e
</source>
== Part 3 - Operator overloading ==
: Remember we define the sum_times() function to add to time objects and return their sum? After we moved the function definition under the class definition in lab7d.py, it became a class function, and method for the time object. It can be invoked by using the '''Time.sum_times(t1,t2) ''' syntax or '''t1.sum_times(t2) ''' syntax. However, there is also a way to invoke it by using a special function which ties to the '+' arithmetic operator.
: The '+' operator is bound to the special function of an object's __add__() method. If we attached the same code we have for the sum_times() function to the special function __add__() for the time object, the we can use the '+' operator to tell the python interpreter to perform sum operation on the time object.
: Changing or specifying the behaviour of an operator so that it works with programmer-defined types is called '''operator overloading'''.
::* '''Associate the code to the __add__ method''':<source lang="python">
def __add__(self, t2):
"""return self.the result by using sum_times(t2)method"""
</source>
:1. Copy lab7e.py to a new file called lab7f.py. Add the function definition for __add__() after the __str____repr__() function to lab7f.py. Make sure that the '''def __add__(self, t2):''' line has the same indentation level as the __init__() function.
:2. Save the file lab7f.py and test it in an interactive Python shell:<source lang="bash">
[rchan@centos7 lab7]$ python3
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7f
</source>
cd ~/ops445/lab7/
pwd #confirm that you are in the right directory
ls CheckLab7.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab7-template/labsblob/LabCheckScriptsmaster/CheckLab7.py?raw=true' -O CheckLab7.py
python3 ./CheckLab7.py -f -v lab7i
</source>