Skip to content

Commit

Permalink
repr()
Browse files Browse the repository at this point in the history
  • Loading branch information
MomIsBestFriend committed Dec 2, 2019
1 parent f5c102f commit 0068f04
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 47 deletions.
8 changes: 4 additions & 4 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ cdef class IntervalMixin:
When `other` is not closed exactly the same as self.
"""
if self.closed != other.closed:
msg = f"'{name}.closed' is '{other.closed}', expected '{self.closed}'."
raise ValueError(msg)
raise ValueError(f"'{name}.closed' is {repr(other.closed)}, "
f"expected {repr(self.closed)}.")


cdef _interval_like(other):
Expand Down Expand Up @@ -316,7 +316,7 @@ cdef class Interval(IntervalMixin):
not tz_compare(left.tzinfo, right.tzinfo)):
# GH 18538
msg = (f"left and right must have the same time zone, got "
f"'{left.tzinfo}' and '{right.tzinfo}'")
f"{repr(left.tzinfo)}' and {repr(right.tzinfo)}")
raise ValueError(msg)
self.left = left
self.right = right
Expand Down Expand Up @@ -379,7 +379,7 @@ cdef class Interval(IntervalMixin):

left, right = self._repr_base()
name = type(self).__name__
repr_str = f'{name}({left!r}, {right!r}, closed={self.closed!r})'
repr_str = f'{name}({repr(left)}, {repr(right)}, closed={repr(self.closed)})'
return repr_str

def __str__(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def spmatrix(request):
return getattr(sparse, request.param + "_matrix")


@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: "axis {!r}".format(x))
@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis {repr(x)}")
def axis(request):
"""
Fixture for returning the axis numbers of a DataFrame.
Expand All @@ -99,7 +99,7 @@ def axis(request):
axis_frame = axis


@pytest.fixture(params=[0, "index"], ids=lambda x: "axis {!r}".format(x))
@pytest.fixture(params=[0, "index"], ids=lambda x: f"axis {repr(x)}")
def axis_series(request):
"""
Fixture for returning the axis numbers of a Series.
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ def _register_accessor(name, cls):
def decorator(accessor):
if hasattr(cls, name):
warnings.warn(
"registration of accessor {!r} under name {!r} for type "
"{!r} is overriding a preexisting attribute with the same "
"name.".format(accessor, name, cls),
f"registration of accessor {repr(accessor)} under name "
f"{repr(name)} for type {repr(cls)} is overriding a preexisting"
f"attribute with the same name.",
UserWarning,
stacklevel=2,
)
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,10 +1194,8 @@ def compute(self, method):
dtype = frame[column].dtype
if not self.is_valid_dtype_n_method(dtype):
raise TypeError(
(
"Column {column!r} has dtype {dtype}, cannot use method "
"{method!r} with this dtype"
).format(column=column, dtype=dtype, method=method)
f"Column {repr(column)} has dtype {dtype}, "
f"cannot use method {repr(method)} with this dtype"
)

def get_indexer(current_indexer, other_indexer):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"):
"""
inplace = validate_bool_kwarg(inplace, "inplace")
if na_position not in ["last", "first"]:
raise ValueError(f"invalid na_position: {na_position!r}")
raise ValueError(f"invalid na_position: {repr(na_position)}")

sorted_idx = nargsort(self, ascending=ascending, na_position=na_position)

Expand Down Expand Up @@ -1769,8 +1769,8 @@ def fillna(self, value=None, method=None, limit=None):

else:
raise TypeError(
'"value" parameter must be a scalar, dict '
f'or Series, but you passed a {type(value).__name__!r}"'
f"'value' parameter must be a scalar, dict "
f"or Series, but you passed a {type(value).__name__}"
)

return self._constructor(codes, dtype=self.dtype, fastpath=True)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, dtype):
self._type = dtype.type

def __repr__(self) -> str:
return "PandasDtype({!r})".format(self.name)
return f"PandasDtype({repr(self.name)})"

@property
def numpy_dtype(self):
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def _align_core(terms):
ordm = np.log10(max(1, abs(reindexer_size - term_axis_size)))
if ordm >= 1 and reindexer_size >= 10000:
w = (
"Alignment difference on axis {axis} is larger "
"than an order of magnitude on term {term!r}, by "
"more than {ordm:.4g}; performance may suffer"
).format(axis=axis, term=terms[i].name, ordm=ordm)
f"Alignment difference on axis {axis} is larger "
f"than an order of magnitude on term {repr(terms[i].name)}, "
f"by more than {ordm:.4g}; performance may suffer"
)
warnings.warn(w, category=PerformanceWarning, stacklevel=6)

