2 This module provides utlilities related to converting to/from base36 encoding.
8 This exception is raised when custom character set length .
16 This exception is raised when more than exactly one instance of a given field is contained within field list.
24 This exception is raised when encountering a character not defined in custom charset in input string.
32 This exception is raised when non-positive integer is passed to encode function.
40 This exception is raised when encountering an integer over maximum size of 16 bytes.
48 This class provides methods allowing conversion between integer, hexadecimal integer and base36 strings.
54 def __init__(self, custom_charset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
58 @param custom_charset custom character set to use with the coder
60 @throws InvalidCustomCharsetLengthException
61 @throws InvalidCustomCharsetException
63 if len(custom_charset) != 36:
65 "Custom charset is not exactly 36 characters long")
69 for x
in custom_charset:
74 idxf = custom_charset.find(x)
76 "Character '%s' at position %s, first appeared at position %s is repeated in custom charset" % (x, idx, idxf))
82 Converts custom charset to standard charset
84 @param unformatted Base36 string in custom charset encoding
86 @returns Base36 string in standardized format
88 @throws InvalidInputStringException
90 std_charset =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
93 for symbol
in unformatted:
98 "Unexpected '%s' in input string." % symbol)
100 formatted += std_charset[idx]
106 Converts standard charset to custom charset
108 @param unformatted Base36 string in standard charset encoding
110 @returns Base36 string in custom format
112 @throws InvalidInputStringException
114 std_charset =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
117 for symbol
in unformatted:
118 idx = std_charset.find(symbol)
122 "Unexpected '%s' in input string." % symbol)
130 Converts bytes to base36 string in custom charset encoding
132 @param bytes_in a bytes object to encode
134 @returns Base36 string in custom format
136 integer = int.from_bytes(bytes_in,
'big')
142 integer, i = divmod(integer, len(self.
custom_charsetcustom_charset))
149 Converts int to base36 string in custom charset encoding
151 @param integer an int to encode
153 @returns Base36 string in custom format
155 @throws NumberNotPositiveOrZeroException
159 "'%s' is not a positive or integer or zero." % integer)
166 integer, i = divmod(integer, len(self.
custom_charsetcustom_charset))
173 Converts base36 string in custom encoding charset to bytes.
175 @param b36 string in custom encoding charset
177 @returns decoded bytes
179 @throws InvalidInputStringException
180 @throws ValueTooLargeException
185 while length < 16
and bytesdata == b
"":
187 bytesdata = integer.to_bytes(length, byteorder=
'big')
189 except OverflowError:
191 if length > 1
and bytesdata == b
"":
193 "Value '%s' (%s) is too large to convert to bytes (maximum 16 bytes long integer)" % (b36, integer))
198 Converts base36 string in custom encoding charset to integer.
200 @param b36 base36 string in custom encoding charset
202 @returns decoded integer
204 @throws InvalidInputStringException
209 integer = int(b36std, 36)
This class provides methods allowing conversion between integer, hexadecimal integer and base36 strin...
def b36_to_bytes(self, b36)
Converts base36 string in custom encoding charset to bytes.
def bytes_to_b36(self, bytes_in)
Converts bytes to base36 string in custom charset encoding.
def to_standard_charset(self, unformatted)
Converts custom charset to standard charset.
def __init__(self, custom_charset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Initialize coder.
def int_to_b36(self, integer)
Converts int to base36 string in custom charset encoding.
def b36_to_int(self, b36)
Converts base36 string in custom encoding charset to integer.
def to_custom_charset(self, unformatted)
Converts standard charset to custom charset.
This exception is raised when more than exactly one instance of a given field is contained within fie...
This exception is raised when custom character set length .
This exception is raised when non-positive integer is passed to encode function.
This exception is raised when encountering an integer over maximum size of 16 bytes.