Skip to content

Commit 810032b

Browse files
committed
CLN: standardize string repr to use __repr__
1 parent ba48fc4 commit 810032b

File tree

6 files changed

+27
-45
lines changed

6 files changed

+27
-45
lines changed

pandas/core/base.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,17 @@
3434

3535
class StringMixin:
3636
"""
37-
Implements string methods so long as object defines a `__str__` method.
37+
Require that an object defines a `__repr__` method.
3838
"""
39-
# side note - this could be made into a metaclass if more than one
40-
# object needs
39+
# TODO: Remove this class from the code base. It's not needed after
40+
# dropping python2.
4141

42-
# ----------------------------------------------------------------------
43-
# Formatting
44-
45-
def __str__(self):
42+
def __repr__(self):
4643
"""
4744
Return a string representation for a particular Object
4845
"""
4946
raise AbstractMethodError(self)
5047

51-
def __repr__(self):
52-
"""
53-
Return a string representation for a particular object.
54-
"""
55-
return str(self)
56-
5748

5849
class PandasObject(DirNamesMixin):
5950

pandas/core/computation/ops.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, name, env, side=None, encoding=None):
6767
def local_name(self):
6868
return self.name.replace(_LOCAL_TAG, '')
6969

70-
def __str__(self):
70+
def __repr__(self):
7171
return pprint_thing(self.name)
7272

7373
def __call__(self, *args, **kwargs):
@@ -166,9 +166,7 @@ def _resolve_name(self):
166166
def name(self):
167167
return self.value
168168

169-
def __str__(self):
170-
# in python 2 str() of float
171-
# can truncate shorter than repr()
169+
def __repr__(self):
172170
return repr(self.name)
173171

174172

@@ -188,7 +186,7 @@ def __init__(self, op, operands, *args, **kwargs):
188186
def __iter__(self):
189187
return iter(self.operands)
190188

191-
def __str__(self):
189+
def __repr__(self):
192190
"""Print a generic n-ary operator and its operands using infix
193191
notation"""
194192
# recurse over the operands
@@ -506,7 +504,7 @@ def __call__(self, env):
506504
operand = self.operand(env)
507505
return self.func(operand)
508506

509-
def __str__(self):
507+
def __repr__(self):
510508
return pprint_thing('{0}({1})'.format(self.op, self.operand))
511509

512510
@property
@@ -531,7 +529,7 @@ def __call__(self, env):
531529
with np.errstate(all='ignore'):
532530
return self.func.func(*operands)
533531

534-
def __str__(self):
532+
def __repr__(self):
535533
operands = map(str, self.operands)
536534
return pprint_thing('{0}({1})'.format(self.op, ','.join(operands)))
537535

pandas/core/computation/pytables.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pandas.core.dtypes.common import is_list_like
1212

1313
import pandas as pd
14-
from pandas.core.base import StringMixin
1514
import pandas.core.common as com
1615
from pandas.core.computation import expr, ops
1716
from pandas.core.computation.common import _ensure_decoded
@@ -36,8 +35,7 @@ class Term(ops.Term):
3635

3736
def __new__(cls, name, env, side=None, encoding=None):
3837
klass = Constant if not isinstance(name, str) else cls
39-
supr_new = StringMixin.__new__
40-
return supr_new(klass)
38+
return object.__new__(klass)
4139

4240
def __init__(self, name, env, side=None, encoding=None):
4341
super().__init__(name, env, side=side, encoding=encoding)
@@ -230,7 +228,7 @@ def convert_values(self):
230228

231229
class FilterBinOp(BinOp):
232230

233-
def __str__(self):
231+
def __repr__(self):
234232
return pprint_thing("[Filter : [{lhs}] -> [{op}]"
235233
.format(lhs=self.filter[0], op=self.filter[1]))
236234

@@ -302,7 +300,7 @@ def evaluate(self):
302300

303301
class ConditionBinOp(BinOp):
304302

305-
def __str__(self):
303+
def __repr__(self):
306304
return pprint_thing("[Condition : [{cond}]]"
307305
.format(cond=self.condition))
308306

@@ -549,7 +547,7 @@ def __init__(self, where, queryables=None, encoding=None, scope_level=0):
549547
encoding=encoding)
550548
self.terms = self.parse()
551549

552-
def __str__(self):
550+
def __repr__(self):
553551
if self.terms is not None:
554552
return pprint_thing(self.terms)
555553
return pprint_thing(self.expr)

