-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.py
39 lines (30 loc) · 1.05 KB
/
mapper.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
#!/usr/bin/python3
import fileinput
import json
from typing import List
def _calculate_change(current: float, previous: float) -> float:
try:
return round((current - previous) / previous * 100, 2)
except ZeroDivisionError:
return 0.0
def _map_entry(symbol: str, data: List[dict]):
previous = data[0]['close']
for single in data[1:]:
current = single['close']
change = _calculate_change(current, previous)
previous = current
print(f'{symbol}\t{change}\t1')
def mapper():
"""
Maps the historical prices and calculates the change for each company
The input lines MUST be in a format of json dict (as specified in financial API)
"""
for line in fileinput.input():
content = json.loads(line.strip())
entry_content = content.get('historicalStockList', [content])
for entry in entry_content:
entry_name = entry['symbol']
entry_data = entry['historical']
_map_entry(entry_name, entry_data)
if __name__ == '__main__':
mapper()