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
#20
Edit:

Did this:
    format = "%Y-%m-%d %H:%M:%S"
    scheduled_time2 = datetime.strptime(DateTimeStr, format)
No format error but I get this output:
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') Reminders in Database: ('Test 4', '2022-10-15 20:19:00') Scheduled Time: ('2022-10-15 11:25:00',) DateTime String: 2022-10-15 11:25:00 Scheduled Time: ('2022-10-15 11:28:00',) DateTime String: 2022-10-15 11:28:00 Scheduled Time: ('2022-10-15 11:30:00',) DateTime String: 2022-10-15 11:30:00 Scheduled Time: ('2022-10-15 20:19:00',) DateTime String: 2022-10-15 20:19:00 Current Date Time: 2022-10-15 20:20:00 Reminders Popup: ('2022-10-15 20:19:00',): Test 1 Reminders Popup: ('2022-10-15 20:19:00',): Test 3 Reminders Popup: ('2022-10-15 20:19:00',): Test 2 Reminders Popup: ('2022-10-15 20:19:00',): Test 4
Which is weird because the Reminder Popup says all 4 reminders are scheduled for 8:19 but they're not (only Test 4 was)

import sched
import time
import sqlite3
from tokenize import Double
from BX_External_Functions import checkForReminders, reminderPopup
from BX_Constants import MainDatabase, currentdate, currentTime

from datetime import datetime
#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)
#----------
    #Convert scheduled_time from Tuple to List
    from itertools import chain
    DateTimeList = list(chain.from_iterable(scheduled_time))
    #Then Convert the List to a String
    DateTimeStr = "".join(str(x) for x in DateTimeList)

    print("DateTime String:", DateTimeStr)

    format = "%Y-%m-%d %H:%M:%S"
    scheduled_time2 = datetime.strptime(DateTimeStr, format)
#----------
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=scheduled_time2.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
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 185 May-31-2024, 02:25 PM
Last Post: ampereap
  problem program runs in a loop jasserin 0 184 May-18-2024, 03:07 PM
Last Post: jasserin
  [SOLVED] Loop through directories and files one level down? Winfried 3 411 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 770 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 482 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 540 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