Skip to content

Commit eb649a5

Browse files
committed
Apply reviewer requests: use is_float, remove extra comments, etc.
1 parent 9819acf commit eb649a5

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

pandas/io/formats/printing.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

251246
def pprint_thing_encoded(

pandas/tests/io/formats/test_printing.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Note! This file is aimed specifically at pandas.io.formats.printing utility
2-
# functions, not the general printing of pandas objects.
2+
# Note! This file is aimed specifically at pandas.io.formats.printing utility
3+
# functions, not the general printing of pandas objects
34
from collections.abc import Mapping
45
import string
56
import pytest
@@ -153,13 +154,13 @@ def just(x, *args, **kwargs):
153154
def test_east_asian_len(self):
154155
adj = printing._EastAsianTextAdjustment()
155156

156-
assert adj.len("abc") == 3
157-
assert adj.len("abc") == 3
157+
assert adj.len("パンダ") == 11
158+
assert adj.len("パンダ") == 10
158159

159-
assert adj.len("パンダ") == 6
160-
assert adj.len("パンダ") == 5
161-
assert adj.len("パンダpanda") == 11
162-
assert adj.len("パンダpanda") == 10
160+
class TestPPrintThing:
161+
def test_real_precision(self):
162+
with option_context("display.precision", 3):
163+
assert printing.print_thing(3.14159265359) == "3.142"
163164

164165
def test_ambiguous_width(self):
165166
adj = printing._EastAsianTextAdjustment()

0 commit comments

Comments
 (0)