@@ -199,53 +199,48 @@ def pprint_thing(
199199 str
200200 String representation of the object.
201201 """
202+ # Type alias for escape_chars; adapt as needed
203+ EscapeChars = Union [Mapping [str , str ], Sequence [str ], None ]
204+
202205 def as_escaped_string (
203- thing : Any , escape_chars : EscapeChars | None = escape_chars
206+ thing : Any ,
207+ escape_chars : EscapeChars = None ,
208+ default_escapes : bool = False
204209 ) -> str :
205- translate = {"\t " : r"\t" , "\n " : r"\n" , "\r " : r"\r" , "'" : r"\'" }
210+ """
211+ Convert the given object (`thing`) to a string, applying optional
212+ escape character replacements and respecting display.precision
213+ for float-like values.
214+ """
215+
216+ # Default translation for escape characters
217+ translate = {"\t " : r"\t" , "\n " : r"\n" , "\r \n " : r"\r\n" , "'" : r"\'" }
218+
219+ # Merge custom escape chars with default if needed
206220 if isinstance (escape_chars , Mapping ):
207221 if default_escapes :
208222 translate .update (escape_chars )
209223 else :
210- translate = escape_chars
211- escape_chars = list (escape_chars .keys ())
224+ translate = dict (escape_chars )
225+ # We'll need just the keys when replacing below
226+ keys_to_escape = list (translate .keys ())
212227 else :
228+ # If escape_chars is None or a sequence, set or fallback to empty tuple
213229 escape_chars = escape_chars or ()
230+ keys_to_escape = translate .keys () # default keys
214231
232+ # Check if thing is float-like, apply display.precision
215233 if is_float (thing ):
216- result = f"{ thing :.{get_option ('display.precision' )}f} "
234+ precision = get_option ("display.precision" )
235+ result = f"{ thing :.{precision }f} "
217236 else :
218237 result = str (thing )
219238
220- for c in escape_chars :
239+ # Replace each escape character with its escaped version
240+ for c in keys_to_escape :
221241 result = result .replace (c , translate [c ])
222- return result
223242
224- if hasattr (thing , "__next__" ):
225- return str (thing )
226- elif isinstance (thing , Mapping ) and _nest_lvl < get_option (
227- "display.pprint_nest_depth"
228- ):
229- result = _pprint_dict (
230- thing , _nest_lvl , quote_strings = True , max_seq_items = max_seq_items
231- )
232- elif is_sequence (thing ) and _nest_lvl < get_option ("display.pprint_nest_depth" ):
233- result = _pprint_seq (
234- # error: Argument 1 to "_pprint_seq" has incompatible type "object";
235- # expected "ExtensionArray | ndarray[Any, Any] | Index | Series |
236- # SequenceNotStr[Any] | range"
237- thing , # type: ignore[arg-type]
238- _nest_lvl ,
239- escape_chars = escape_chars ,
240- quote_strings = quote_strings ,
241- max_seq_items = max_seq_items ,
242- )
243- elif isinstance (thing , str ) and quote_strings :
244- result = f"'{ as_escaped_string (thing )} '"
245- else :
246- result = as_escaped_string (thing )
247-
248- return result
243+ return result
249244
250245
251246def pprint_thing_encoded (
0 commit comments