-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
56 lines (45 loc) · 1.56 KB
/
helpers.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
import re
import requests
import traceback
def verbose_method(method):
"""Wrapper to print the method name, arguments, and result of a call"""
def _method(self, *args):
arg_str = ', '.join(repr(a) for a in args)
try:
result = method(self, *args)
arrow = "=>"
except Exception as e:
result = repr(e)
arrow = "=X"
raise
finally:
print(f'call: {self!r}.{method.__name__}({arg_str}) {arrow} {result!r}')
return result
return _method
def get_xkcd_hotlink(comic_number):
"""Extract the embedded URL for an XKCD comic (no magic here)"""
match = re.search(
r"Image URL \(for hotlinking/embedding\):\s*(?P<url>http.*(jpg|png))",
requests.get(f"https://xkcd.com/{comic_number}/").text,
)
if match:
return match.groupdict()["url"]
def get_xkcd_comic_info(comic_number):
pass
class expect_except:
"""
Expect an exception and trap it. Helps run all of a notebook
that may have intentional errors for pedagogical reasons
"""
def __init__(self, *exc_types):
if not exc_types:
raise ValueError("at least one exception must be provided")
self.exc_types = exc_types
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
if exc_type is None:
raise RuntimeError('expected exception did not occur!')
if issubclass(exc_type, self.exc_types):
traceback.print_exception(exc_type, exc_value, tb)
return True