progress.py 1.14 KB
Newer Older
1
class completion(object):
2
    def __init__(self, **d):
3 4 5 6 7 8 9
        self.dict = dict({'duration_total': 0,
                          'duration_watched': 0,
                          'done': True,
                          'questions_correct': 0,
                          'questions_incorrect': 0,
                          'questions_total': 0})
        if d:
10 11 12 13 14 15 16 17 18
            self.dict.update(d)

    def __getitem__(self, key):
        return self.dict[key]

    def __setitem__(self, key, value):
        self.dict[key] = value

    def __add__(self, other):
19 20 21 22 23 24 25
        result = dict(self.dict)
        for item in ['duration_total',
                     'duration_watched',
                     'done',
                     'questions_correct',
                     'questions_incorrect',
                     'questions_total']:
26
            result[item] = result[item] + other.dict[item]
27
        return completion(**result)
28 29

    def __contains__(self, key):
30 31 32 33 34 35
        return key in dict

    def __repr__(self):
        return repr(self.dict)

if __name__ == '__main__':
36 37 38
    dict1 = completion(duration_total=5)
    dict2 = completion(duration_total=7)
    print dict1 + dict2