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
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 1,964 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 1,508 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 659 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 707 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  Error loop with Chatgpt sportak12 0 481 Jan-14-2025, 12:04 PM
Last Post: sportak12
  How to convert while loop to for loop in my code? tatahuft 4 780 Dec-21-2024, 07:59 AM
Last Post: snippsat
  How to get keep information in a loop ginod 4 852 Dec-11-2024, 01:32 AM
Last Post: deanhystad
  Regarding The For Loop Hudjefa 5 1,426 Nov-15-2024, 01:02 PM
Last Post: deanhystad
  For Loop beginner agoldav 2 688 Nov-05-2024, 12:51 AM
Last Post: agoldav
  A question about 'Event loop is closed' fc5igm 3 4,965 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