-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathmain.py
426 lines (322 loc) · 8.71 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import collections
import dataclasses
import gzip
import io
import json
import math
import pathlib
import re
import typing
from html.parser import HTMLParser
import numpy as np
# 1
def manually_rounding_in_print():
t = 1.23456
print(f"Finished in {t}s")
print(f"Finished in {round(t, 2)}s")
print(f"Finished in {t:.2f}s")
# 2
def repeatedly_converting_to_from_numpy_arrays():
nums = list(range(256 * 256 * 256))
arr = np.array(nums) # 1.01s
m = max(nums) # .16s
m = np.max(arr) # .01s
m = arr.max() # .01s
m = max(arr) # .73s
# 3
def manipulating_paths_as_strings():
path = "path/to/data/my_data.json"
zipped_file = path.removesuffix(".json") + ".zip"
data_dir = "/".join(path.split("/")[-2])
other_file = f"{data_dir}/other_file.txt"
deeper_dir = f"{data_dir}/abc/def"
path = pathlib.Path("path/to/data/my_data.json")
zipped_file = path.with_suffix(".zip")
data_dir = path.parent
other_file = path.with_name("other_file.txt")
deeper_dir = data_dir.joinpath("abc", "def")
# also os.path but pathlib is preferred
# 4
def do_io_taking_path(path: str):
with open(path, "w") as fp:
fp.write("...")
# do_io_taking_io(fp)
def do_io_taking_io(fp: typing.TextIO):
fp.write("...")
def calls_do_io_with_gzip_io():
with gzip.open("example.txt.gz", "wt") as fp:
do_io_taking_io(fp)
with gzip.open("example.txt.gz", "rt") as fp:
assert fp.read() == "..."
# 5
def concatenating_strings_with_plus():
s = ""
for i in range(100):
s += f"some string {i}"
ss = io.StringIO()
for i in range(100):
ss.write(f"some string {i}")
s = ss.getvalue()
lines = []
for i in range(100):
lines.append(f"some string {i}")
s = "\n".join(lines)
return s
# 6
def using_eval_as_a_parser():
data_str = '{"a":1, "b":2, "c":3}'
data = eval(data_str)
data = json.loads(data_str)
with open("file_that_data_str_came_from.txt") as fp:
data = json.load(fp)
print(data)
# pydantic...
# 7
strict = True
def storing_inputs_and_or_outputs_as_globals():
for i in range(100):
if strict:
...
else:
...
global ans
ans = ...
# SELF PROMO
# 8
def thinking_and_or_return_bools():
a = {"a": 1, "b": 2, "c": 3}
b = [1, 2, 3]
print(a or b) # {"a": 1, "b": 2, "c": 3}
print(a and b) # [1, 2, 3]
print({} or []) # []
print({} and []) # {}
# or: first true one or last false one
# and: first false one or last true one
cond = a or b
if cond == True:
print("cond is true")
elif cond:
print("cond is truthy")
else:
print("cond is falsey")
# 9
def single_letter_variables():
for i in range(100): # OK
...
for idx in range(100): # easier to ctrl+f for idx
...
_ = "unused OK"
x, y, z = (1, 2, 3) # OK
a0, r, t = 1.0, .01, 1.0
a = a0 * math.exp(r * t)
# Please use names
p = "data.txt"
with open(p) as f:
for l in f:
s = l.split()
t, u = s[0], s[-1]
ti, ui = int(t), int(u)
d = ui - ti
...
# with open(p) as fp:
# for line in fp:
# tokens = line.split()
# first_token, last_token = tokens[0], tokens[-1]
# first_int, last_int = int(first_token), int(last_token)
# diff = last_int - first_int
# ...
# 10
def using_div_and_mod_instead_of_divmod(x, p):
q, r = x // p, x % p
q, r = divmod(x, p)
if r == 0:
print(f"{p} divides {x} evenly into {q} parts")
else:
print(f"{p} divides {x} into {q} parts with a remainder of {r}")
# 11
class JavaLike:
def __init__(self, x):
self._x = x
def get_x(self):
return self._x
def set_x(self, x):
# ...
self._x = x
@property
def x(self):
return self._x
@x.setter
def x(self, val):
self._x = val
def not_knowing_about_properties():
obj = JavaLike(0)
obj.set_x(42)
print(obj.get_x())
obj.x = 42
print(obj.x)
# 12
class Thingy:
@property
def val(self):
# long computation
...
return 42
def expensive_properties():
thing = Thingy()
val = thing.val # if val is property, looks CHEAP
val = thing.val() # if val is function, looks maybe expensive
# 13
def inserting_or_deleting_while_iterating():
# d = {chr(65+i): i for i in range(10)}
# for key, val in d.items():
# if val % 2 == 0:
# del d[key]
# # d[key] = 42
d = {chr(65 + i): i for i in range(10)}
for key, val in list(d.items()):
if val % 2 == 0:
del d[key]
print(d)
d = {chr(65 + i): i for i in range(10)}
to_delete = set()
for key, val in d.items():
if val % 2 == 0:
to_delete.add(key)
for key in to_delete:
del d[key]
print(d)
# 14
def using_filter_and_map_instead_of_comprehensions():
xs = list(range(10))
odds = filter(lambda x: x % 2 == 1, xs)
squares = map(lambda x: x * x, xs)
odds = (x for x in xs if x % 2 == 1)
squares = (x * x for x in xs)
def func(x):
...
filtered = filter(func, xs)
filtered = (x for x in xs if func(x))
mapped = map(func, xs)
mapped = (func(x) for x in xs)
filtered = list(filter(func, xs))
filtered = [x for x in xs if func(x)]
mapped = list(map(func, xs))
mapped = [func(x) for x in xs]
# 15
def defining_too_many_dunders():
class Person:
def __init__(self, name: str, friends: set):
self.name = name
self.friends = friends
def __hash__(self): # fine
return hash(self.name)
def __iadd__(self, other): # why?
self.friends.add(other)
other.friends.add(self)
return self
def add_friend(self, other):
self.friends.add(other)
other.friends.add(self)
p1 = Person("James", set())
p2 = Person("Other James", set())
p1 += p2 # friends!
p1.add_friend(p2)
# 16
def trying_to_parse_html_or_xml_using_regex():
html = """
<html>
<body>
<a href="https://mcoding.io">Great website</a>
</body>
</html>
"""
links_regex = '<a href="(.*?)"'
for match in re.finditer(links_regex, html):
print(f"Found link: {match.group(1)}")
class UrlParser(HTMLParser):
def handle_starttag(self, tag: str, attrs):
if tag != "a":
return
for attr, val in attrs:
if attr == "href":
print(f"Found link: {val}")
break
UrlParser().feed(html)
# or use BeautifulSoup...
# 17
def not_knowing_about_raw_strings():
some_path = "windows\\path\\to\\file.txt"
some_path = r"c:\path\to\file.txt"
some_regex = "\\d+\\.\\d*"
some_regex = r"\d+\.\d*"
val = 42
interpolated = fr"\\ {val} //"
print(interpolated)
# gotcha = r"can't end in backslash \" # SyntaxError
print(gotcha)
# 18
def thinking_super_means_parent():
class Root:
def f(self):
print("Root.f")
class A(Root):
def f(self):
print("A.f")
super().f()
class B(Root):
def f(self):
print("B.f")
super().f()
class C(A, B):
def f(self):
print("C.f")
super().f()
C().f()
# C.f
# A.f
# B.f
# Root.f
print([cls.__name__ for cls in C.__mro__]) # C, A, B, Root, object
# 19
@dataclasses.dataclass
class Measurement:
value: float
timestamp: float
location: tuple[float, float]
error_estimate: tuple[float, float]
class Measurement(typing.NamedTuple):
value: float
timestamp: float
location: tuple[float, float]
error_estimate: tuple[float, float]
class Measurement(typing.TypedDict):
value: float
timestamp: float
location: tuple[float, float]
error_estimate: tuple[float, float]
def passing_structured_data_as_dict_or_tuple():
# take some measurement
measurement = 1.0001
timestamp = ...
location = ...
error_estimate = ...
data = {
"measurement": measurement,
"timestamp": timestamp,
"location": location,
"error_estimate": error_estimate,
}
data = (measurement, timestamp, location, error_estimate)
return data
# 20
def using_namedtuple_instead_of_NamedTuple():
Point = collections.namedtuple("Point", ["x", "y", "z"])
p = Point(1, 2, 3)
print(p.x + p.y + p.z)
class Point(typing.NamedTuple):
x: float
y: float
z: float
p = Point(1, 2, 3)
print(p.x + p.y + p.z)
# 21. import time side effects