-
Notifications
You must be signed in to change notification settings - Fork 0
/
Training.py
48 lines (39 loc) · 1.68 KB
/
Training.py
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
from Exercise import *
from Metric import *
from exercises_and_metrics_types import *
class Training( object ):
"""Single training consists of a number of exercises and
some arbitrary data, such as duration, start time, end time,
comments etc."""
def __init__( self, exercises_list, training_description_dict ):
self.exercises = exercises_list
self.description = training_description_dict
def add_exercise( self, exercise ):
"""Add single exercise. Only one is expected."""
self.exercises.append( exercise )
def add_description( self, description_dict ):
self.description.update( description_dict )
def repr_for_json_dump( self ):
for_json = {}
exercises_for_json_dump = [ x.repr_for_json_dump() for x in self.exercises ]
for_json.update( self.description )
for_json.update( { 'exercises': exercises_for_json_dump } )
return for_json
def __repr__( self ):
return "Training: \n" + \
"\t" + repr( self.description ) + "\n" + \
"\t" + repr( self.exercises ) + "\n"
def __str__( self ):
return "Training: \n" + \
"\t" + str( self.description ) + "\n" + \
"\t" + str( self.exercises ) + "\n"
@classmethod
def init_from_json( cls, dict_from_json ):
training = cls( [], {} )
exercises = dict_from_json.pop('exercises')
for x in exercises:
exc_class_name = x.pop("type", "Exercise")
exc_class = globals()[exc_class_name]
training.add_exercise( exc_class.init_from_json( x ) )
training.add_description( dict_from_json )
return training