Time Scheduling
This example demonstrates scheduling capabilities in the True-Core time module.
Example Code
Time Scheduling Example
1"""
2Examples of using the Schedule and Event classes for advanced time management.
3"""
4
5from true.time import Time, TimeUnit, Event, Schedule
6
7
8def demo_basic_scheduling():
9 """Demonstrate basic scheduling operations."""
10 print("=== Basic Scheduling ===")
11
12 # Create a schedule
13 schedule = Schedule()
14
15 # Create some events
16 meeting = Event(
17 name="Team Meeting",
18 start_time=Time.now(),
19 end_time=Time.now().add(1, TimeUnit.HOURS),
20 description="Weekly team sync",
21 tags=["meeting", "team"]
22 )
23
24 lunch = Event(
25 name="Lunch Break",
26 start_time=Time.now().add(2, TimeUnit.HOURS),
27 end_time=Time.now().add(3, TimeUnit.HOURS),
28 tags=["break"]
29 )
30
31 # Add events to schedule
32 schedule.add_event(meeting)
33 schedule.add_event(lunch)
34
35 print(f"Schedule has {len(schedule)} events")
36 print(f"Schedule: {schedule}")
37
38
39def demo_event_operations():
40 """Demonstrate event manipulation and querying."""
41 print("\n=== Event Operations ===")
42
43 schedule = Schedule()
44
45 # Create an event
46 event = Event(
47 name="Project Review",
48 start_time=Time.now(),
49 end_time=Time.now().add(2, TimeUnit.HOURS),
50 tags=["meeting", "project"]
51 )
52
53 schedule.add_event(event)
54
55 # Update event
56 schedule.update_event(
57 "Project Review",
58 description="Quarterly project review meeting",
59 priority=1
60 )
61
62 # Get event duration
63 event = [e for e in schedule if e.name == "Project Review"][0]
64 print(f"Event duration: {event.duration(TimeUnit.MINUTES)} minutes")
65
66 # Remove event
67 schedule.remove_event("Project Review")
68 print(f"Events after removal: {len(schedule)}")
69
70
71def demo_schedule_analysis():
72 """Demonstrate schedule analysis features."""
73 print("\n=== Schedule Analysis ===")
74
75 schedule = Schedule()
76
77 # Add multiple events
78 events = [
79 Event(
80 name="Meeting 1",
81 start_time=Time.now(),
82 end_time=Time.now().add(1, TimeUnit.HOURS),
83 tags=["meeting"]
84 ),
85 Event(
86 name="Meeting 2",
87 start_time=Time.now().add(2, TimeUnit.HOURS),
88 end_time=Time.now().add(3, TimeUnit.HOURS),
89 tags=["meeting"]
90 ),
91 Event(
92 name="Break",
93 start_time=Time.now().add(4, TimeUnit.HOURS),
94 end_time=Time.now().add(5, TimeUnit.HOURS),
95 tags=["break"]
96 )
97 ]
98
99 for event in events:
100 schedule.add_event(event)
101
102 # Get schedule statistics
103 start_time = Time.now()
104 end_time = Time.now().add(6, TimeUnit.HOURS)
105 stats = schedule.get_statistics(start_time, end_time)
106
107 print("Schedule Statistics:")
108 print(f"Total events: {stats['total_events']}")
109 print(f"Total duration: {stats['total_duration']} minutes")
110 print("Tags distribution:", stats['tags_distribution'])
111
112 # Find free slots
113 free_slots = schedule.find_free_slots(
114 start_time,
115 end_time,
116 duration=30,
117 unit=TimeUnit.MINUTES
118 )
119 print(free_slots)
120
121
122def demo_conflict_detection():
123 """Demonstrate schedule conflict detection."""
124 print("\n=== Conflict Detection ===")
125
126 schedule = Schedule()
127
128 # Create overlapping events
129 event1 = Event(
130 name="Event 1",
131 start_time=Time.now(),
132 end_time=Time.now().add(2, TimeUnit.HOURS)
133 )
134
135 event2 = Event(
136 name="Event 2",
137 start_time=Time.now().add(1, TimeUnit.HOURS),
138 end_time=Time.now().add(3, TimeUnit.HOURS)
139 )
140
141 # Add first event
142 schedule.add_event(event1)
143 print("Added first event")
144
145 # Try to add conflicting event
146 try:
147 schedule.add_event(event2)
148 except Exception as e:
149 print(f"Conflict detected: {e}")
150
151
152if __name__ == "__main__":
153 demo_basic_scheduling()
154 demo_event_operations()
155 demo_schedule_analysis()
156 demo_conflict_detection()
Key Features
Task Scheduling - One-time tasks - Recurring tasks - Task cancellation
Schedule Management - Schedule creation - Schedule modification - Schedule cleanup
Advanced Features - Conditional scheduling - Priority scheduling - Error handling