-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathutils.py
43 lines (32 loc) · 1.11 KB
/
utils.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
import json
import os
import json
import pandas as pd
from datetime import date, timedelta
def is_holiday():
if date.today().strftime("%a") in ['Sat', 'Sun', 'Mon']:
return True
else:
return False
def get_friday():
days = {'Sat': 1, 'Sun': 2, 'Mon': 3}
diff = int(days[date.today().strftime("%a")])
return (date.today() - timedelta(days=diff)).strftime("%d-%b-%Y")
def get_today():
return (date.today() - timedelta(days=1)).strftime("%d-%b-%Y")
def render_response(data, as_json=False, as_Dataframe=False):
if as_json is True:
return json.dumps(data)
# parameter 'as_Dataframe' only works with get_scheme_historical_nav()
elif as_Dataframe is True:
df = pd.DataFrame.from_records(data['data'])
df['dayChange'] = df['nav'].astype(float).diff(periods=-1)
df = df.set_index('date')
return df
else:
return data
class Utilities:
def __init__(self):
self._filepath = str(os.path.dirname(os.path.abspath(__file__))) + '/const.json'
with open(self._filepath, 'r') as f:
self.values = json.load(f)