-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: Refactor string special methods #4092
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
Changes from all commits
411d13f
0cf93aa
7222e5a
8468b13
a558314
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Base class(es) for all pandas objects. | ||
""" | ||
from pandas.util import py3compat | ||
|
||
class StringMixin(object): | ||
"""implements string methods so long as object defines a `__unicode__` method. | ||
Handles Python2/3 compatibility transparently.""" | ||
# side note - this could be made into a metaclass if more than one object nees | ||
def __str__(self): | ||
""" | ||
Return a string representation for a particular object. | ||
|
||
Invoked by str(obj) 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 object. | ||
|
||
Invoked by bytes(obj) in py3 only. | ||
Yields a bytestring in both py2/py3. | ||
""" | ||
from pandas.core.config import get_option | ||
|
||
encoding = get_option("display.encoding") | ||
return self.__unicode__().encode(encoding, 'replace') | ||
|
||
def __repr__(self): | ||
""" | ||
Return a string representation for a particular object. | ||
|
||
Yields Bytestring in Py2, Unicode String in py3. | ||
""" | ||
return str(self) | ||
|
||
class PandasObject(StringMixin): | ||
"""baseclass for various pandas objects""" | ||
|
||
@property | ||
def _constructor(self): | ||
"""class constructor (for this class it's just `__class__`""" | ||
return self.__class__ | ||
|
||
def __unicode__(self): | ||
""" | ||
Return a string representation for a particular object. | ||
|
||
Invoked by unicode(obj) in py2 only. Yields a Unicode String in both | ||
py2/py3. | ||
""" | ||
# Should be overwritten by base classes | ||
return object.__repr__(self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. That would be an infinite loop. (because StringMixin calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh whoops blah sorry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpcloud is it worth it to fix the trailing whitespace? Easy to remove the fix, but it was bothering me a little.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure why not? i suppose it makes it more difficult to review...but for docs i don't think it's a big deal