f = partial(ti.reindex, reindexer, axis=axis, copy=False)
Expand Down
11 changes: 5 additions & 6 deletions pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def _check_engine(engine):
if engine not in _engines:
valid = list(_engines.keys())
raise KeyError(
"Invalid engine {engine!r} passed, valid engines are"
" {valid}".format(engine=engine, valid=valid)
f"Invalid engine {repr(engine)} passed, valid engines are {valid}"
)

# TODO: validate this in a more general way (thinking of future engines
Expand Down Expand Up @@ -82,8 +81,8 @@ def _check_parser(parser: str):

if parser not in _parsers:
raise KeyError(
"Invalid parser {parser!r} passed, valid parsers are"
" {valid}".format(parser=parser, valid=_parsers.keys())
f"Invalid parser {repr(parser)} passed, "
f"valid parsers are {repr(_parsers.keys())}"
)


Expand All @@ -93,8 +92,8 @@ def _check_resolvers(resolvers):
if not hasattr(resolver, "__getitem__"):
name = type(resolver).__name__
raise TypeError(
"Resolver of type {name!r} does not implement "
"the __getitem__ method".format(name=name)
f"Resolver of type {repr(name)} does not "
f"implement the __getitem__ method"
)


Expand Down
4 changes: 1 addition & 3 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ def _node_not_implemented(node_name, cls):
"""

def f(self, *args, **kwargs):
raise NotImplementedError(
"{name!r} nodes are not implemented".format(name=node_name)
)
raise NotImplementedError(f"{repr(node_name)} nodes are not implemented")

return f

Expand Down
10 changes: 4 additions & 6 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,15 @@ def _bool_arith_check(
if _has_bool_dtype(a) and _has_bool_dtype(b):
if op_str in unsupported:
warnings.warn(
"evaluating in Python space because the {op!r} "
"operator is not supported by numexpr for "
"the bool dtype, use {alt_op!r} instead".format(
op=op_str, alt_op=unsupported[op_str]
)
f"evaluating in Python space because the {repr(op_str)} "
f"operator is not supported by numexpr for "
f"the bool dtype, use {repr(unsupported[op_str])} instead"
)
return False

if op_str in not_allowed:
raise NotImplementedError(
"operator {op!r} not implemented for bool dtypes".format(op=op_str)
f"operator {repr(op_str)} not implemented for bool dtypes"
)
return True

Expand Down
19 changes: 8 additions & 11 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ class UndefinedVariableError(NameError):
"""

def __init__(self, name, is_local: bool):
base_msg = f"{repr(name)} is not defined"
if is_local:
msg = "local variable {0!r} is not defined"
msg = f"local variable {base_msg}"
else:
msg = "name {0!r} is not defined"
super().__init__(msg.format(name))
msg = f"name {base_msg}"
super().__init__(msg)


class Term:
Expand Down Expand Up @@ -143,10 +144,7 @@ def type(self):

@property
def raw(self) -> str:
return pprint_thing(
"{0}(name={1!r}, type={2})"
"".format(type(self).__name__, self.name, self.type)
)
return f"{type(self).__name__}(name={repr(self.name)}, type={self.type})"

@property
def is_datetime(self) -> bool:
Expand Down Expand Up @@ -374,8 +372,7 @@ def __init__(self, op: str, lhs, rhs, **kwargs):
# has to be made a list for python3
keys = list(_binary_ops_dict.keys())
raise ValueError(
"Invalid binary operator {0!r}, valid"
" operators are {1}".format(op, keys)
f"Invalid binary operator {repr(op)}, valid operators are {keys}"
)

def __call__(self, env):
Expand Down Expand Up @@ -548,8 +545,8 @@ def __init__(self, op: str, operand):
self.func = _unary_ops_dict[op]
except KeyError:
raise ValueError(
"Invalid unary operator {0!r}, valid operators "
"are {1}".format(op, _unary_ops_syms)
f"Invalid unary operator {repr(op)}, "
f"valid operators are {_unary_ops_syms}"
)

def __call__(self, env):
Expand Down

0 comments on commit 0068f04

Please sign in to comment.