Skip to content

Commit 618bb44

Browse files
committed
python: reformat
1 parent 06cd10d commit 618bb44

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

python/graphics.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@
99

1010

1111
class Browser:
12-
1312
def __init__(self):
1413
self.window = tkinter.Tk()
1514
self.scroll = 0
1615
self.min_scroll = 0
1716
self.max_scroll = 0
18-
self.window.title('Browser-engineering')
17+
self.window.title("Browser-engineering")
1918
self.window.bind("<Up>", self.scrollup)
2019
self.window.bind("<Down>", self.scrolldown)
2120
self.window.bind("<MouseWheel>", self.mousewheel)
22-
self.canvas = tkinter.Canvas(
23-
self.window,
24-
width=WIDTH,
25-
height=HEIGHT
26-
)
21+
self.canvas = tkinter.Canvas(self.window, width=WIDTH, height=HEIGHT)
2722
self.canvas.pack()
2823

2924
def load(self, url):
@@ -39,7 +34,7 @@ def layout(self, text):
3934
self.max_scroll = max(self.max_scroll, cursor_y)
4035
display_list.append((cursor_x, cursor_y, c))
4136
cursor_x += HSTEP
42-
if cursor_x >= WIDTH - HSTEP or c == '\n':
37+
if cursor_x >= WIDTH - HSTEP or c == "\n":
4338
cursor_y += VSTEP
4439
cursor_x = HSTEP
4540
return display_list
@@ -74,8 +69,8 @@ def mousewheel(self, e):
7469
self.render()
7570

7671

77-
78-
if __name__ == '__main__':
72+
if __name__ == "__main__":
7973
import sys
74+
8075
Browser().load(sys.argv[1])
8176
tkinter.mainloop()

python/http.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import socket
33
import ssl
44
import zlib
5+
56
try:
67
import brotli
78
except ImportError:
@@ -10,56 +11,56 @@
1011

1112
def request(url):
1213
# 1. Parse scheme
13-
scheme, url = url.split('://', 1)
14-
assert scheme in ['http', 'https'], f'Unknown scheme {scheme}'
15-
port = 80 if scheme == 'http' else 443
14+
scheme, url = url.split("://", 1)
15+
assert scheme in ["http", "https"], f"Unknown scheme {scheme}"
16+
port = 80 if scheme == "http" else 443
1617

1718
# 2. Parse host
18-
host, path = url.split('/', 1)
19-
path = '/' + path
19+
host, path = url.split("/", 1)
20+
path = "/" + path
2021

2122
# 3. Parse port
22-
if ':' in host:
23-
host, port = host.split(':', 1)
23+
if ":" in host:
24+
host, port = host.split(":", 1)
2425
port = int(port)
2526

2627
# 4. Connect
2728
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as s:
28-
if scheme == 'https':
29+
if scheme == "https":
2930
ctx = ssl.create_default_context()
3031
s = ctx.wrap_socket(s, server_hostname=host)
3132

3233
s.connect((host, port))
3334

3435
# 5. Send request
35-
s.send(f'GET {path} HTTP/1.0\r\n'.encode())
36-
s.send(f'Host: {host}\r\n'.encode())
37-
s.send('Accept-Encoding: br,gzip,deflate\r\n'.encode())
38-
s.send('\r\n'.encode())
36+
s.send(f"GET {path} HTTP/1.0\r\n".encode())
37+
s.send(f"Host: {host}\r\n".encode())
38+
s.send("Accept-Encoding: br,gzip,deflate\r\n".encode())
39+
s.send("\r\n".encode())
3940

4041
# 6. Receive response
41-
response = s.makefile('rb', newline='\r\n')
42+
response = s.makefile("rb", newline="\r\n")
4243

4344
# 7. Read status line
4445
line = response.readline().decode()
4546
# 8. Parse status line
46-
version, status, explanation = line.split(' ', 2)
47+
version, status, explanation = line.split(" ", 2)
4748

4849
# 9. Check status
49-
assert status == "200", f'{status}: {explanation}'
50+
assert status == "200", f"{status}: {explanation}"
5051

5152
# 10. Parse headers
5253
headers = {}
5354
while True:
5455
line = response.readline().decode()
55-
if line == '\r\n':
56+
if line == "\r\n":
5657
break
57-
header, value = line.split(':', 1)
58+
header, value = line.split(":", 1)
5859
headers[header.lower()] = value.strip()
5960

6061
body = response.read()
61-
if 'content-encoding' in headers:
62-
encoding = headers['content-encoding']
62+
if "content-encoding" in headers:
63+
encoding = headers["content-encoding"]
6364
body = decompress(body, encoding)
6465
body = body.decode()
6566

@@ -68,28 +69,28 @@ def request(url):
6869

6970

7071
def decompress(data, encoding):
71-
if encoding == 'gzip':
72+
if encoding == "gzip":
7273
return gzip.decompress(data)
73-
elif encoding == 'deflate':
74+
elif encoding == "deflate":
7475
return zlib.decompress(data, wbits=-zlib.MAX_WBITS)
75-
elif encoding == 'br':
76+
elif encoding == "br":
7677
if brotli is None:
77-
raise RuntimeError('please install brotli package: pip install brotli')
78+
raise RuntimeError("please install brotli package: pip install brotli")
7879
return brotli.decompress(data)
79-
elif encoding == 'identity':
80+
elif encoding == "identity":
8081
return data
8182
else:
82-
raise RuntimeError(f'unexpected content-encoding: {encoding}')
83+
raise RuntimeError(f"unexpected content-encoding: {encoding}")
8384

8485

8586
def lex(body):
86-
text = ''
87+
text = ""
8788
in_angle = False
8889
for c in body:
89-
if c == '<':
90+
if c == "<":
9091
in_angle = True
91-
elif c == '>':
92+
elif c == ">":
9293
in_angle = False
9394
elif not in_angle:
94-
text += c
95+
text += c
9596
return text

0 commit comments

Comments
 (0)