pandas/core/computation/scope.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def _get_pretty_string(obj):
7979

8080

8181
class Scope(StringMixin):
82-
8382
"""Object to hold scope, with a few bells to deal with some custom syntax
8483
and contexts added by pandas.
8584
@@ -135,13 +134,13 @@ def __init__(self, level, global_dict=None, local_dict=None, resolvers=(),
135134
self.resolvers = DeepChainMap(*resolvers)
136135
self.temps = {}
137136

138-
def __str__(self):
137+
def __repr__(self):
139138
scope_keys = _get_pretty_string(list(self.scope.keys()))
140139
res_keys = _get_pretty_string(list(self.resolvers.keys()))
141-
unicode_str = '{name}(scope={scope_keys}, resolvers={res_keys})'
142-
return unicode_str.format(name=type(self).__name__,
143-
scope_keys=scope_keys,
144-
res_keys=res_keys)
140+
template = '{name}(scope={scope_keys}, resolvers={res_keys})'
141+
return template.format(name=type(self).__name__,
142+
scope_keys=scope_keys,
143+
res_keys=res_keys)
145144

146145
@property
147146
def has_resolvers(self):

pandas/io/pytables.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
to_datetime)
3333
from pandas.core.arrays.categorical import Categorical
3434
from pandas.core.arrays.sparse import BlockIndex, IntIndex
35-
from pandas.core.base import StringMixin
3635
import pandas.core.common as com
3736
from pandas.core.computation.pytables import Expr, maybe_expression
3837
from pandas.core.index import ensure_index
@@ -398,8 +397,7 @@ def _is_metadata_of(group, parent_group):
398397
return False
399398

400399

401-
class HDFStore(StringMixin):
402-
400+
class HDFStore:
403401
"""
404402
Dict-like IO interface for storing pandas objects in PyTables
405403
either Fixed or Table format.
@@ -520,7 +518,7 @@ def __contains__(self, key):
520518
def __len__(self):
521519
return len(self.groups())
522520

523-
def __str__(self):
521+
def __repr__(self):
524522
return '{type}\nFile path: {path}\n'.format(
525523
type=type(self), path=pprint_thing(self._path))
526524

@@ -1519,8 +1517,7 @@ def get_result(self, coordinates=False):
15191517
return results
15201518

15211519

1522-
class IndexCol(StringMixin):
1523-
1520+
class IndexCol:
15241521
""" an index column description class
15251522
15261523
Parameters
@@ -1587,7 +1584,7 @@ def set_table(self, table):
15871584
self.table = table
15881585
return self
15891586

1590-
def __str__(self):
1587+
def __repr__(self):
15911588
temp = tuple(
15921589
map(pprint_thing,
15931590
(self.name,
@@ -1881,7 +1878,7 @@ def __init__(self, values=None, kind=None, typ=None,
18811878
self.set_data(data)
18821879
self.set_metadata(metadata)
18831880

1884-
def __str__(self):
1881+
def __repr__(self):
18851882
temp = tuple(
18861883
map(pprint_thing,
18871884
(self.name,
@@ -2286,8 +2283,7 @@ def get_attr(self):
22862283
pass
22872284

22882285

2289-
class Fixed(StringMixin):
2290-
2286+
class Fixed:
22912287
""" represent an object in my store
22922288
facilitate read/write of various types of objects
22932289
this is an abstract base class
@@ -2336,7 +2332,7 @@ def pandas_type(self):
23362332
def format_type(self):
23372333
return 'fixed'
23382334

2339-
def __str__(self):
2335+
def __repr__(self):
23402336
""" return a pretty representation of myself """
23412337
self.infer_axes()
23422338
s = self.shape
@@ -3077,7 +3073,7 @@ def table_type_short(self):
30773073
def format_type(self):
30783074
return 'table'
30793075

3080-
def __str__(self):
3076+
def __repr__(self):
30813077
""" return a pretty representatgion of myself """
30823078
self.infer_axes()
30833079
dc = ",dc->[{columns}]".format(columns=(','.join(

pandas/io/stata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def generate_value_label(self, byteorder, encoding):
712712
return bio.read()
713713

714714

715-
class StataMissingValue(StringMixin):
715+
class StataMissingValue:
716716
"""
717717
An observation's missing value.
718718

0 commit comments

Comments
 (0)