Skip to content

WIP,CLN: com.ReprMixin everywhere, cls defines __unicode__ and py2/3 #3183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,42 @@ def in_qtconsole():
except:
return False

class ReprMixin(object):
"""
Classes enowed with this mixin need only implement __unicode__
and str,bytes, repr will return the right type of string in py2/py3

"""
def __str__(self):
"""
Return a string representation for a particular DataFrame

Invoked by str(df) in both py2/py3.
Yields Bytestring in Py2, Unicode String in py3.
"""
if py3compat.PY3:
return self.__unicode__()
return self.__bytes__()

def __bytes__(self):
"""
Return a string representation for a particular DataFrame

Invoked by bytes(df) in py3 only.
Yields a bytestring in both py2/py3.
"""
encoding = get_option("display.encoding")
return self.__unicode__().encode(encoding, 'replace')

def __repr__(self):
"""
Return a string representation for a particular DataFrame

Yields Bytestring in Py2, Unicode String in py3.
"""
return str(self)


# Unicode consolidation
# ---------------------
#
Expand Down
32 changes: 2 additions & 30 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,28 +638,8 @@ def _need_info_repr_(self):
else:
return False

def __str__(self):
"""
Return a string representation for a particular DataFrame

Invoked by str(df) in both py2/py3.
Yields Bytestring in Py2, Unicode String in py3.
"""

if py3compat.PY3:
return self.__unicode__()
return self.__bytes__()

def __bytes__(self):
"""
Return a string representation for a particular DataFrame

Invoked by bytes(df) in py3 only.
Yields a bytestring in both py2/py3.
"""
encoding = com.get_option("display.encoding")
return self.__unicode__().encode(encoding, 'replace')

# ReprMixin->PandasObject->NDFrame->DataFrame
# just define unicode, and str,bytes,repr work on py2/py3
def __unicode__(self):
"""
Return a string representation for a particular DataFrame
Expand Down Expand Up @@ -688,14 +668,6 @@ def _need_wide_repr(self):
return (get_option("display.expand_frame_repr")
and com.in_interactive_session())

def __repr__(self):
"""
Return a string representation for a particular DataFrame

Yields Bytestring in Py2, Unicode String in py3.
"""
return str(self)

def _repr_html_(self):
"""
Return a html representation for a particular DataFrame.
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PandasError(Exception):
pass


class PandasObject(object):
class PandasObject(com.ReprMixin, object):

_AXIS_NUMBERS = {
'index': 0,
Expand Down Expand Up @@ -539,7 +539,9 @@ def _constructor(self):
def axes(self):
return self._data.axes

def __repr__(self):
# ReprMixin->PandasObject->NDFrame
# just define unicode, and str,bytes,repr and everything works on py3/py3
def __unicode__(self):
return 'NDFrame'

@property
Expand Down
66 changes: 5 additions & 61 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _shouldbe_timestamp(obj):
or tslib.is_timestamp_array(obj))


class Index(np.ndarray):
class Index(com.ReprMixin,np.ndarray):
"""
Immutable ndarray implementing an ordered, sliceable set. The basic object
storing axis labels for all pandas objects
Expand Down Expand Up @@ -144,28 +144,8 @@ def __array_finalize__(self, obj):
def _shallow_copy(self):
return self.view()

def __str__(self):
"""
Return a string representation for a particular Index

Invoked by str(df) in both py2/py3.
Yields Bytestring in Py2, Unicode String in py3.
"""

if py3compat.PY3:
return self.__unicode__()
return self.__bytes__()

def __bytes__(self):
"""
Return a string representation for a particular Index

Invoked by bytes(df) in py3 only.
Yields a bytestring in both py2/py3.
"""
encoding = com.get_option("display.encoding")
return self.__unicode__().encode(encoding, 'replace')

# ReprMixin->Series
# just define unicode, and str,bytes,repr work on py2/py3
def __unicode__(self):
"""
Return a string representation for a particular Index
Expand All @@ -180,14 +160,6 @@ def __unicode__(self):
prepr = com.pprint_thing(data, escape_chars=('\t', '\r', '\n'))
return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype)

def __repr__(self):
"""
Return a string representation for a particular Index

Yields Bytestring in Py2, Unicode String in py3.
"""
return str(self)

