Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c19f997

Browse files
committed
use struct instead of bit-manipulate in Python
1 parent 8ad22c8 commit c19f997

1 file changed

Lines changed: 3 additions & 21 deletions

File tree

Lib/gzip.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import string
33
import zlib
4+
import struct
45
import __builtin__
56

67
# implements a python function that reads and writes a gzipped file
@@ -14,29 +15,10 @@
1415
READ, WRITE = 1, 2
1516

1617
def write32(output, value):
17-
t = divmod(value, 256)
18-
b1 = chr(t[1])
19-
20-
t = divmod(t[0], 256)
21-
b2 = chr(t[1])
22-
23-
t = divmod(t[0], 256)
24-
b3 = chr(t[1])
25-
26-
t = divmod(t[0], 256)
27-
b4 = chr(t[1])
28-
29-
buf = b1 + b2 + b3 + b4
30-
output.write(buf)
31-
18+
output.write(struct.pack("<l", value))
3219

3320
def read32(input):
34-
buf = input.read(4)
35-
v = ord(buf[0])
36-
v = v + (ord(buf[1]) << 8)
37-
v = v + (ord(buf[2]) << 16)
38-
v = v + (ord(buf[3]) << 24)
39-
return v
21+
return struct.unpack("<l", input.read(4))[0]
4022

4123
def open(filename, mode="r", compresslevel=9):
4224
return GzipFile(filename, mode, compresslevel)

0 commit comments

Comments
 (0)