This article was published as a part of the Data Science Blogathon
Infinite timer using Python is a program written in Python using its libraries. It serves as a reminder by notifying you of tasks or notifying users at a regular interval like a so-called infinite timer.
Suppose someone is very forgetful about his/her breaks or tasks. Then this program can be used to remind him of the work at a regular time interval.
Now the real part comes in. Here we are going to use some python libraries to make it.
Now we will install the plyer and speechRecognition and pyttsx3 module. The syntax for this –
pip install plyer speechRecognition pyttsx3
NOTE: TIME AND RE MODULE COMES BUILT-IN WITH PYTHON, SO NO NEED TO INSTALL IT!
So the theme is that we are going to make a program in which we will give the commands using our voice like talking to Siri or Alexa and it will set our reminder and will notify the user.
First of all import notification from plyer module
from plyer import notification
Then import remaining libraries-
import time import pyttsx3 import re
We will import speech_recognition as sr(So we can call it easily)
import speech_recognition as sr
Now we will activate the Recognizer function from the speech_recognition module
listener = sr.Recognizer()
Now activate init() function from pyttsx3 to initialize the pyttsx3 engine and then set the properties for it as given here:
engine = pyttsx3.init() voices = engine.getProperty('voices') engine.setProperty('voice', voices[0].id) engine.setProperty("rate", 125)
Now we are going to create a function that will use pyttsx3 to speak out something. So we can say something to user like “Hello How can I help you” and a number of times.
def talk(text): engine.say(text) engine.runAndWait()
In the above function, we have used the say() function which will mainly speak out the content, and then runAndWait() function will stop the engine.
Now we are making another function receive_command. Here using speech_recognition we are going to intake the words from the user.
def receive_command(): try: with sr.Microphone() as source: print('listening...') voice = listener.listen(source) command = listener.recognize_google(voice) command = command.lower() except: talk(“Error in decoding command”) return command
In the above function, what we have done is, firstly we have put there a try-except statement which will help when the recognizer may not be able to catch up with the commands. Then we have used the Microphone function from the “sr” library as source to listen to the commands. Then to ensure that we are successfully listening to the user we will print listening. Using function listen of “sr” program we will get the source then using recognize_google() function the command will be decoded and got back to the program in command variable. Now we will use a python in-built function lower() to make the command in lower case of the alphabet to match if any word if we want. Then using except syntax we have excepted the function that is if there is any problem in try then except will perform and talk() function will be called and it will say “Error in decoding command” and then the function will return the command variable
Now we are going to make another function that is a reminder and it will be our main function of this program. Its motive will be to interact with the user and use the above functions to comfort the user.
def reminder(): talk("hello what's the command") talk("reminder please") command = receive_command() command2 = re.findall("[0-5][0-9]",command) talk("Do you want to repeat it at regular interval. Yes or NO") command3 = receive_command() talk("OK") repeat = True if "no" in command3: repeat = "A" while repeat: notification.notify( title = "Timer", message = "Message for reminder", app_icon = "path to your .ico file", app_name = "Tiimer" ) time.sleep(60*int(command2[0])) #In minutes
In this last function reminder(), we have welcomed the user then asked the user for the reminder, then listened to it using the receive_command() function. Now we will use the re module to find the timer given in the command. So we are using findall() function of re and here ”([0-5][0-9]” is the syntax to find the exact minutes after which the program needs to remind. Then asked the user if he/she wants to repeat it at regular intervals or only 1 time. And using receive_command function, listened to the user and returned the command in command3 variable, again ensuring user “OK” that the program listened to him/her successfully then using a logic that if the user says “yes” then repeat will be True or it defaults true and then using “if statement” if a user says “no”, repeat will be “A” and it will overwrite repeat. Now, there is only one character in repeat variable, so in while loop “A” will be of 1 character then it will run only for 1 time and if the user had said “yes” then repeat will be True and it will run always until the user had not killed the program. Now in the loop, we have used notify function of notification which we had imported before from the plyer library. Then it requires several things like title, message, app_icon, etc.
Title – It takes string. And it is the title of the notification
In the above arguments, most of them are optional like if you only want it simple then just put title and message.
Again going to the last function we have given here title, message, app_name, app_icon. And then in the last statement, we used the time library, and then and as per the order of user we will multiply it by 60 to convert it into minutes.
TIP: YOU CAN CHANGE THE TIMER TO HOURS OR ONLY SECONDS AS YOUR CONVENIENCE AND YOU CAN SET THE TITLE AND MESSAGE ONLY ONE TIME AND USE IT EVERY TIME WITHOUT CHANGING. YOU CAN USE receive_command() TO CHANGE THE TITLE OR MESSAGE.
REMEMBER: Speech_recognition will take some time to decode the command or even sometimes it will through error. So try again and it will not work when the user is offline.
if __name__ == '__main__': reminder()
At last, we have called the ”__main__” function so that we can run it anywhere on the system and then called reminder() function
You change the minutes or seconds or even you change the contents of the message or title. You can customize the program as your wish.
If you are likely to be reminded at a regular interval and you don’t want to run this program manually then you can automatically run this on startup. The steps for running automatically is given for windows-
Voila! Now you have your own so-called infinite timer.
If you face any problem with the program then feel free to ask in the comment section.
Images:
I am Atulya Khatri a python geek. I love to learn different programming languages and different libraries and create stuff. Do share this with all your friends who you think need this.