Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating a Starter Timer Class

00:00 I’m now going to use what we’ve been talking about regarding timer functions and abstracting that into a Timer class. This will handle the timer state and make it more customizable and easier for you to use.

00:11 Let’s check it out. Here I’ve created a starter_timer.py file. At the very top, I’m importing the time module. Next up, I’m defining a class called TimerError.

00:23 This is a custom exception that will help us hone in on any errors that might be happening within our Timer class. And last but not least, we have our Timer class.

00:32 The very first method is the .__init__() method. I will note it has two underscores at the beginning and end of the word init. These are known as dunders, so it’s dunder init, and it comes with three parameters.

00:44 The first being the self reference to the instance itself. Next is text, which will be text that we can use to format any logger output.

00:53 And the last parameter is logger, which is set as a default to the print() function. But again, the user can define that as they want.

01:02 It’s customizable in that way. Within our .__init__() method, we are defining properties and instantiating them. And the first is _start_time, which is set to None.

01:12 This will handle the state of our timer. And internally, we’re storing text and the logger function. Next up is the .start() method.

01:20 This comes with just the self reference. To start with, we have a guard clause, which is checking the _start_time property and checking to see if it is not None, meaning that it is a timer that has already started. We don’t want to start it again, so we want to raise an error in that case. But if we get past that, that means that we don’t have a starting timer already done, and we can start one.

01:43 And here we have a call to time.perf_counter(), setting the _start_time to that value.

01:50 And now we have our .stop() method. This, again, has the self parameter. To begin with, we have a guard clause similar to the one from the .start() method.

01:59 This time it’s checking to see if the _start_time is None, meaning that the timer hasn’t started yet, so we can’t stop something that never started, and we will raise an error in that case.

02:09 If we get past that, then we are going to make a call to perf_counter() again and subtract the _start_time, that was when the timer was started, and getting that difference, we have the elapsed time.

02:21 And here we’re going to reset our _start_time to None. And if we have a logger function, we’re going to format the elapsed time and log that.

02:30 Lastly, we’re going to return the elapsed time so that whatever is calling this can use that perhaps in some way. Now let’s see how we can use this Timer class in practice.

02:41 Here I am in the terminal, and let me just check my files. Okay, I have a bunch of files here, but the one that we’re focused on is the starter_timer.

02:50 Let’s get into Python, and let me import from starter_timer, the name of the file, and I’m going to import that Timer class that we’ve defined. And here I’m going to instantiate a Timer instance with t = Timer().

03:06 So now we have an instance of our Timer class here, and I can say t.start(), I’m going to start our timer.

03:14 And if we wait a few moments, we can do .stop() to stop our timer. And here we should see the log of the elapsed time and then the return of that value.

03:26 Sure enough, that’s exactly what we see.

03:29 Now we can take this Timer class and make some modifications to it, make it a little bit more versatile.

Become a Member to join the conversation.