Skip to content

Commit 7c155cb

Browse files
committed
draw tree of HTTPX exceptions
1 parent c416116 commit 7c155cb

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
3+
from tree import tree
4+
5+
6+
SP = '\N{SPACE}'
7+
HLIN = '\N{BOX DRAWINGS LIGHT HORIZONTAL}' # ─
8+
ELBOW = f'\N{BOX DRAWINGS LIGHT UP AND RIGHT}{HLIN*2}{SP}' # └──
9+
TEE = f'\N{BOX DRAWINGS LIGHT VERTICAL AND RIGHT}{HLIN*2}{SP}' # ├──
10+
PIPE = f'\N{BOX DRAWINGS LIGHT VERTICAL}{SP*3}' # │
11+
12+
13+
def cls_name(cls):
14+
module = 'builtins.' if cls.__module__ == 'builtins' else ''
15+
return module + cls.__name__
16+
17+
def render_lines(tree_iter):
18+
cls, _, _ = next(tree_iter)
19+
yield cls_name(cls)
20+
prefix = ''
21+
22+
for cls, level, last in tree_iter:
23+
prefix = prefix[:4 * (level-1)]
24+
prefix = prefix.replace(TEE, PIPE).replace(ELBOW, SP*4)
25+
prefix += ELBOW if last else TEE
26+
yield prefix + cls_name(cls)
27+
28+
29+
def draw(cls):
30+
for line in render_lines(tree(cls)):
31+
print(line)
32+
33+
34+
if __name__ == '__main__':
35+
draw(Exception)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
3+
import httpx # make httpx classes available to .__subclasses__()
4+
5+
6+
def tree(cls, level=0, last_sibling=True):
7+
yield cls, level, last_sibling
8+
subclasses = [c for c in cls.__subclasses__()
9+
if c.__module__ == 'httpx' or c is RuntimeError]
10+
if subclasses:
11+
last = subclasses[-1]
12+
for sub_cls in subclasses:
13+
yield from tree(sub_cls, level+1, sub_cls is last)
14+
15+
16+
def display(cls):
17+
for cls, level, _ in tree(cls):
18+
indent = ' ' * 4 * level
19+
module = 'builtins.' if cls.__module__ == 'builtins' else ''
20+
print(f'{indent}{module}{cls.__name__}')
21+
22+
23+
if __name__ == '__main__':
24+
display(Exception)

0 commit comments

Comments
 (0)