Skip to content

Commit 75205dc

Browse files
committed
Minor changes
1 parent 0206f66 commit 75205dc

5 files changed

Lines changed: 21 additions & 14 deletions

File tree

codext/compressions/gzipp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
from ..__common__ import *
1616

1717

18-
__examples__ = {'enc-dec(gzip)': ["test", "This is a test"]}
18+
__examples__ = {'enc-dec(gzip)': ["test", "This is a test", "@random{512,1024,2048}"]}
1919

2020

21-
def gzip_encode(text, errors="strict"):
21+
def gzip_compress(text, errors="strict"):
2222
out = BytesIO()
2323
with GzipFile(fileobj=out, mode="wb") as f:
2424
f.write(b(text))
2525
return out.getvalue(), len(text)
2626

2727

28-
def gzip_decode(data, errors="strict"):
28+
def gzip_decompress(data, errors="strict"):
2929
# then try decompressing considering the file signature
3030
try:
3131
with GzipFile(fileobj=BytesIO(b(data)), mode="rb") as f:
@@ -40,5 +40,5 @@ def gzip_decode(data, errors="strict"):
4040
return r, len(r)
4141

4242

43-
add("gzip", gzip_encode, gzip_decode, entropy=7.9)
43+
add("gzip", gzip_compress, gzip_decompress, entropy=7.9)
4444

codext/compressions/lz77.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ..__common__ import *
1515

1616

17-
__examples__ = {'enc-dec(lz77)': ["test", "This is a test", "@random{1024}"]}
17+
__examples__ = {'enc-dec(lz77)': ["test", "This is a test", "@random{512,1024,2048}"]}
1818

1919

2020
_B2b = lambda B: bin(B if isinstance(B, int) else ord(B))[2:].zfill(8)

codext/compressions/lz78.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
from ..__common__ import *
1515

1616

17-
__examples__ = {'enc-dec(lz78)': ["test", "This is a test", "@random{1024}"]}
17+
__examples__ = {'enc-dec(lz78)': ["test", "This is a test", "@random{512,1024,2048}"]}
1818

1919

2020
def lz78_compress(input, errors="strict"):
2121
""" Compresses the given data by applying LZ78 compression algorithm. """
2222
data = tuple(c if isinstance(c, int) else ord(c) for c in input)
23+
if len(data) == 0:
24+
return "", 0
2325
out = (data[0], )
2426
d = {tuple(): (0, ), (data[0], ): (1, )}
2527
a, b, ctr = 1, 1, [2]
@@ -41,14 +43,16 @@ def lz78_compress(input, errors="strict"):
4143
if data[a:b] in d and a != b:
4244
w = tuple(d[data[a:b]])
4345
out += w + tuple(0 for i in range(len(ctr) - len(w)))
44-
return bytes(out), len(out)
46+
return "".join(chr(i) for i in out), len(out)
4547

4648

4749
def lz78_decompress(input, errors="strict"):
4850
""" Decompresses the given data. """
4951
data = tuple(c if isinstance(c, int) else ord(c) for c in input)
52+
if len(data) == 0:
53+
return "", 0
5054
out = (data[0], )
51-
l = [tuple(), (data[0], )]
55+
l = [tuple(), out]
5256
a, b, c, i, char = 1, 1, 256, 0, False
5357
try:
5458
while a < len(data):
@@ -69,8 +73,8 @@ def lz78_decompress(input, errors="strict"):
6973
a += b
7074
char = True
7175
except:
72-
return handle_error("lz78", errors, decode=True)(data[a], a), len(input)
73-
return bytes(out), len(out)
76+
return handle_error("lz78", errors, decode=True)(chr(data[a]), a), len(input)
77+
return "".join(chr(i) for i in out), len(out)
7478

7579

7680
add("lz78", lz78_compress, lz78_decompress)

codext/compressions/pkzip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ..__common__ import *
1515

1616

17-
_str = ["test", "This is a test", "@random{1024}"]
17+
_str = ["test", "This is a test", "@random{512,1024,2048}"]
1818
__examples1__ = {'enc-dec(pkzip-deflate|deflate)': _str}
1919
__examples2__ = {'enc-dec(pkzip_bz2|bzip2)': _str}
2020
__examples3__ = {'enc-dec(pkzip-lzma|lzma)': _str}

tests/test_generated.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ def _template(self):
7171
params.get('repl_minlen', 1) * params['repl_char'])
7272
# examples validation tests
7373
if k.startswith("enc-dec") and isinstance(examples, list):
74-
for s in examples:
75-
rd = re.match(r"\@random(?:\{(\d+)\})?$", s)
74+
for e in examples[:]:
75+
rd = re.match(r"\@random(?:\{(\d+(?:,(\d+))*?)\})?$", e)
7676
if rd:
77-
s = "".join(chr(randint(0, 255)) for i in range(int(rd.group(1) or "512")))
77+
examples.remove(e)
78+
for n in (rd.group(1) or "512").split(","):
79+
examples.append("".join(chr(randint(0, 255)) for i in range(int(n))))
80+
for s in [""] + examples:
7881
self.assertEqual(icdec(f2(icenc(f1(s, ename)), ename)), icdec(s))
7982
self.assertEqual(icdec(f2(icenc(f1(b(s), ename)), ename)), b(icdec(s)))
8083
# file tests

0 commit comments

Comments
 (0)