-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
199 lines (144 loc) · 5.5 KB
/
utils.py
File metadata and controls
199 lines (144 loc) · 5.5 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import re
import json
import zipfile
import requests
import numpy as np
import pandas as pd
from datetime import date, timedelta
frow, row = 1, 1
fcol, col = "A", "A"
def extract_value_from_zip(zip_file: str) -> str:
with zipfile.ZipFile(zip_file) as z:
file_list = z.namelist()
csv_files = [
file_name for file_name in file_list if file_name.endswith(".csv")]
if not csv_files:
return "No CSV file found in the ZIP."
if len(csv_files) > 1:
return "Multiple CSV files found. Please upload a ZIP with only one CSV file."
with z.open(csv_files[0]) as f:
df = pd.read_csv(f)
if "answer" not in df.columns:
return "The CSV file does not contain a column named 'answer'."
return df.loc[0, "answer"]
def validate_expression(expression: str) -> bool:
return expression.count("=") == 1 and expression.count("(") == expression.count(")")
def calculate_gsheets_formula(client, expression: str) -> int:
global row, col
expression = f"={expression}" if not expression.startswith(
"=") else expression
expression = expression.strip()
if not validate_expression(expression):
return "Invalid formula"
if row > 100:
row = 1
col = chr(ord(col) + 1)
cell = f"{col}{row}"
sheet = client.open('TDS Streamlit Web App').sheet1
sheet.clear()
sheet.update_acell(cell, expression)
row += 1
value = sheet.get_values(cell)[0][0]
if value.isdigit():
return int(value)
return f"Some error occurred: {value}"
def validate_ms_excel_expression(expression: str) -> bool:
pattern = re.compile(
r"=SUM\(\s*TAKE\(\s*SORTBY\(\s*\{(\d+\s*,\s*)*\d+\}\s*,\s*\{(\d+\s*,\s*)*\d+\}\s*\)\s*,\s*\d+\s*(,\s*\d+)?\s*\)\s*\)")
return bool(pattern.match(expression))
def split_params(expression: str) -> tuple:
data1 = list(map(int, expression[expression.find(
"{"):expression.find("}")+1][1:-1].split(",")))
data2 = list(map(int, expression[expression.rfind(
"{"):expression.rfind("}")+1][1:-1].split(",")))
_, x, y = expression.strip("))").rsplit(",", 2)
x = int(x.strip())
y = int(y.strip())
return data1, data2, x, y
def sum_take_sortby(array1: list, array2: list, x: int, y: int = None) -> int:
array1 = np.array(array1)
array2 = np.array(array2)
sorted_indices = np.argsort(array2)
sorted_array1 = array1[sorted_indices]
if y is None:
result = sorted_array1
else:
result = sorted_array1[:y]
return np.sum(result)
def calculate_ms_excel_formula(expression: str) -> int:
expression = f"={expression}" if not expression.startswith(
"=") else expression
expression = expression.strip()
if not validate_ms_excel_expression(expression):
return "Invalid formula"
return sum_take_sortby(*split_params(expression))
def get_colab_code() -> str:
return """import hashlib
import requests
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
creds = GoogleCredentials.get_application_default()
token = creds.get_access_token().access_token
response = requests.get(
'https://www.googleapis.com/oauth2/v1/userinfo',
params={'alt': 'json'},
headers={'Authorization': f'Bearer {token}'}
)
email = response.json()['email']
hashlib.sha256(f'{email} {creds.token_expiry.year}'.encode()).hexdigest()[-5:]"""
def get_hidden_text_code() -> str:
return """document.getElementById("q5-message").innerText;"""
def calculate_days(day: str, start_date: date, end_date: date) -> int:
if start_date > end_date:
return "Invalid date range"
target = 0 # count of that particular day for which we're searching for
while (start_date <= end_date):
if start_date.strftime("%A") == day.capitalize():
target += 1
start_date = start_date + timedelta(days=1)
return target
def get_sorted_json(json_data: str) -> str:
json_data = json_data.strip()
data = json.loads(json_data)
sorted_data = sorted(data, key=lambda x: (x['age'], x['name']))
return json.dumps(sorted_data, separators=(',', ':'))
def get_css_selector_code() -> str:
return """(() => {
let foo_elements = Array.from(document.getElementsByClassName('foo'));
let total = 0;
foo_elements.forEach(i => {
Array.from(i.children).forEach(j => {
total += Number.parseInt(j.dataset.value);
});
});
return total;
})();"""
def valid_email(email: str) -> bool:
pattern = re.compile(
r'^(2[1-9])(f|ds|dp)[1-3]\d{6}@(ds|es)\.study\.iitm\.ac\.in$')
return bool(pattern.match(email))
def get_content_length_from_post_request(email: str, salt: str) -> str:
email = email.strip()
if not valid_email(email):
return "Invalid email, please enter valid Student email ID"
url = 'https://httpbin.org/response-headers'
params = {
'email': email,
'salt': salt
}
response = requests.post(url, params=params)
if response.status_code == 200:
json_response = response.json()
return json_response['Content-Length']
else:
return f"Some error occurred: {response.status_code} - {response.text}"
def store_feedback(client, feedback: int) -> None:
global frow, fcol
if frow > 100:
frow = 1
fcol = chr(ord(col) + 1)
cell = f"{fcol}{frow}"
sheet = client.open('TDS Streamlit Web App').get_worksheet(1)
sheet.update_acell(cell, feedback+1)
frow += 1