Skip to content

Commit 12e7518

Browse files
feat overlapping time frames
1 parent 5319fda commit 12e7518

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/persistence/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22

33
class Model:
4-
"""Root of class """
4+
"""Root of class hierarchy"""
55

66
@dataclass
77
class StudentModel(Model):

src/use_cases/scholar_time_by_students.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
class PersistenceScholarTimeProtocol(Protocol):
55
def student_names(self) -> Iterable[str]:
6-
""""""
6+
"""It should return an iterable with the names of students in the system"""
77

88
def student_presence_group_by_day(self, student_name) -> Dict[int, List]:
9-
"""It should check when the segments intercept between them"""
9+
"""It should return each student's presence group by the presence days"""
1010

1111
def run(
1212
repo: PersistenceScholarTimeProtocol
@@ -17,10 +17,31 @@ def run(
1717
days, delta = 0, 0
1818
for presences in group_by_day.values():
1919
days += 1
20-
delta += sum([item.delta_time for item in presences])
20+
21+
# In the database, there may be errors and overlapping presence instances,
22+
# so that the total number of minutes cannot be calculated with a simple sum.
23+
presences.sort(key=lambda x:x.start_min)
24+
# Each key in the dict meads a day where the student went to the school
25+
# So, there are minimaly a presente instance
26+
pivot = presences[0]
27+
delta += pivot.delta_time
28+
for item in presences[1:]:
29+
if pivot.end_min <= item.start_min:
30+
# The student went out and then came back in. Example by symbols ()[]
31+
delta += item.delta_time
32+
elif pivot.end_min < item.end_min:
33+
# entered twice and then exited twice more. Example by symbols ([)]
34+
print("Warning: The information might be incomplete")
35+
delta += item.end_min - pivot.end_min
36+
else:
37+
# One presence is a subprocense of the other Example by symbols ([])
38+
print("Warning: The information might be incomplete")
39+
continue
40+
41+
pivot = item
42+
2143

2244
if days == 0:
23-
print(f'{student}: 0 minutes')
45+
yield f'{student}: 0 minutes'
2446
else:
25-
print(
26-
f'{student}: {delta} minutes in {days} day{"" if days == 1 else "s"}')
47+
yield f'{student}: {delta} minutes in {days} day{"" if days == 1 else "s"}'

0 commit comments

Comments
 (0)