Skip to content
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

Fix pandas.DataFrame support in core._PrintResult #446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions fire/core.py
Expand Up @@ -262,6 +262,13 @@ def _PrintResult(component_trace, verbose=False, serialize=None):
print(str(result))
return

elif value_types.HasCustomRepr(result):
# Same as above, but for __repr__.
# For pandas.DataFrame, __str__ is inherited from object, but __repr__ has
# a custom implementation (see pandas.core.frame.DataFrame.__repr__)
print(str(result))
return

if isinstance(result, (list, set, frozenset, types.GeneratorType)):
for i in result:
print(_OneLineResult(i))
Expand Down
18 changes: 18 additions & 0 deletions fire/value_types.py
Expand Up @@ -83,3 +83,21 @@ def HasCustomStr(component):
if str_attr and str_attr.defining_class is not object:
return True
return False

def HasCustomRepr(component):
"""Reproduces above HasCustomStr function to determine if component has a
custom __repr__ method.

...

Args:
component: The object to check for a custom __repr__ method.
Returns:
Whether `component` has a custom __repr__ method.
"""
if hasattr(component, '__repr__'):
class_attrs = inspectutils.GetClassAttrsDict(type(component)) or {}
repr_attr = class_attrs.get('__repr__')
if repr_attr and repr_attr.defining_class is not object:
return True
return False