comparison cutils/util/__init__.py @ 379:6d7659a709f2

FIX: treesum: str.translate() behaves differently in Python3: make it PY3-compatible
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 12 May 2025 12:12:44 +0200
parents 54a6d4534ef4
children
comparison
equal deleted inserted replaced
378:32b937a73068 379:6d7659a709f2
11 11
12 12
13 __all__ = ["PY2", 13 __all__ = ["PY2",
14 "PY35", 14 "PY35",
15 "n", "b", "u", 15 "n", "b", "u",
16 "parse_grouped_decimal_number",
16 "normalize_filename", 17 "normalize_filename",
17 "escape_for_output", 18 "escape_for_output",
18 "argv2algo", 19 "argv2algo",
19 "algotag2algotype", 20 "algotag2algotype",
20 "algotag2digest_size", 21 "algotag2digest_size",
55 """Convert `s` to a unicode string""" 56 """Convert `s` to a unicode string"""
56 if isinstance(s, str): 57 if isinstance(s, str):
57 return s.decode(encoding) 58 return s.decode(encoding)
58 return s 59 return s
59 60
61 def parse_grouped_decimal_number(s):
62 return int(n(s).translate(None, "., '_"), 10)
63
60 else: 64 else:
61 65
62 def n(s, encoding="ascii"): 66 def n(s, encoding="ascii"):
63 """Convert `s` to the native string implementation""" 67 """Convert `s` to the native string implementation"""
64 if isinstance(s, (bytes, bytearray)): 68 if isinstance(s, (bytes, bytearray)):
70 if isinstance(s, str): 74 if isinstance(s, str):
71 return s.encode(encoding, errors) 75 return s.encode(encoding, errors)
72 return s 76 return s
73 77
74 u = n 78 u = n
79
80 def parse_grouped_decimal_number(s):
81 return int(n(s).translate(NORMALIZATION_DELETE_CHARS), 10)
82
83 NORMALIZATION_DELETE_CHARS = {
84 ord('.'): None,
85 ord(','): None,
86 ord(' '): None,
87 ord("'"): None,
88 ord('_'): None
89 }
75 90
76 91
77 def escape_for_output(what): 92 def escape_for_output(what):
78 """Escape `what` in such a way that the output can be safely written into 93 """Escape `what` in such a way that the output can be safely written into
79 a line and/or column-oriented output file 94 a line and/or column-oriented output file