-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
97 lines (76 loc) · 3.22 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Here are utilities - functions that are being used in multiple cogs."""
import json
import os
import gspread
import pandas as pd
class WorksheetNameNotInList(Exception):
"""Exception for Worksheet variable type: only lists are allowed."""
def get_worksheets(gs_name, ws_names, create=False, size=(10_000, 20)):
"""Gets worksheets of spreadsheet. (max 4 worksheets are allowed)
If create is False and if at least one worksheet was found, it returns the
worksheet instead of raising an error.
Args:
gs_name (str): google spreadsheet name
ws_names (Tuple[str]): worksheet names (a tab of spreadsheet)
create (bool, optional): Create new spreadsheet/worksheet if it doesn't
exist. Defaults to False.
size (Tuple[str], optional: Number of rows and columns for all
worksheets. Defaults to (10_000, 20).
Raises:
err: SpreadsheetNotFound
WorksheetNameNotInList: Worksheets are not in a tuple!
err: WorksheetNotFound
Returns:
List[gspread.worksheet.Worksheet, pandas.core.frame.DataFrame]: (
list of worksheet tables,
list of worksheet dataframe tables
)
"""
print("Getting worksheets... ", end="")
credentials_dict_str = os.environ["GOOGLE_CREDENTIALS"]
credentials_dict = json.loads(credentials_dict_str)
google_credentials = gspread.service_account_from_dict(credentials_dict)
try:
spreadsheet = google_credentials.open(gs_name)
except gspread.exceptions.SpreadsheetNotFound as err:
if create:
spreadsheet = google_credentials.create(gs_name)
else:
raise err
worksheets = []
worksheet_dfs = []
try:
for ws_name in ws_names:
worksheet = spreadsheet.worksheet(ws_name)
worksheets.append(worksheet)
worksheet_df = pd.DataFrame(worksheet.get_all_records())
worksheet_dfs.append(worksheet_df)
except gspread.exceptions.WorksheetNotFound as err:
if len(ws_name) == 1 and ws_names.startswith(ws_name):
message = "You probably havent put your worksheets into the tuple!"
raise WorksheetNameNotInList(message) from err
elif create:
assert len(ws_names) < 5, "You're creating too many worksheets!"
for ws_name in ws_names:
worksheet = spreadsheet.add_worksheet(
ws_name, rows=size[0], cols=size[1]
)
worksheets.append(worksheet)
worksheet_df = pd.DataFrame(worksheet.get_all_records())
worksheet_dfs.append(worksheet_df)
elif not worksheet_dfs:
raise err
print("Done!")
return worksheets, worksheet_dfs
def update_worksheet(ws, ws_df):
"""Updates worksheet with dataframe. (appends new rows, updates existing values?)
Args:
ws_df (pandas.core.frame.DataFrame): worksheet dataframe table
ws (gspread.worksheet.Worksheet): worksheet table
"""
print("Updating worksheets... ", end="")
ws_df = ws_df.fillna("")
df_list = [ws_df.columns.values.tolist()]
df_list += ws_df.values.tolist()
ws.update(df_list, value_input_option="USER_ENTERED")
print("Done!")