-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.py
More file actions
70 lines (60 loc) · 1.27 KB
/
aws.py
File metadata and controls
70 lines (60 loc) · 1.27 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from settings import (
pd,
json,
s3_fs,
s3_boto,
s3_bucket,
s3_bucket_path,
StringIO,
os,
)
'''
Method: fetch_file_from_s3
Summary: Fetches a file from s3 and returns it as a dataframe
'''
def fetch_file_from_s3(
file_name,
):
# Fetch file from s3
df = pd.read_csv(
s3_fs.open(
s3_bucket_path + '/' + file_name,
)
)
return df
'''
Method: post_file_to_s3
Summary: Updates a file in S3
'''
def post_file_to_s3(
df,
file_name,
):
csv_buffer = StringIO()
df.to_csv(
csv_buffer,
index=False
)
s3_boto.Bucket(
s3_bucket
).put_object(
Key = file_name,
Body=csv_buffer.getvalue()
)
'''
Method: fetch_user_goals
Summary: Fetches goals for a specific user and returns goals as json
'''
def fetch_active_user_goals(
username,
):
# Fetch all goals from db
user_goals = fetch_file_from_s3(os.environ['goals_db'])
# Filter goals for specified user and ensure goals are still active
user_goals = user_goals[
(user_goals['user'] == username)
& (user_goals['active'] == True)
]
# Format user_goals as json
user_goals_json = user_goals.to_json(orient = 'records')
return json.loads(user_goals_json)