-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
221 lines (146 loc) · 4.9 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Coding Challenges
import csv
import os
from zipfile import ZipFile
import re
import urllib.parse
import urllib.request
from typing import Callable
def merge_csv(csv_list, output_path):
"""Merge csv with similar but different headers"""
fieldnames = list()
for file in csv_list:
with open(file, "r") as input_csv:
fn = csv.DictReader(input_csv).fieldnames
fieldnames.extend(x for x in fn if x not in fieldnames)
with open(output_path, "w", newline="") as output_csv:
writer = csv.DictWriter(output_csv, fieldnames=fieldnames)
writer.writeheader()
for file in csv_list:
with open(file, "r") as input_csv:
reader = csv.DictReader(input_csv)
for row in reader:
writer.writerow(row)
# Zip Archive
def zip_all(search_dir, extension_list, output_path):
with ZipFile(output_path, "w") as output_zip:
for root, dirs, files in os.walk(search_dir):
rel_path = os.path.relpath(root, search_dir)
for file in files:
name, ext = os.path.splitext(file)
if ext.lower() in extension_list:
output_zip.write(
os.path.join(root, file),
arcname=os.path.join(rel_path, file),
)
# Download Sequential files
def download_files(first_url, output_dir):
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
url_head, url_tail = os.path.split(first_url)
first_index = re.findall(r"[0-9+", url_tail)[-1]
index_count, error_count = 0, 0
while error_count < 5:
next_index = str(int(first_index) + index_count)
if first_index[0] == "0": # zero padded
next_index = (
"0" * (len(first_index) - len(next_index)) + next_index
)
next_url = urllib.parse.urljoin(
url_head, re.sub(first_index, next_index, url_tail)
)
try:
output_file = os.path.join(output_dir, os.path.basename(next_url))
urllib.request.urlretrieve(next_url, output_file)
print(f"Successfully downloaded {os.path.basename(next_index)}")
except IOError:
print(f"Could not retrieve {next_url}")
error_count += 1
index_count += 1
def palindrome(input_str):
forwards = "".join(re.findall(r"[a-z]+", input_str.lower()))
return forwards[:] == forwards[::-1]
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/")
async def read_items(session_id: Annotated[str | None, Cookie()] = None):
return {"session_id": session_id}
from typing import Callable
def process_action(func: Callable) -> None:
print(f"NAME: {func.__name__} function.\n")
def is_valid_age(age: int) -> bool:
if age >= 21:
return True
else:
return False
# optimized version
def is_valid_age_op(age: int) -> bool:
return age >= 21
def cal_sum(n) -> int:
result = 0
for i in range(n):
result += i
return result
def cal_sum_ops(n) -> int:
return (n * (n-1))//2
# return sum([range(n)])
import time
def get_time(func: Callable, *args: int) -> None:
start: float = time.perf_counter()
func(*args)
end: float = time.perf_counter()
print(f'"{func.__name__}()" took: {end - start:.5f} seconds')
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/")
async def read_items(session_id: Annotated[str | None, Cookie()] = None):
return {"session_id": session_id}
from typing import Callable
def process_action(func: Callable) -> None:
print(f"NAME: {func.__name__} function.\n")
def is_valid_age(age: int) -> bool:
if age >= 21:
return True
else:
return False
# optimized version
def is_valid_age_op(age: int) -> bool:
return age >= 21
def cal_sum(n) -> int:
result = 0
for i in range(n):
result += i
return result
def cal_sum_ops(n) -> int:
return (n * (n-1))//2
# return sum([range(n)])
import time
def get_time(func: Callable, *args: int) -> None:
start: float = time.perf_counter()
func(*args)
end: float = time.perf_counter()
print(f'"{func.__name__}()" took: {end - start:.5f} seconds')
def process_action(func: Callable) -> None:
print(f"NAME: {func.__name__} function.\n")
def is_valid_age(age: int) -> bool:
if age >= 21:
return True
else:
return False
# optimized version
def is_valid_age_op(age: int) -> bool:
return age >= 21
def cal_sum(n) -> int:
result = 0
for i in range(n):
result += i
return result
def cal_sum_ops(n) -> int:
return (n * (n-1))//2
# return sum([range(n)])
import time
def get_time(func: Callable, *args: int) -> None:
start: float = time.perf_counter()
func(*args)
end: float = time.perf_counter()
print(f'"{func.__name__}()" took: {end - start:.5f} seconds')