source: flowtimer/flowtimer/RecurringPhaseSequence.py@ daa2276

guix
Last change on this file since daa2276 was daa2276, checked in by Enrico Schwass <ennoausberlin@…>, 9 months ago

better variable names and tests added

  • Property mode set to 100644
File size: 3.3 KB
Line 
1import json
2from copy import deepcopy
3from flowtimer.Phase import Phase
4
5
6class RecurringPhaseSequence:
7
8 @classmethod
9 def from_json(cls, a_json_string):
10 def custom_object_hook(d):
11 if 'title' in d and 'initial_ticks' in d:
12 return Phase(d['title'], d['initial_ticks'])
13 if 'phase_list' in d and 'initial_repetitions' in d:
14 return RecurringPhaseSequence(d['phase_list'], d['initial_repetitions'])
15 return d
16 return json.loads(a_json_string, object_hook=custom_object_hook)
17
18 @classmethod
19 def default_json_string(cls):
20 return json.dumps({"phase_list": [{"title": "Huddle", "initial_ticks": 10},
21 {"title": "Tasking", "initial_ticks": 5},
22 {"title": "Work", "initial_ticks": 45},
23 {"title": "Break", "initial_ticks": 15}],
24 "initial_repetitions": 3})
25
26 @classmethod
27 def default(cls):
28 return cls.from_json(cls.default_json_string())
29
30 def __init__(self, phase_list, repetitions):
31 assert repetitions > 0
32 assert phase_list is not []
33 self.state = "initial"
34 self.phase_list = phase_list
35 self.current_phase = phase_list[0]
36 self.initial_repetitions = repetitions
37 self.passes_left = repetitions
38
39 def to_json(self):
40 return json.dumps(self.__dict__, default=lambda each: each.to_json())
41
42 def current_phase_number(self):
43 return self.phase_list.index(self.current_phase)
44
45 def phases_left_in_pass(self):
46 return len(self.upcoming_phases_in_pass())
47
48 def upcoming_phases_in_pass(self):
49 if self.current_phase_number() < len(self.phase_list) - 1:
50 return self.phase_list[self.current_phase_number()+1:]
51 return []
52
53 @property
54 def ticks_left(self):
55 return (
56 (self.passes_left-1) * sum([each.initial_ticks for each in self.phase_list]) +
57 self.current_phase.ticks_left +
58 sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
59
60 def finished(self):
61 return (self.passes_left < 1) and (not self.upcoming_phases_in_pass())
62
63 def abort(self):
64 self.current_phase.abort()
65 self.state = "finished"
66
67 def tick(self, ticks):
68 if not self.finished():
69 result = self.current_phase.tick(ticks)
70 print("Current phase", self.current_phase)
71 print("Passes left:", self.passes_left)
72 if self.current_phase.finished():
73 if self.upcoming_phases_in_pass():
74 self.current_phase.reset()
75 self.current_phase = self.upcoming_phases_in_pass()[0]
76 self.current_phase.start()
77 print("New phase:", self.current_phase)
78 self.tick(abs(result))
79 return True
80 self.passes_left -= 1
81 if self.finished():
82 self.state = "finished"
83 else:
84 self.current_phase.reset()
85 self.current_phase = self.phase_list[0]
86 return True
87
88 def unrolled(self):
89 return [deepcopy(seq) for seq in [each for each in self.initial_repetitions * self.phase_list]]
Note: See TracBrowser for help on using the repository browser.