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
  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