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
#18
After playing with this a bit, I don't think datetime is the best way to save a datetime object. Following my advice that save values in their most useful format, I wrote an example that saves time as a timestamp. The scheduler uses timestamps. The databases can store timestamps, but DateTime objects have to be converted to and from strings. And using DateTime forces two applications (the one that saves the events and the one that sends the events) to agree on DateTime format used.

This example writes reminders to a database, retrieves them from the database, schedules the reminders, and sends the reminders. The main change needed to make this work with your code is that your events are entered using a datetime string. You'll need to convert that to a datetime object, and then use the datetime object to get a timestamp.
import sqlite3 as sql
from datetime import datetime
import sched, time

time_format = "%I:%M:%S %p"

def send_reminder(sched_time, message):
    """Just pring the message and the scheduled time (and current time)"""
    print(message)
    print(datetime.now().strftime(time_format))
    print(datetime.fromtimestamp(sched_time).strftime(time_format))

def save_reminders(reminders):
    """Make a table in the database to hold the reminders"""
    now = int(time.time())
    db = sql.connect("test.db")
    db.execute("CREATE TABLE IF NOT EXISTS reminders(message TEXT, time INTEGER)")
    db.execute("DELETE FROM reminders")
    for reminder, seconds in reminders:
        db.execute(
            "INSERT INTO reminders (message, time) VALUES(?, ?)",
            (reminder, now + seconds))
    db.commit()
    db.close()

def schedule_reminders(schedule):
    """Get reminders from the database and add to the schedule."""
    db = sql.connect("test.db")
    for reminder in db.execute("SELECT * FROM reminders"):
        message, sched_time = reminder
        schedule.enterabs(sched_time, 1, send_reminder, (sched_time, message))
    db.close()

# Make a database of reminders that run 5, 10 and 15 seconds from now.
save_reminders((
    ("This is the first message", 5),
    ("This message is 5 seconds after the first", 10),
    ("This message is 15 seconds after the first", 20))
)

schedule = sched.scheduler(time.time)
schedule_reminders(schedule)
while not schedule.empty():
    schedule.run()
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 deanhystad - Oct-15-2022, 10:21 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
  problem program runs in a loop jasserin 0 134 May-18-2024, 03:07 PM
Last Post: jasserin
  [SOLVED] Loop through directories and files one level down? Winfried 3 337 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 656 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 313 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 736 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 985 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 545 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 444 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 504 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 514 Jan-09-2024, 11:11 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