Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Managing Multiple Timers
00:00
Currently, our Timer class can only really handle one timer at a time. Wouldn’t it be great if we could actually manage multiple timers and have them all in various different states and also give them unique names?
00:12
That way we can distinguish them from one another. Well, that’s what we’ll be looking at next. Here I’ve created a file called multitimer.py, and it’s going to look very similar to our first Python file about the Timer class.
00:25
And I’ll just note the differences. At the very start, we’re still importing the time module. We still have the TimerError class here.
00:31
And now we have our Timer class with some differences. At the very beginning, we will note a timers dictionary. And you’ll notice that this is not at the instance level. This is actually at the class level.
00:44
So this is a class variable that the instances will all have access to. And we’ll see how that works in a little bit. Next up is our .__init__() method.
00:52
And we have a new parameter called name. And this will allow us to give unique names to our timers. Within our .__init__() method, we have a few different properties being instantiated. But notably, we are storing the name internally here.
01:06
At the very end of our method, we are having a check for name if it exists. We are going to look into the timers dictionary. And we’re calling a method called .setdefault().
01:17
This is a dictionary method. And it allows you to create a key-value pair if there isn’t one already existing. If that name value doesn’t already exist as a key, then we can create one.
01:29
And the key is going to be the name. And 0 will be the starting sort of default value. The .start() method has no change.
01:37
And lastly, the .stop() method has basically everything being the same as before. Except at the very bottom, where we’re checking to see if there is a name, then we will go into the timers dictionary and use the name as the key.
01:51
And we are going to add the elapsed time to that, essentially making the timers dictionary like an accumulator of elapsed time. So we can call our timer multiple times, and that will collect all the different elapsed times in that dictionary.
02:06 And of course, we’re going to return the elapsed time at the end. So that’s also the same. All right, let’s see this class in action. So here we are in the terminal.
02:17
Once again, I always like to check my files. And here we have a bunch of files. We’ll be focusing on the multitimer. So let’s get into Python.
02:25
And here I’m going to import from multitimer, name of the file. I want to import that Timer class. And I’m going to create one timer, which I’m going to call t1.
02:38
And I’m going to say you are a Timer. And you don’t have to, but I want to give it a name. Let’s call it "test1". I’m very original with my names here.
02:47
Press Enter, and let’s see t1. I want to look at this dictionary here. And we see that we’ve instantiated it and started our timers dictionary with 0.
02:57 And now let’s start our timer.
03:01
I guess we can wait a moment and then say t1.stop(). And we can see that we get the same output like we were expecting from the previous iteration.
03:11
And we can now check our t1.timers dictionary. And we can see that the elapsed time has been stored internally. I do want to show you as well that the Timer class has the timers dictionary on it. And it’s the same thing.
03:25
And if you actually check for equivalence with t1.timers, we’ll see that they are actually the same object reference here. And I want to also show you that if we start our timer yet again, and wait a moment, and then do t1.stop(), I can’t spell the word stop.
03:43
There we go. Then we can see some elapsed time there. But if we check the timers, we should see that it has accumulated and not started again.
03:52
And we see about 5 seconds plus 7 comes out to about 12 seconds. And of course, we can always start a new timer. So I’ll say t2 = Timer And let’s give that a very original name.
04:04
"test2", I know it’s so original. And here we have t2.start(). And then I can say t2.stop(). And sure enough, if I go to the Timer.timers, we can see everything is in working order.
04:20
All right, we’re now in a really good spot to take our Timer class and push it to the next level. We’re going to look into data classes.
Become a Member to join the conversation.