Python Forum
Help adding a loop inside a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help adding a loop inside a loop
#15
First off, I just want to say thanks a lot for your time & patience in helping me to understand this, I really appreciate it.

So I got it to run, but there are still a couple of things I'm not sure about.

(1) Why do all the reminders popup when they haven't reached their scheduled/set time yet?
Ex: If you look at the output, the current time was 11:23 AM but all the reminders popped up even though they were set at 11:25, 11:28, & 11:30.
Is there a way to get it so nothing would have popped up until 11:25 (Then only the "Test 1" reminder should pop up because it's scheduled for 11:25, not the rest of them)?

(2) How do I get the AM/PM to display? I know the "%p" is supposed to do it in: "%Y-%m-%d %I:%M %p" but it doesn't show.

Thanks again.

My Output from (Test.py -> The code shown below):
Output:
Reminders in Database: ('Test 1', '2022-10-15 11:25:00') Reminders in Database: ('Test 2', '2022-10-15 11:28:00') Reminders in Database: ('Test 3', '2022-10-15 11:30:00') Scheduled Time: ('2022-10-15 11:25:00',) Scheduled Time: ('2022-10-15 11:28:00',) Scheduled Time: ('2022-10-15 11:30:00',) Current Date Time: 2022-10-15 11:23:00 Reminders Popup: ('2022-10-15 11:25:00',): Test 1 Reminders Popup: ('2022-10-15 11:28:00',): Test 2 Reminders Popup: ('2022-10-15 11:30:00',): Test 3
The code I have now (Test.py -> Not link to my main code yet):
import sched
import time
import sqlite3
from BX_External_Functions import checkForReminders, reminderPopup
from BX_Constants import MainDatabase, currentdate, currentTime
  
#Create schedule
scheduler = sched.scheduler(time.time, time.sleep)

#Read in reminders from database
#For each reminder, add an event to the schedule (enter/enterabs)
connection = sqlite3.connect(MainDatabase)
for row in connection.execute("SELECT Reminder, DateTime FROM Reminders"):
    print("Reminders in Database: ", row)
connection.close()

print("\n")

connection = sqlite3.connect(MainDatabase)
for row in connection.execute("SELECT DateTime FROM Reminders"):
    scheduled_time = row
    print("Scheduled Time: ",  scheduled_time)
connection.close()

import datetime
currentdate = datetime.date.today()
from datetime import datetime
timeNow = datetime.now()
currentTime = timeNow.strftime("%I:%M %p")

Current_DateTime = datetime.strptime(f"{currentdate} {currentTime}", "%Y-%m-%d %I:%M %p")
print("\nCurrent Date Time: ", Current_DateTime ,"\n")

# #If a new reminder is created, add it to the database & add an event to the schedule
# scheduler.enterabs()

# #Inside Baxter's while True loop, call schedule.run(blocking=False)
# print ('Checking for reminders...')
# scheduler.run(False)

def schedule_reminder(reminder):
    """Schedule a reminder"""
    scheduler.enterabs(
        time=Current_DateTime.timestamp(),
        priority=1,  
        action=send_reminder,
        argument=(reminder, scheduled_time))

def send_reminder(Reminder, scheduled_time):
    """Function called when time to send reminder"""
    print(f"Reminders Popup: {scheduled_time}: {Reminder}")


#Read in reminders from database
connection = sqlite3.connect(MainDatabase)
for reminder in connection.execute("SELECT Reminder FROM Reminders"):
    schedule_reminder(*reminder)
connection.close()

# somewhere later in your main code
while True:
    scheduler.run(False)
    # do other stuff
And this is how my Reminders Table/Database is set up:
def createBXDatabase():
        #Create a database (BX_Database.db)
        connection = sqlite3.connect("BX_Database.db")
        cursor = connection.cursor()
        #--------------------------------------------
        #               Reminders Table
        #--------------------------------------------
        remindersTable = """CREATE TABLE IF NOT EXISTS Reminders
                (ID INTEGER PRIMARY KEY  AUTOINCREMENT,
                Reminder            TEXT,
                DateTime            TEXT,
                DateAdded           datetime default current_timestamp);"""

        #Execute the creation of the table
        cursor.execute(remindersTable)
        #Commit the changes
        connection.commit()
        #Close the connection
        connection.close()
Reply


Messages In This Thread
Help adding a loop inside a loop - by Extra - Oct-09-2022, 09:37 PM
RE: Help adding a loop inside a loop - by Extra - Oct-09-2022, 10:41 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 02:36 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 03:01 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 05:07 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 06:38 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 07:31 PM
RE: Help adding a loop inside a loop - by Extra - Oct-15-2022, 03:52 PM
RE: Help adding a loop inside a loop - by Extra - Oct-15-2022, 07:10 PM
RE: Help adding a loop inside a loop - by Extra - Oct-15-2022, 11:12 PM
RE: Help adding a loop inside a loop - by Extra - Oct-16-2022, 12:17 AM
RE: Help adding a loop inside a loop - by Extra - Oct-16-2022, 06:10 PM
RE: Help adding a loop inside a loop - by Extra - Oct-17-2022, 10:41 PM
RE: Help adding a loop inside a loop - by Extra - Oct-17-2022, 11:23 PM
RE: Help adding a loop inside a loop - by Extra - Oct-19-2022, 01:01 AM
RE: Help adding a loop inside a loop - by Extra - Oct-23-2022, 12:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 2,117 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 1,576 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 671 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 719 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  Error loop with Chatgpt sportak12 0 492 Jan-14-2025, 12:04 PM
Last Post: sportak12
  How to convert while loop to for loop in my code? tatahuft 4 805 Dec-21-2024, 07:59 AM
Last Post: snippsat
  How to get keep information in a loop ginod 4 874 Dec-11-2024, 01:32 AM
Last Post: deanhystad
  Regarding The For Loop Hudjefa 5 1,447 Nov-15-2024, 01:02 PM
Last Post: deanhystad
  For Loop beginner agoldav 2 712 Nov-05-2024, 12:51 AM
Last Post: agoldav
  A question about 'Event loop is closed' fc5igm 3 5,121 Oct-01-2024, 09:12 AM
Last Post: skdaro

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020