Skip to content

Commit b325314

Browse files
committed
Fixed minor issues
1 parent b186a96 commit b325314

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

codext/__common__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,9 +1303,13 @@ class _Text(object):
13031303
__slots__ = ["entropy", "lcharset", "len", "padding", "printables"]
13041304

13051305
def __init__(self, text, pad_char=None):
1306+
c = text[-1]
1307+
last_char = c if isinstance(c, int) else ord(c)
1308+
self.padding = pad_char is not None and last_char == ord(pad_char)
1309+
if self.padding:
1310+
text = text.rstrip(pad_char)
13061311
self.len = len(text)
13071312
self.lcharset = len(set(text))
1308-
self.padding = pad_char is not None and text[-1] in [pad_char, b(pad_char)]
13091313
self.printables = float(len([c for c in text if (chr(c) if isinstance(c, int) else c) in printable])) / self.len
13101314
self.entropy = entropy(text)
13111315

@@ -1363,16 +1367,16 @@ def __score(prev_input, input, prev_encoding, codec, heuristic=False, extended=F
13631367
s += .1
13641368
expf = sc.get('expansion_factor', 1.)
13651369
if expf:
1366-
f = float(len(new_input)) / obj.len
1370+
f = obj.len / float(len(new_input)) # expansion while encoding => at decoding: 1/f
13671371
if isinstance(expf, type(lambda: None)):
13681372
try: # this case allows to consider the current encoding name from the current codec
13691373
expf = expf(f, encoding)
13701374
except TypeError:
13711375
expf = expf(f)
13721376
if isinstance(expf, (int, float)):
1373-
expf = (f - .1 <= expf <= f + .1)
1377+
expf = (1/f - .1 <= 1/expf <= 1/f + .1)
13741378
elif isinstance(expf, (tuple, list)) and len(expf) == 2:
1375-
expf = f - expf[1] <= expf[0] <= expf[1] + .1
1379+
expf = 1/f - expf[1] <= 1/expf[0] <= 1/f + expf[1]
13761380
s += [-1., .1][expf]
13771381
# afterwards, if the input text has an entropy close to the expected one, give a bonus weighted on the
13781382
# number of input characters to take bad entropies of shorter strings into account

codext/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def main():
8888
"echo -en \"test\" | codext encode base64 gzip | codext guess gzip -c base",
8989
])
9090
parser = argparse.ArgumentParser(description=descr, epilog=examples, formatter_class=argparse.RawTextHelpFormatter)
91-
sparsers = parser.add_subparsers(dest="command", help="command to be executed")
91+
sparsers = parser.add_subparsers(dest="command", required=True, help="command to be executed")
9292
parser.add_argument("-i", "--input-file", dest="infile", help="input file (if none, take stdin as input)")
9393
parser.add_argument("-o", "--output-file", dest="outfile", help="output file (if none, display result to stdout)")
9494
parser.add_argument("-s", "--strip-newlines", action="store_true", dest="strip",
@@ -140,7 +140,7 @@ def main():
140140
search = sparsers.add_parser("search", help="search for codecs")
141141
search.add_argument("pattern", nargs="+", help="encoding pattern to search")
142142
listi = sparsers.add_parser("list", help="list items")
143-
lsparsers = listi.add_subparsers(dest="type", help="type of item to be listed")
143+
lsparsers = listi.add_subparsers(dest="type", required=True, help="type of item to be listed")
144144
liste = lsparsers.add_parser("encodings", help="list encodings")
145145
liste.add_argument("category", nargs="*", help="selected categories")
146146
listm = lsparsers.add_parser("macros", help="list macros")

0 commit comments

Comments
 (0)