From 55edc6b24ca5448295327ae10fb25cf87378c485 Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 5 Aug 2019 00:02:23 +0100 Subject: [PATCH 1/2] Move StringMixin to computations.common --- pandas/core/base.py | 24 ------------------------ pandas/core/computation/common.py | 5 +++++ pandas/core/computation/expr.py | 5 ++--- pandas/core/computation/ops.py | 15 +++++++-------- pandas/core/computation/pytables.py | 9 ++++----- pandas/core/computation/scope.py | 4 ++-- pandas/tests/series/test_repr.py | 5 ++--- 7 files changed, 22 insertions(+), 45 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 38a8bf7171521..7d2a62318232c 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -46,30 +46,6 @@ ) -class StringMixin: - """ - Implements string methods so long as object defines a `__str__` method. - """ - - # side note - this could be made into a metaclass if more than one - # object needs - - # ---------------------------------------------------------------------- - # Formatting - - def __str__(self): - """ - Return a string representation for a particular Object - """ - raise AbstractMethodError(self) - - def __repr__(self): - """ - Return a string representation for a particular object. - """ - return str(self) - - class PandasObject(DirNamesMixin): """baseclass for various pandas objects""" diff --git a/pandas/core/computation/common.py b/pandas/core/computation/common.py index ddb1023479cba..ddb79a0a811ca 100644 --- a/pandas/core/computation/common.py +++ b/pandas/core/computation/common.py @@ -36,3 +36,8 @@ def _remove_spaces_column_name(name): class NameResolutionError(NameError): pass + + +class StringMixin: + # TODO: delete this class. Removing this ATM caused a failure. + ... diff --git a/pandas/core/computation/expr.py b/pandas/core/computation/expr.py index e10d189bc3c6f..d0d87c23e9346 100644 --- a/pandas/core/computation/expr.py +++ b/pandas/core/computation/expr.py @@ -13,7 +13,6 @@ import pandas as pd from pandas.core import common as com -from pandas.core.base import StringMixin from pandas.core.computation.common import ( _BACKTICK_QUOTED_STRING, _remove_spaces_column_name, @@ -799,7 +798,7 @@ def __init__(self, env, engine, parser, preparser=lambda x: x): super().__init__(env, engine, parser, preparser=preparser) -class Expr(StringMixin): +class Expr: """Object encapsulating an expression. @@ -831,7 +830,7 @@ def assigner(self): def __call__(self): return self.terms(self.env) - def __str__(self): + def __repr__(self): return printing.pprint_thing(self.terms) def __len__(self): diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index 870acc3cc9956..2bf09a553ce18 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -12,7 +12,6 @@ from pandas.core.dtypes.common import is_list_like, is_scalar -from pandas.core.base import StringMixin import pandas.core.common as com from pandas.core.computation.common import _ensure_decoded, _result_type_many from pandas.core.computation.scope import _DEFAULT_GLOBALS @@ -63,7 +62,7 @@ def __init__(self, name, is_local): super().__init__(msg.format(name)) -class Term(StringMixin): +class Term: def __new__(cls, name, env, side=None, encoding=None): klass = Constant if not isinstance(name, str) else cls supr_new = super(Term, klass).__new__ @@ -82,7 +81,7 @@ def __init__(self, name, env, side=None, encoding=None): def local_name(self): return self.name.replace(_LOCAL_TAG, "") - def __str__(self): + def __repr__(self): return pprint_thing(self.name) def __call__(self, *args, **kwargs): @@ -182,7 +181,7 @@ def _resolve_name(self): def name(self): return self.value - def __str__(self): + def __repr__(self): # in python 2 str() of float # can truncate shorter than repr() return repr(self.name) @@ -191,7 +190,7 @@ def __str__(self): _bool_op_map = {"not": "~", "and": "&", "or": "|"} -class Op(StringMixin): +class Op: """Hold an operator of arbitrary arity """ @@ -204,7 +203,7 @@ def __init__(self, op, operands, *args, **kwargs): def __iter__(self): return iter(self.operands) - def __str__(self): + def __repr__(self): """Print a generic n-ary operator and its operands using infix notation""" # recurse over the operands @@ -537,7 +536,7 @@ def __call__(self, env): operand = self.operand(env) return self.func(operand) - def __str__(self): + def __repr__(self): return pprint_thing("{0}({1})".format(self.op, self.operand)) @property @@ -562,7 +561,7 @@ def __call__(self, env): with np.errstate(all="ignore"): return self.func.func(*operands) - def __str__(self): + def __repr__(self): operands = map(str, self.operands) return pprint_thing("{0}({1})".format(self.op, ",".join(operands))) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 60cf35163bcf4..41c021bcdd2bf 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -11,10 +11,9 @@ from pandas.core.dtypes.common import is_list_like import pandas as pd -from pandas.core.base import StringMixin import pandas.core.common as com from pandas.core.computation import expr, ops -from pandas.core.computation.common import _ensure_decoded +from pandas.core.computation.common import StringMixin, _ensure_decoded from pandas.core.computation.expr import BaseExprVisitor from pandas.core.computation.ops import UndefinedVariableError, is_term @@ -231,7 +230,7 @@ def convert_values(self): class FilterBinOp(BinOp): - def __str__(self): + def __repr__(self): return pprint_thing( "[Filter : [{lhs}] -> [{op}]".format(lhs=self.filter[0], op=self.filter[1]) ) @@ -297,7 +296,7 @@ def evaluate(self): class ConditionBinOp(BinOp): - def __str__(self): + def __repr__(self): return pprint_thing("[Condition : [{cond}]]".format(cond=self.condition)) def invert(self): @@ -548,7 +547,7 @@ def __init__(self, where, queryables=None, encoding=None, scope_level=0): ) self.terms = self.parse() - def __str__(self): + def __repr__(self): if self.terms is not None: return pprint_thing(self.terms) return pprint_thing(self.expr) diff --git a/pandas/core/computation/scope.py b/pandas/core/computation/scope.py index 4d5a523337f66..8ddd0dd7622e7 100644 --- a/pandas/core/computation/scope.py +++ b/pandas/core/computation/scope.py @@ -15,8 +15,8 @@ from pandas._libs.tslibs import Timestamp from pandas.compat.chainmap import DeepChainMap -from pandas.core.base import StringMixin import pandas.core.computation as compu +from pandas.core.computation.common import StringMixin def _ensure_scope( @@ -141,7 +141,7 @@ def __init__( self.resolvers = DeepChainMap(*resolvers) self.temps = {} - def __str__(self): + def __repr__(self): scope_keys = _get_pretty_string(list(self.scope.keys())) res_keys = _get_pretty_string(list(self.resolvers.keys())) unicode_str = "{name}(scope={scope_keys}, resolvers={res_keys})" diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 3e8f653c47424..3c6da304dd68d 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -14,7 +14,6 @@ period_range, timedelta_range, ) -from pandas.core.base import StringMixin from pandas.core.index import MultiIndex import pandas.util.testing as tm @@ -226,11 +225,11 @@ class TestCategoricalRepr: def test_categorical_repr_unicode(self): # see gh-21002 - class County(StringMixin): + class County: name = "San Sebastián" state = "PR" - def __str__(self): + def __repr__(self): return self.name + ", " + self.state cat = pd.Categorical([County() for _ in range(61)]) From a098efc8f8a05431ba9fd4c0d03adc2533b4dae8 Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 5 Aug 2019 00:46:21 +0100 Subject: [PATCH 2/2] Change according to comments --- pandas/core/computation/common.py | 2 +- pandas/core/computation/pytables.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/computation/common.py b/pandas/core/computation/common.py index ddb79a0a811ca..b8e212fd2a32e 100644 --- a/pandas/core/computation/common.py +++ b/pandas/core/computation/common.py @@ -40,4 +40,4 @@ class NameResolutionError(NameError): class StringMixin: # TODO: delete this class. Removing this ATM caused a failure. - ... + pass diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 41c021bcdd2bf..1523eb05ac41d 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -13,7 +13,7 @@ import pandas as pd import pandas.core.common as com from pandas.core.computation import expr, ops -from pandas.core.computation.common import StringMixin, _ensure_decoded +from pandas.core.computation.common import _ensure_decoded from pandas.core.computation.expr import BaseExprVisitor from pandas.core.computation.ops import UndefinedVariableError, is_term @@ -31,8 +31,7 @@ def __init__(self, level, global_dict=None, local_dict=None, queryables=None): class Term(ops.Term): def __new__(cls, name, env, side=None, encoding=None): klass = Constant if not isinstance(name, str) else cls - supr_new = StringMixin.__new__ - return supr_new(klass) + return object.__new__(klass) def __init__(self, name, env, side=None, encoding=None): super().__init__(name, env, side=side, encoding=encoding)