0

i am trying to code an automated notification system which returns a notification (Michael) based on a certain day and time. i used a class object but when it calls the allocated method, it returns no output. the code gives no error which makes it all confusing. i ran the code 2 minutes earlier and when the current time came, nothing happened. this is my first python project.

#import required modules

import datetime

from datetime import date

import calendar


#Get the variable for time right mow

dt = datetime.datetime.now()

#remove the millisecond

x = dt.strftime("%H:%M:%S")

#define class

class Change:
    def __init__(self):
        # define value for date_str argument in mon method
        self.date_str = x

    def mon(self, date_str):
        if date_str == "10:22:00":
            print('michael')

#class instantiation via object
#set value for today's day name

my_date = date.today()

Today = calendar.day_name[my_date.weekday()]

if Today == 'Thursday':

    if x == "10:22:00":

        #call object when if statement is true
        assembly_end_mental_maths_starts = Change()
        assembly_end_mental_maths_starts.mon("10:22:00")
Michael I.
  • 9
  • 1
  • 4
  • 4
    Looks like either `Today != 'Thursday'` or `x != "10:22:00"` in the last two `if` statements. Given that `x` is the _current_ time, are you sure you're running this code at exactly `10:22:00`? – ForceBru Sep 17 '20 at 10:11
  • oh yes yes.. i ran the code 2 minutes earlier and when the current time came, nothing happened. – Michael I. Sep 17 '20 at 10:21

1 Answers1

0

This seems to me a classic XY problem,

But First Your Question, as @ForceBru said Are you running the program at exactly that time to get it to trigger, and even then its not a good solution if you loop it, since the condition will need to trigger at exactly that second, which might pass between the loop cycles. What You are looking for is schedule.

How do I get a Cron like scheduler in Python? [closed]

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
Anunay
  • 397
  • 2
  • 10
  • i ran the code 2 minutes earlier. how will schedule cope with inconsistencies in time when it rings an alarm after 15 minutes and then 40 minutes and the next alarm 20 minutes after then? the code is for automated school notification factoring in assembly, lessons, break etc. – Michael I. Sep 17 '20 at 10:31
  • What is different with the library is that, instead of checking whether the exact time is 10:00:00 (which might never happen), it treats them as "things to do on/after" 10:00 so if the loop is triggered at 10:00:01, it'll be like "Oh I should have done this at 10:00:00, lets do it now." Also your code doesn't have a loop, so it just ran through once found the time wasn't what it hoped, and never ran again. – Anunay Sep 17 '20 at 10:42
  • okay that was really helpful. how can i add a loop to the code please? – Michael I. Sep 17 '20 at 11:02
  • while 1: print("Check If something is something") time.sleep(1) #We don't want to spam the console with msges – Anunay Sep 17 '20 at 11:08
  • Also It seems like you're not well versed with basic python/programming, I would suggest looking at https://www.w3schools.com/python/python_for_loops.asp – Anunay Sep 17 '20 at 11:10
  • Thank you.. this is actually a snippet from my first code. i am new to python you guessed right. i knew it was going to be a while loop but i wasn't sure of the following condition. (while (what?)) – Michael I. Sep 17 '20 at 11:13