forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.py
More file actions
42 lines (33 loc) · 942 Bytes
/
timeline.py
File metadata and controls
42 lines (33 loc) · 942 Bytes
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
"""app.timeline.py"""
from collections import OrderedDict
class Timeline:
"""
Timeline with history of data.
"""
def __init__(self, history=None):
self.__timeline = history if history else {}
@property
def timeline(self):
"""
Gets the history sorted by date (key).
"""
return OrderedDict(sorted(self.__timeline.items()))
@property
def latest(self):
"""
Gets the latest available history value.
"""
# Get values in a list.
values = list(self.timeline.values())
# Last item is the latest.
if values:
return values[-1] or 0
# Fallback value of 0.
return 0
def serialize(self):
"""
Serializes the timeline into a dict.
:returns: The serialized timeline.
:rtype: dict
"""
return {"latest": self.latest, "timeline": self.timeline}