def astype(self, dtype):
return Index(self.values.astype(dtype), name=self.name,
dtype=dtype)
Expand Down Expand Up @@ -1434,28 +1406,8 @@ def _array_values(self):
def dtype(self):
return np.dtype('O')

def __str__(self):
"""
Return a string representation for a particular Index

Invoked by str(df) in both py2/py3.
Yields Bytestring in Py2, Unicode String in py3.
"""

if py3compat.PY3:
return self.__unicode__()
return self.__bytes__()

def __bytes__(self):
"""
Return a string representation for a particular Index

Invoked by bytes(df) in py3 only.
Yields a bytestring in both py2/py3.
"""
encoding = com.get_option("display.encoding")
return self.__unicode__().encode(encoding, 'replace')

# ReprMixin->Index->MultiIndex
# just define unicode, and str,bytes,repr work on py2/py3
def __unicode__(self):
"""
Return a string representation for a particular Index
Expand All @@ -1478,14 +1430,6 @@ def __unicode__(self):

return output % summary

def __repr__(self):
"""
Return a string representation for a particular Index

Yields Bytestring in Py2, Unicode String in py3.
"""
return str(self)

def __len__(self):
return len(self.labels[0])

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ def make_block(values, items, ref_items):
# TODO: flexible with index=None and/or items=None


class BlockManager(object):
class BlockManager(com.ReprMixin,object):
"""
Core internal data structure to implement DataFrame

Expand Down Expand Up @@ -894,7 +894,9 @@ def __setstate__(self, state):
def __len__(self):
return len(self.items)

def __repr__(self):
# ReprMixin->BlockManager
# just define unicode, and str,bytes,repr work on py2/py3
def __unicode__(self):
output = 'BlockManager'
for i, ax in enumerate(self.axes):
if i == 0:
Expand Down
32 changes: 2 additions & 30 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,28 +456,8 @@ def __invert__(self):
#----------------------------------------------------------------------
# Magic methods

def __str__(self):
"""
Return a string representation for a particular Panel

Invoked by str(df) in both py2/py3.
Yields Bytestring in Py2, Unicode String in py3.
"""

if py3compat.PY3:
return self.__unicode__()
return self.__bytes__()

def __bytes__(self):
"""
Return a string representation for a particular Panel

Invoked by bytes(df) in py3 only.
Yields a bytestring in both py2/py3.
"""
encoding = com.get_option("display.encoding")
return self.__unicode__().encode(encoding, 'replace')

# ReprMixin->PandasObject->Panel
# just define unicode, and str,bytes,repr work on py2/py3
def __unicode__(self):
"""
Return a string representation for a particular Panel
Expand All @@ -502,14 +482,6 @@ def axis_pretty(a):
[class_name, dims] + [axis_pretty(a) for a in self._AXIS_ORDERS])
return output

def __repr__(self):
"""
Return a string representation for a particular Panel

Yields Bytestring in Py2, Unicode String in py3.
"""
return str(self)

def __iter__(self):
return iter(getattr(self, self._info_axis))

Expand Down
32 changes: 2 additions & 30 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,28 +1067,8 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):

return df.reset_index(level=level, drop=drop)

def __str__(self):
"""
Return a string representation for a particular DataFrame

Invoked by str(df) in both py2/py3.
Yields Bytestring in Py2, Unicode String in py3.
"""

if py3compat.PY3:
return self.__unicode__()
return self.__bytes__()

def __bytes__(self):
"""
Return a string representation for a particular DataFrame

Invoked by bytes(df) in py3 only.
Yields a bytestring in both py2/py3.
"""
encoding = com.get_option("display.encoding")
return self.__unicode__().encode(encoding, 'replace')

# ReprMixin->PandasObject->Series->DataFrame
# just define unicode, and str,bytes,repr work on py2/py3
def __unicode__(self):
"""
Return a string representation for a particular DataFrame
Expand All @@ -1112,14 +1092,6 @@ def __unicode__(self):
assert type(result) == unicode
return result

def __repr__(self):
"""
Return a string representation for a particular Series

Yields Bytestring in Py2, Unicode String in py3.
"""
return str(self)

def _tidy_repr(self, max_vals=20):
"""

Expand Down
Loading