Changeset 37ae3b7 in flowtimer


Ignore:
Timestamp:
08/15/24 21:00:01 (9 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
guix
Children:
3b76475
Parents:
729b23d
Message:

more refactorings

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • flowtimer/RecurringPhaseSequence.py

    r729b23d r37ae3b7  
    11import json
     2from copy import deepcopy
     3from Phase import Phase
    24
    35
    46class RecurringPhaseSequence:
     7
     8    @classmethod
     9    def from_json(cls, a_json_string):
     10        def custom_object_hook(d):
     11            if 'title' in d and 'duration' in d:
     12                return Phase(d['title'], d['duration'])
     13            if 'phase_list' in d and 'repetitions' in d:
     14                return RecurringPhaseSequence(d['phase_list'], d['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", "duration": 10},
     21                                          {"title": "Tasking", "duration": 5},
     22                                          {"title": "Work", "duration": 45},
     23                                          {"title": "Break", "duration": 15}],
     24                           "repetitions": 3})
    525
    626    def __init__(self, phase_list, repetitions):
     
    1232
    1333    def unrolled(self):
    14         return [[deepcopy(seq) for seq in [each for each in se1f.repetitions * se1f.phase_list]]]
     34        return [deepcopy(seq) for seq in [each for each in self.repetitions * self.phase_list]]
    1535        # return self.repetitions * self.phase_list
  • flowtimer/Schedule.py

    r729b23d r37ae3b7  
    55
    66    def __init__(self, phase_list):
    7         self.currentValue = 0
    87        self.phase_list = phase_list
    98        self.current_phase = phase_list[0]
     
    1110
    1211    def to_json(self):
    13         json.dumps(self.__dict__)
     12        return json.dumps(self.__dict__)
    1413
    1514    def start(self):
     
    4443            self.current_phase = self.phase_list[index+1]
    4544
     45    def time_left(self):
     46        return self.current_phase.time_left + sum([phase.time_left for phase in self.upcoming_phases])
     47
     48    def upcoming_phases(self):
     49        index = self.phase_list.index(self.current_phase)
     50        return self.phase_list[index:]
     51
    4652    def current_phase_is_final(self):
    4753        index = self.phase_list.index(self.current_phase)
  • flowtimer/main.py

    r729b23d r37ae3b7  
    33from tkinter import filedialog
    44from Schedule import Schedule
    5 from Phase import Phase
     5from RecurringPhaseSequence import RecurringPhaseSequence
    66from PIL import Image, ImageTk
    7 from copy import copy
    8 import pandas as pd
     7from pathlib import Path
    98import os
    109import math
     
    2423        self.count = 0
    2524        self.currentValue = 0
    26         self.default_config = pd.read_csv("default_config.csv", sep=',', header=0,
    27                                           names=('Titel', 'Values'), index_col=False,
    28                                           keep_default_na=False)
    29         self.photo = ImageTk.PhotoImage(file='flowtimer_startbg_new.png')
    30         self.show_config = pd.read_csv("default_config.csv", usecols=[0, 1])
     25        config = Path(__file__).parent / 'configs' / 'default.json'
     26        with open(config) as config_file:
     27            self.schedule = Schedule(RecurringPhaseSequence.from_json(config_file.read()).unrolled())
     28        self.photo = ImageTk.PhotoImage(file=Path(__file__).parent / 'resources' / 'flowtimer_startbg_new.png')
     29        print(self.schedule.current_phase)
     30        self.show_config = self.schedule.current_phase.title
    3131        self.config_state = 'default'
     32        print(self.schedule)
    3233
    3334        self.build_gui()
     
    9596                                     font="courier 14", width=20, command=self.quit_app)
    9697        self.quit_button.pack(side='left', fill='both', ipady=20, expand=True)
    97 
    98         df = self.default_config
    99         repeat_of_phases = df['Values'][3]
    100 
    101         phase_1 = Phase(title=df['Titel'][0], duration=df['Values'][0])
    102         phase_2 = Phase(title=df['Titel'][1], duration=df['Values'][1])
    103         phase_3 = Phase(title=df['Titel'][2], duration=df['Values'][2])
    104 
    105         if repeat_of_phases == 1:
    106             phase_list = [phase_1, phase_2, phase_3]
    107 
    108         if repeat_of_phases == 2:
    109             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3)]
    110 
    111         if repeat_of_phases == 3:
    112             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3),
    113                           copy(phase_2), copy(phase_3)]
    114 
    115         if repeat_of_phases == 4:
    116             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3),
    117                           copy(phase_2), copy(phase_3), copy(phase_2), copy(phase_3)]
    118 
    119         if repeat_of_phases == 5:
    120             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3),
    121                           copy(phase_2), copy(phase_3), copy(phase_2), copy(phase_3),
    122                           copy(phase_2), copy(phase_3)]
    123 
    124         self.repeats = repeat_of_phases
    125 
    126         self.schedule = Schedule(phase_list)
    12798
    12899    def tick(self):
     
    254225    def change_config(self):
    255226        self.config_state = 'user'
    256         self.config_files = [("all files", "*.csv")]
     227        self.config_files = [("all files", "*.json")]
    257228        self.answer = filedialog.askopenfilename(parent=root,
    258                                                  initialdir=os.chdir("./configs"),
     229                                                 initialdir=Path(__file__).parent / 'configs',
    259230                                                 title="Please choose a config file:",
    260231                                                 filetypes=self.config_files)
    261         self.user_config = pd.read_csv(self.answer, sep=',', header=0,
    262                                        names=('Titel', 'Values'),
    263                                        index_col=False, keep_default_na=False)
    264         self.choosed_config = pd.read_csv(self.answer, usecols=[0, 1])
    265         self.df = self.user_config
    266         self.label_config_text.config(text=self.choosed_config)
    267         df = self.user_config
    268         repeat_of_phases = df['Values'][3]
    269 
    270         phase_1 = Phase(title=df['Titel'][0], duration=df['Values'][0])
    271         phase_2 = Phase(title=df['Titel'][1], duration=df['Values'][1])
    272         phase_3 = Phase(title=df['Titel'][2], duration=df['Values'][2])
    273 
    274         if repeat_of_phases == 1:
    275             phase_list = [phase_1, phase_2, phase_3]
    276 
    277         if repeat_of_phases == 2:
    278             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3)]
    279 
    280         if repeat_of_phases == 3:
    281             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3),
    282                           copy(phase_2), copy(phase_3)]
    283 
    284         if repeat_of_phases == 4:
    285             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3),
    286                           copy(phase_2), copy(phase_3),
    287                           copy(phase_2), copy(phase_3)]
    288 
    289         if repeat_of_phases == 5:
    290             phase_list = [phase_1, phase_2, phase_3, copy(phase_2), copy(phase_3),
    291                           copy(phase_2), copy(phase_3), copy(phase_2), copy(phase_3),
    292                           copy(phase_2), copy(phase_3)]
    293 
    294         self.repeats = repeat_of_phases
    295 
    296         self.schedule = Schedule(phase_list)
     232        with open(self.answer) as config_file:
     233            self.schedule = Schedule(RecurringPhaseSequence.from_json(config_file.read()).unrolled())
    297234
    298235
     
    301238root.geometry("1280x860")
    302239root.config(bg='black')
    303 root.iconphoto(False, ImageTk.PhotoImage(Image.open('icon_bombtimer.png')))
     240root.iconphoto(False, ImageTk.PhotoImage(Image.open(Path(__file__).parent / 'resources' / 'icon_bombtimer.png')))
    304241
    305242app = TimerApp(root)
  • tests/test_schedule.py

    r729b23d r37ae3b7  
    1313        assert schedule.current_phase == phase1
    1414        assert schedule.state == "initial"
    15         assert schedule.currentValue == 0
    1615
    1716    def test_schedule_start(self):
Note: See TracChangeset for help on using the changeset viewer.