Scheduling a simple notification script in Python to run every hour - python

I'm totally new to Python.
I just wrote this simple script that prints a message on my desktop:
from plyer import notification
notification.notify(
title="HI MATTEO!",
message="Here's the notification to be sent every hour!",
timeout=10
)
Here's the screenshot of the notification
I'd like to know if there's a way to run this notification every hour (or at a given time)
Thanks so much
Matteo Fossati

You should look at the schedule package (https://pypi.org/project/schedule/).
Then, run something like:
schedule.every().hour.do(my_notification)
...
while True:
schedule.run_pending()
time.sleep(1)

Related

2nd Function doesnt work until 1st function is completely executed in python

So basically, i have made a tkinter app that has a reminder utiliy in specific to generate notifications at the scheduled time. Everything works fine until I run the app module and another module having the notification generating function one at a time , but when I call the notification generating function intto the app module, my app doesnt work but the notification works. I want the app to run such that the notification generating function kind of runs in the background until the app module is open.
github link: https://github.com/click-boom/Trella
Looking into chatgpt i found terms like threading and multiprocessing, but i have no concept of that and still tried but didnt work.
Sure enough what you are looking for is multithreading.
Here is a simple example of how multithreading works (sorry for my lack of drawing skills).
This is how all monothread programs work. In most programming languages this is the default behaviour.
So in this example Second Task will have to wait for First Task to complete.
If you want several tasks to run concurrently, you can use multithreading.
This is how you could implement this in Python.
Monothreading:
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
firstTask()
secondTask()
Here, secondTask will only run after firstTask is done (i.e after 10 seconds).
Multithreading:
from threading import Thread
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
first_thread = Thread(target=firstTask)
second_thread = Thread(target=secondTask)
first_thread.start()
second_thread.start()
I hope this will be a help to someone !

Automate push notifier on Desktop-Python

I created a script with python that extracts a KPI from a database and compares it with another KPI, then I created a push notification that sent me a message on the desktop if the KPI of the database is out of its tolerance. My question is how can I automate this push notifier in my desktop? I mean every time the KPI changes I want to receive automatically a message from the notifier on the Desktop? Do you have any idea, please? Thank you :)
If you want to do that locally. I can think of 2 options.
You need to create another function/script that detects if there is a change in the database. Then
You have that script running in the background constantly. You can make it check for changes every minute or so. If there is a change, then it calls the script that you made. You can also make it being executed every time you turn on your PC
Similar to before but with the use of a windows scheduler. That schedules can run the script every minute or so.

Loop reddit bot to check for answers every 10 minutes

I'm making a Reddit bot that goes through comments on certain subreddits and replies to those with certain keyphrases.
I originally did not have a loop, and it worked fine, but I had to click run again every few minutes. I am running my python script on pythonanywhere.com, using PRAW.
import praw
import time
SECONDS_PER_MIN = 60
subreddit = reddit.subreddit('memes+dankmemes+comics+funny+pics')
keyphrase = ('Sauce+Sauce?')
def main():
while True:
for comment in subreddit.stream.comments():
if keyphrase in comment.body:
comment.reply('[Here.](https://www.youtube.com/watch?v=dQw4w9WgXcQ)\n\nI am a bot and this action was performed automatically. Learn more at [https://saucebot.com/](https://www.youtube.com/watch?v=dQw4w9WgXcQ)')
print('Posted!')
time.sleep(SECONDS_PER_MIN * 11)
if __name__ == '__main__':
main()
I expect it to respond to a random person who says "sauce" every 10 minutes, but now it won't respond to anyone.
Are you running your script on a PC? You could potentially use the task scheduler for that without using python at all. Just save your script as a binary using pyinstaller, then schedule it to run every ten minutes.

Alarm in pyTelegram Bot

I want to write a simple reminder-bot for telegram. Bot gets from user time (Hours:Minutes) and saves it. When system time equals to users remind time, bot sends message to user.
This is how i track current time:
import time
def timer():
now = time.strftime('%X').split(':')[0:2]
return now
The question is:
How can I make my code wait till time to send message comes without using time.sleep() and checking current time each minute (uses too much memory of raspberrypi)?
If you use python-telegram-bot library, you can use the JobQueue to set up timed jobs.
See here for an example.

Schedule Python Script - Windows 7

I have a python script which I would like to run at regular intervals. I am running windows 7. What is the best way to accomplish this? Easiest way?
You can do it in the command line as follows:
schtasks /Create /SC HOURLY /TN PythonTask /TR "PATH_TO_PYTHON_EXE PATH_TO_PYTHON_SCRIPT"
That will create an hourly task called 'PythonTask'. You can replace HOURLY with DAILY, WEEKLY etc.
PATH_TO_PYTHON_EXE will be something like: C:\python25\python.exe. Check out more examples by writing this in the command line:
schtasks /?
Otherwise you can open the Task Scheduler and do it through the GUI.
Hope this helps.
You can use the GUI from the control panel (called "scheduled tasks") to add a task, most of it should be self-explanatory, but there are two things to watch out for:
Make sure you fill in C:\python27\python.exe as the program path, and the path to your script as the argument.
If you choose Run whether user is logged on or not I get an error: The directory name is invalid (0x87010B). Choosing Run only when user is logged on "solves" this issue.
This took me quite a bit to figure out ...
A simple way to do this is to have a continuously running script with a delay loop. For example:
def doit():
print "doing useful things here"
if __name__ == "__main__":
while True:
doit()
time.sleep(3600) # 3600 seconds = 1 hour
Then leave this script running, and it will do its job once per hour.
Note that this is just one approach to the problem; using an OS-provided service like the Task Scheduler is another way that avoids having to leave your script running all the time.

Categories