3
3
"""
4
4
5
5
import sys
6
- from typing import Callable , Dict , Iterable , List , Optional , Sequence , Tuple , Union
6
+ from typing import Any , Callable , Dict , Iterable , List , Optional , Sequence , Tuple , Union
7
7
8
8
from pandas ._config import get_option
9
9
10
10
from pandas .core .dtypes .inference import is_sequence
11
11
12
+ EscapeChars = Union [Dict [str , str ], Iterable [str ]]
13
+
12
14
13
15
def adjoin (space : int , * lists : List [str ], ** kwargs ) -> str :
14
16
"""
@@ -148,19 +150,16 @@ def _pprint_dict(
148
150
149
151
150
152
def pprint_thing (
151
- thing ,
153
+ thing : Any ,
152
154
_nest_lvl : int = 0 ,
153
- escape_chars : Optional [Union [ Dict [ str , str ], Iterable [ str ]] ] = None ,
155
+ escape_chars : Optional [EscapeChars ] = None ,
154
156
default_escapes : bool = False ,
155
157
quote_strings : bool = False ,
156
158
max_seq_items : Optional [int ] = None ,
157
159
) -> str :
158
160
"""
159
161
This function is the sanctioned way of converting objects
160
- to a unicode representation.
161
-
162
- properly handles nested sequences containing unicode strings
163
- (unicode(object) does not)
162
+ to a string representation and properly handles nested sequences.
164
163
165
164
Parameters
166
165
----------
@@ -178,21 +177,13 @@ def pprint_thing(
178
177
179
178
Returns
180
179
-------
181
- result - unicode str
180
+ str
182
181
183
182
"""
184
183
185
- def as_escaped_unicode (thing , escape_chars = escape_chars ):
186
- # Unicode is fine, else we try to decode using utf-8 and 'replace'
187
- # if that's not it either, we have no way of knowing and the user
188
- # should deal with it himself.
189
-
190
- try :
191
- result = str (thing ) # we should try this first
192
- except UnicodeDecodeError :
193
- # either utf-8 or we replace errors
194
- result = str (thing ).decode ("utf-8" , "replace" )
195
-
184
+ def as_escaped_string (
185
+ thing : Any , escape_chars : Optional [EscapeChars ] = escape_chars
186
+ ) -> str :
196
187
translate = {"\t " : r"\t" , "\n " : r"\n" , "\r " : r"\r" }
197
188
if isinstance (escape_chars , dict ):
198
189
if default_escapes :
@@ -202,10 +193,11 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
202
193
escape_chars = list (escape_chars .keys ())
203
194
else :
204
195
escape_chars = escape_chars or tuple ()
196
+
197
+ result = str (thing )
205
198
for c in escape_chars :
206
199
result = result .replace (c , translate [c ])
207
-
208
- return str (result )
200
+ return result
209
201
210
202
if hasattr (thing , "__next__" ):
211
203
return str (thing )
@@ -224,11 +216,11 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
224
216
max_seq_items = max_seq_items ,
225
217
)
226
218
elif isinstance (thing , str ) and quote_strings :
227
- result = "'{thing}'" .format (thing = as_escaped_unicode (thing ))
219
+ result = "'{thing}'" .format (thing = as_escaped_string (thing ))
228
220
else :
229
- result = as_escaped_unicode (thing )
221
+ result = as_escaped_string (thing )
230
222
231
- return str ( result ) # always unicode
223
+ return result
232
224
233
225
234
226
def pprint_thing_encoded (
0 commit comments