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
#22
Thanks for your help. I finally got it to work properly!

I scheduled a couple test reminders and they all popped up at their scheduled time.
Output:
#New Reminder I scheduled for 1:00 PM 01:00 PM 2022-10-16 13:00:00 Reminders Popup: 2022-10-16 12:46:00: Test 4 Reminders Popup: 2022-10-16 12:46:00: T Reminders Popup: 2022-10-16 12:52:00: Test 4 Reminders Popup: 2022-10-16 12:52:00: Test 5 Reminders Popup: 2022-10-16 12:54:00: Test 6 Reminders Popup: 2022-10-16 12:54:00: Test 7 Reminders Popup: 2022-10-16 12:59:00: Test C Reminders Popup: 2022-10-16 13:00:00: Test B Reminders Popup: 2022-10-16 13:00:00: Test no Reminders Popup: 2022-10-16 13:00:00: Test 8 Reminders Popup: 2022-10-16 13:00:00: Test 3 Reminders Popup: 2022-10-16 13:00:00: Test A Reminders Popup: 2022-10-16 13:00:00: Test 1 Reminders Popup: 2022-10-16 13:00:00: Test D Reminders Popup: 2022-10-16 13:00:00: Test 9
Now all that's left to do is delete the old reminders, clean up the code, and test it/tie it in to my main code.

This is my code (I used npyscreen as a GUI to set my reminders):
# Working Test Version

import npyscreen
import sqlite3
from BX_Constants import (MainDatabase)
import sched
import time
from datetime import datetime

#Create schedule
scheduler = sched.scheduler(time.time, time.sleep)

class SetReminderTUI(npyscreen.NPSApp):
    def main(self):
        frame  = npyscreen.Form(name = "Set Reminder",)
        Reminder  = frame.add(npyscreen.TitleText, name = "Reminder:",)
        date = frame.add(npyscreen.TitleDateCombo, name = "Date:")
        AmPm  = frame.add(npyscreen.TitleText, name = "AM/PM:",)
        time = frame.add(npyscreen.TitleSelectOne, max_height=5, value = [1,], name="Choose a Time",
                values = ["12:00","12:30","12:59","01:00","01:30"
                        ,"02:00","02:30","03:00","03:30","04:00","04:30","05:00"
                        ,"05:30","06:00","06:30","07:00","07:30"
                        ,"08:00","08:30", "09:00","09:30","10:00","10:30"
                        ,"11:00","11:30"], scroll_exit=True)


        # This lets the user interact with the Form.
        frame.edit()

        Reminder = Reminder.value
        SelectedTime = time.get_selected_objects()
        AmPm = AmPm.value
        Date = date.value

        #Convert SelectedTime from Tuple to List
        from itertools import chain
        SelectedTimeLst = list(chain.from_iterable(SelectedTime))
        #Then Convert the List to a String
        SelectedTimeStr = "".join(str(x) for x in SelectedTimeLst)
        
        Time = SelectedTimeStr +" "+ AmPm

        from datetime import datetime
        dateTime = datetime.strptime(f"{Date} {Time}", "%Y-%m-%d %I:%M %p")
        print(Time)
        print(dateTime)

        #Save the Datetime as a timestamp in the database
        Scheduled_Time = dateTime.timestamp()

        #------------------------------------------
        #           Add Reminder to Database
        #------------------------------------------
        #Connect to the database
        connection = sqlite3.connect(MainDatabase)
        cursor = connection.cursor()
        
        #Add the reminder to the reminders table
        cursor.execute("INSERT into Reminders (Reminder, DateTime) VALUES(?,?)",(Reminder,Scheduled_Time))
        connection.commit()
        connection.close() #Close the connection
        #------------------------------------------

App = SetReminderTUI()
App.run()

def schedule_reminder(schedule):
    """Schedule a reminder"""
    connection = sqlite3.connect(MainDatabase)
    for reminder in connection.execute("SELECT Reminder, DateTime FROM reminders"):
        message, sched_time = reminder
        scheduler.enterabs(sched_time, 1, send_reminder, (message, sched_time))
    connection.close()

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

schedule_reminder(scheduler)
while not scheduler.empty():
    scheduler.run(False)
Edit:
How would I go about deleting old reminders?

Tried the code below but I get this error:
Error:
connection.execute("DELETE FROM reminders WHERE DateTimeStamp = ?",(scheduled_time)) ValueError: parameters are of unsupported type
Even through my DateTimeStamp is stored as a REAL value (Float) in my Database, and scheduled_time is also a float
#Display the Reminder
def send_reminder(Reminder, scheduled_time):
    """Function called when time to send reminder"""
    #Convert the scheduled_time.timestamp() to a readable DateTime String
    scheduled_timeStr = datetime .fromtimestamp(scheduled_time)
    reminderPopup(Reminder)
    print(f"Reminders Popup: {scheduled_timeStr}: {Reminder}")

    #Delete reminder after it's been displayed
    connection = sqlite3.connect(MainDatabase) #Connect to Database
    connection.execute("DELETE FROM reminders WHERE DateTimeStamp = ?",(scheduled_time))
    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
  Need help with a while loop ampereap 4 170 May-31-2024, 02:25 PM
Last Post: ampereap
  problem program runs in a loop jasserin 0 179 May-18-2024, 03:07 PM
Last Post: jasserin
  [SOLVED] Loop through directories and files one level down? Winfried 3 408 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 769 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 349 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 805 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 1,055 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 595 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 479 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 539 Jan-17-2024, 10:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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