Changeset f959488 in flowtimer


Ignore:
Timestamp:
08/17/24 22:48:01 (9 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
guix
Children:
daa2276
Parents:
3b76475
Message:

Schedule can be initialized with a list of Phases and RecurringPhaseSequences

Location:
flowtimer
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • flowtimer/Phase.py

    r3b76475 rf959488  
    88    """
    99
    10     def __init__(self, title, duration):
     10    def __init__(self, title, ticks):
    1111        """
    1212        creates the variables associated with that class
     
    1515        :param title: Name of phase
    1616
    17         :type duration: int
    18         :param duration: Duration in seconds
     17        :type ticks: int
     18        :param ticks: ticks in seconds
    1919        """
    2020
    2121        self.title = title
    22         self.duration = duration
     22        self.initial_ticks = ticks
    2323        self.state = "initial"
    24         self.time_left = self.duration
     24        self.ticks_left = ticks
    2525
    2626    def to_json(self):
    27         return json.dumps({"title": self.title, "duration": self.duration})
    28         # return json.dumps(self.__dict__)
     27        return json.dumps({"title": self.title, "duration": self.initial_ticks})
    2928
    3029    def __str__(self):
     
    3635        """
    3736
    38         return ("-->" + self.title + "\nDuration=" +
    39                 str(self.duration) + "\n")
     37        return ("-->" + self.title + "\nTicks left=" +
     38                str(self.ticks_left) + "\n" + str(self.state) + "\n")
    4039
    4140    def abort(self):
     
    4443    def start(self):
    4544        self.state = "running"
     45
     46    def reset(self):
     47        self.ticks_left = self.initial_ticks
     48        self.state = "initial"
    4649
    4750    def pause(self):
     
    5861        return self.state == "paused"
    5962
    60     def tick(self, duration):
    61         self.time_left -= duration
     63    def tick(self, ticks):
     64        result = self.ticks_left - ticks
    6265
    63         if self.time_left <= 0:
    64             self.time_left = 0
     66        if result <= 0:
     67            print("Single phase finished")
     68            self.ticks_left = 0
    6569            self.state = "finished"
     70        else:
     71            self.ticks_left = result
     72        return result
  • flowtimer/RecurringPhaseSequence.py

    r3b76475 rf959488  
    2424                           "repetitions": 3})
    2525
     26    @classmethod
     27    def default(cls):
     28        return cls.from_json(cls.default_json_string())
     29
    2630    def __init__(self, phase_list, repetitions):
     31        assert repetitions > 0
     32        assert phase_list is not []
     33        self.state = "initial"
    2734        self.phase_list = phase_list
    28         self.repetitions = repetitions
     35        self.current_phase = phase_list[0]
     36        self.initial_repetitions = repetitions
     37        self.passes_left = repetitions
    2938
    3039    def to_json(self):
    3140        return json.dumps(self.__dict__, default=lambda each: each.to_json())
    3241
     42    def upcoming_phases_in_pass(self):
     43        index = self.phase_list.index(self.current_phase)
     44        if index < len(self.phase_list) - 1:
     45            return self.phase_list[index+1:]
     46        return []
     47
     48    def ticks_left(self):
     49        return (self.passes_left * sum([each.initial_ticks for each in self.phase_list]) +
     50                self.current_phase.ticks_left +
     51                sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
     52
     53    def finished(self):
     54        return (self.passes_left < 1) and (not self.upcoming_phases_in_pass())
     55
     56    def abort(self):
     57        self.current_phase.abort()
     58        self.state = "finished"
     59
     60    def tick(self, ticks):
     61        if not self.finished():
     62            result = self.current_phase.tick(ticks)
     63            print("Current phase", self.current_phase)
     64            print("Passes left:", self.passes_left)
     65            if self.current_phase.finished():
     66                if self.upcoming_phases_in_pass():
     67                    self.current_phase.reset()
     68                    self.current_phase = self.upcoming_phases_in_pass()[0]
     69                    self.current_phase.start()
     70                    print("New phase:", self.current_phase)
     71                    self.tick(abs(result))
     72                    return True
     73                self.passes_left -= 1
     74                if self.finished():
     75                    self.state = "finished"
     76                else:
     77                    self.current_phase.reset()
     78                    self.current_phase = self.phase_list[0]
     79        return True
     80
    3381    def unrolled(self):
    3482        return [deepcopy(seq) for seq in [each for each in self.repetitions * self.phase_list]]
    35         # return self.repetitions * self.phase_list
  • flowtimer/Schedule.py

    r3b76475 rf959488  
    3232        if (self.current_phase.finished()) and (self.phase_list[-1] == self.current_phase):
    3333            self.state = "finished"
     34            print("Finished")
    3435            return True
    3536        else:
     
    4344            self.current_phase = self.phase_list[index+1]
    4445
    45     def time_left(self):
    46         return self.current_phase.time_left + sum([phase.time_left for phase in self.upcoming_phases()])
     46    def ticks_left(self):
     47        return (self.current_phase.ticks_left +
     48                sum([phase.ticks_left for phase in self.upcoming_phases()]))
    4749
    4850    def upcoming_phases(self):
Note: See TracChangeset for help on using the changeset viewer.