-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.py
More file actions
87 lines (62 loc) · 1.72 KB
/
Copy pathtask_manager.py
File metadata and controls
87 lines (62 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import time
# -----------------------
# Decorator
# -----------------------
def log_execution(func):
def wrapper(*args, **kwargs):
start = time.time()
print(f"\nRunning: {func.__name__}")
result = func(*args, **kwargs)
end = time.time()
print(f"Finished in {end - start:.4f} seconds")
return result
return wrapper
# -----------------------
# Task Class
# -----------------------
class Task:
def __init__(self, title, priority):
self.title = title
self.priority = priority
def __str__(self):
return f"{self.title} (Priority: {self.priority})"
# -----------------------
# Iterator
# -----------------------
class TaskIterator:
def __init__(self, tasks):
self.tasks = tasks
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.tasks):
raise StopIteration
task = self.tasks[self.index]
self.index += 1
return task
# -----------------------
# Iterable
# -----------------------
class TaskCollection:
def __init__(self):
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def __iter__(self):
return TaskIterator(self.tasks)
# -----------------------
# Functions using Decorator
# -----------------------
@log_execution
def display_tasks(task_collection):
for task in task_collection:
print(task)
# -----------------------
# Main Program
# -----------------------
tasks = TaskCollection()
tasks.add_task(Task("Learn Python Decorators", "High"))
tasks.add_task(Task("Practice Iterators", "Medium"))
tasks.add_task(Task("Complete Mini Project", "High"))
display_tasks(tasks)