@@ -38,13 +38,10 @@ def _generate_charset(n):
3838
3939def _get_charset (charset , p = "" ):
4040 """
41- Charaters set selection function. It allows to define charsets in many
42- different ways.
41+ Characters set selection function. It allows to define charsets in many different ways.
4342
44- :param charset: charset object, can be a string (the charset itself), a
45- function (that chooses the right charset depending on the
46- input parameter) or a dictionary (either by exact key or by
47- pattern matching)
43+ :param charset: charset object, can be a string (the charset itself), a function (that chooses the right charset
44+ depending on the input parameter) or a dictionary (either by exact key or by pattern matching)
4845 :param p: the parameter for choosing the charset
4946 """
5047 # case 1: charset is a function, so return its result
@@ -53,12 +50,11 @@ def _get_charset(charset, p=""):
5350 # case 2: charset is a string, so return it
5451 elif isinstance (charset , string_types ):
5552 return charset
56- # case 3: charset is a dict with keys '' and 'inv', typically for a charset
57- # using lowercase and uppercase characters that can be inverted
53+ # case 3: charset is a dict with keys '' and 'inv', typically for a charset using lowercase and uppercase characters
54+ # that can be inverted
5855 elif isinstance (charset , dict ) and list (charset .keys ()) == ["" , "inv" ]:
5956 return charset ["inv" if re .match (r"[-_]inv(erted)?$" , p ) else "" ]
60- # case 4: charset is a dict, but not with the specific keys '' and 'inv', so
61- # consider it as pattern-charset pairs
57+ # case 4: charset is a dict, but not with the specific keys '' and 'inv', so consider it as pattern-charset pairs
6258 elif isinstance (charset , dict ):
6359 # try to handle [p]arameter as a simple key
6460 try :
@@ -74,8 +70,7 @@ def _get_charset(charset, p=""):
7470 continue
7571 if re .match (pattern , p ):
7672 return cset
77- # special case: the given [p]arameter can be the charset itself if
78- # it has the right length
73+ # special case: the given [p]arameter can be the charset itself if it has the right length
7974 p = re .sub (r"^[-_]+" , "" , p )
8075 if len (p ) == n :
8176 return p
@@ -119,23 +114,21 @@ def base_decode(input, charset, errors="strict", exc=BaseEncodeError):
119114 i = i * n + charset .index (c )
120115 except ValueError :
121116 if errors == "strict" :
122- raise exc ("'base' codec can't decode character '{}' in position"
123- " {}" .format (c , k ))
117+ raise exc ("'base' codec can't decode character '{}' in position {}" .format (c , k ))
124118 elif errors in ["ignore" , "replace" ]:
125119 continue
126120 else :
127121 raise ValueError ("Unsupported error handling {}" .format (errors ))
128122 return base_encode (i , [chr (j ) for j in range (256 )], errors , exc )
129123
130124
131- def base (charset , pattern = None , pow2 = False ,
132- encode_template = base_encode , decode_template = base_decode ):
125+ def base (charset , pattern = None , pow2 = False , encode_template = base_encode , decode_template = base_decode ):
133126 """
134127 Base-N codec factory.
135128
136129 :param charset: charset selection function
137- :param pattern: matching pattern for the codec name (first capturing group
138- is used as the parameter for selecting the charset)
130+ :param pattern: matching pattern for the codec name (first capturing group is used as the parameter for selecting
131+ the charset)
139132 :param pow2: whether the base codec's N is a power of 2
140133 """
141134 is_n = isinstance (charset , int )
0 commit comments