10.4 Time Class with Properties for Data Access

  • Properties can control the manner in which they get and modify an object’s data—assuming programmers follow conventions
  • For robust date and time manipulation capabilities, see Python's datetime module

10.4.1 Test-Driving Class Time

Before we look at class Time’s definition, let’s demonstrate its capabilities

In [1]:
from timewithproperties import Time

Creating a Time Object

  • Create a Time object
  • Class Time’s __init__ method has hour, minute and second parameters, each with a default argument value of 0
In [2]:
wake_up = Time(hour=6, minute=30)

Displaying a Time Object

  • Class Time defines two methods that produce string representations of Time object
  • When you evaluate a variable in IPython, it calls the object’s __repr__ special method to produce a string representation of the object
In [3]:
wake_up
Out[3]:
Time(hour=6, minute=30, second=0)
  • The __str__ special method is called when an object is converted to a string, such as when you output the object with print
In [4]:
print(wake_up)
6:30:00 AM

Getting an Attribute Via a Property

  • Class Time provides hour, minute and second properties
    • Provide the convenience of data attributes for getting and modifying an object’s data
    • Implemented as methods, so they may contain additional logic
In [5]:
wake_up.hour
Out[5]:
6
  • Appears to simply get an hour data attribute’s value
  • A ctually a call to an hour method that returns the value of an _hour data attribute

Setting the Time

  • Time method set_time method provides hour, minute and second parameters, each with a default of 0
In [6]:
wake_up.set_time(hour=7, minute=45)
In [7]:
wake_up
Out[7]:
Time(hour=7, minute=45, second=0)

Setting an Attribute via a Property

  • Class Time also supports setting the hour, minute and second values individually via its properties
In [8]:
wake_up.hour = 6
In [9]:
wake_up
Out[9]:
Time(hour=6, minute=45, second=0)
  • Appears to simply assign a value to a data attribute
  • Actually a call to an hour method that takes 6 as an argument, validates the value, then assigns it to a corresponding data attribute named _hour

Attempting to Set an Invalid Value

To prove that class Time’s properties validate the values you assign to them, let’s try to assign an invalid value to the hour property, which results in a ValueError:

In [10]:
wake_up.hour = 100
------------------------------------------------------------------------
ValueError                             Traceback (most recent call last)
<ipython-input-10-1fce0716ef14> in <module>
----> 1 wake_up.hour = 100

~/Dropbox/books/2019/Python/PyCDS_JupyterSlides/ch10/timewithproperties.py in hour(self, hour)
     20         """Set the hour."""
     21         if not (0 <= hour < 24):
---> 22             raise ValueError(f'Hour ({hour}) must be 0-23')
     23 
     24         self._hour = hour

ValueError: Hour (100) must be 0-23

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.