Infinite timer in Python | Hpw to build an infinite timer using Python – News Couple
ANALYTICS

Infinite timer in Python | Hpw to build an infinite timer using Python


This article was published as part of the Data Science Blogathon

summary

  1. What is this “infinite timer in Python”?
  2. What are its uses?
  3. how to make it?

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 regular intervals such as the so-called infinite timer.

What are the uses of Infinite Timer?

Suppose someone is very forgetful about breaks or tasks. Then this program can be used to remind him to act in a regular period of time.

Note – This program must be run continuously as a reminder at regular intervals as many of the built-in programs can be used for one-time reminders.

How to create an infinite timer in Python?

Now comes the real part. Here we are going to use some Python libraries to make it.

So the libraries used are:

  1. plyer unit – for notification purpose
  2. Speech Recognition – For Voice Command
  3. pyttsx3 (python text-to-speech) module – for user interaction
  4. time unit – to remember
  5. Regex or reconfiguration unit

Now we will install the plyer module, speechRecognition, and pyttsx3. The syntax for this –

pip install plyer speechRecognition pyttsx3

NOTE: THE TIME AND RE MODULE COME INTEGRATED WITH PYTHON, NO NEED TO INSTALL IT!

So the topic is that we are going to make a program where we are going to give commands using our voice like talking to Siri or Alexa and it will set our reminder and it will notify the user.

So let’s start programming

First of all, import notification from the plyer module

from plyer import notification

Then import the remaining libraries-

import time
import pyttsx3
import re

We’ll import speech_recognition as sr (so we can call it easy)

import speech_recognition as sr

We will now activate the speech recognition function from the speech recognition module

listener = sr.Recognizer()

Now activate the init() function of pyttsx3 to initialize the pyttsx3 engine and then set its properties as shown 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 uses pyttsx3 to talk about something. So we can say something to the user like “Hi how can I help you” and a number of times.

def talk(text):
    engine.say(text)
    engine.runAndWait()

In the above function, we used the say() function which will basically talk about the content, then the runAndWait() function will stop the engine.

Now we are going to create another function Receiver_command. Here with speech_recognition we will take 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 did is, first we put there a try-except statement which will help when the recognizer can’t catch up with the commands. Then we used the microphone function from the ‘sr’ library as a source to listen for the commands. Then to make sure we are successfully listening to the user we will print the listen. Using the listen function of the ‘sr’ program, we will get the source and then using the Recogn_google() function, the command will be decoded and returned to the program in the command variable. We will now use python’s built-in function less() to make the command in lowercase letters of the alphabet match any word if we want. Then using except syntax we exclude function which if there is any problem trying then except the execute and speak() function will be called and it will say “decode command error” and after that the function will return the command variable

Now we will make another function of the reminder and it will be our main function of this program. His motive will be to interact with the user and use the above functions to calm 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 the last function reminder(), we greeted the user and then asked the user for the reminder, and then listened to them using the Receiver_command() function. Now we will use the re module to find the timer given in the command. So we use the findall() function for re and here “([0-5][0-9]“is the syntax to find the exact minutes after which the program needs to be reminded. Then it asked the user if he wanted to repeat it at regular intervals or just once. Using the Receiver_command function, listen to the user and return the command in the command3 variable, again ensuring the user “OK” that the program listened to it successfully then using the logic that if the user says ‘yes’ then the iteration will be true or the default value is true then use the ‘if statement’ if the user says ‘no’ then the iteration will be ‘A’ and it will replace the iteration. Now, there is only one character in the iterator variable so in a while loop “A” will be of one character, it will only run once and if the user says “yes” then the iterator will be true and it will always run until the user didn’t kill the program. Now in the loop we used a function The notification is for notifications which we have imported before from plyer library.Then it requires several things like title, message, app icon etc.

So we will go through it one by one-

Title – It takes a string. Which is the address of the notification

  • Message – string and notification message
  • app_name – string and name of the app that triggers this notification
  • app_icon – String and path of .ico file which is the path of the icon to be displayed with the message
  • timeout – integer and time to display the message, defaults to 10
  • Bar – a string and text that is displayed on the status bar when a notification arrives
  • Toast – logical and simple Android message instead of full notification

In the above arguments, most of them are optional like if you want it simple just put the title and message.

Going back to the last function we provided here, title, message, app name, app icon. Then in the last statement we used the time library, after that and according to the user’s order we will multiply it by 60 to convert it to minutes.

Tip: You can change the time to hours or seconds only as per your convenience and you can set the address and message only once and use it each time without changing. You can use receive_command() to change the address or message.

Remember: Speech_recognition will take some time to decode the command or sometimes even through error. So try again and it will not work when user is offline.

if __name__ == '__main__':
    reminder()

Finally we called the ‘__main__’ function so we can run it anywhere on the system and then call the reminder() function

conclusion

You can change the minutes, seconds, or even the contents of the message or address. You can customize the program to your liking.

Now one pro tip:

If you are likely to be reminded at a regular interval and you don’t want to run this program manually, you can run this automatically at startup. Operating steps are given automatically for windows-

First, create a shortcut for this program and copy the file or “ctrl + c” > press windows logo key + R > type “shell: startup” > press Enter > paste the file or press “ctrl + v”

there he is! Now you have your own infinite timer.

If you face any issue with the software, feel free to ask it in the comments section.

Pictures:

photo from photography ron lash from Pixels

About:

I am Atulia Khatri, obsessed with snake. I love learning different programming languages, different libraries, and creating things. Share this with all your friends who you think need this.

The media described in this article is not owned by Analytics Vidhya and is used at the author’s discretion



Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button