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

Implement a human-readable str/repr for State objects #2021

Merged
merged 5 commits into from
Jun 21, 2019
Merged
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ v0.15.0 (unreleased)
and ``glue.viewers.common.qt.toolbar_mode`` to ``glue.viewers.matplotlib.toolbar_mode``
and ``glue.viewers.matplotlib.qt.toolbar_mode``. [#1984]

* Implement a human-readable str/repr for State objects. [#2021]

* Avoiding importing Qt when using the base histogram and profile layer artists.
[#2012]

Expand Down
20 changes: 20 additions & 0 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import absolute_import, division, print_function

try:
from textwrap import indent
except ImportError: # PY2
def indent(text, prefix):
return ''.join([prefix + line for line in text.splitlines(True)])

from collections import defaultdict

import numpy as np
Expand Down Expand Up @@ -81,6 +87,20 @@ def as_dict(self):
properties[name] = getattr(self, name)
return properties

def __str__(self):
s = ""
state_dict = self.as_dict()
for key in sorted(state_dict):
value = state_dict[key]
if np.isscalar(value):
s += "{0}: {1}\n".format(key, value)
else:
s += "{0}: {1}\n".format(key, repr(value))
return s.strip()

def __repr__(self):
return "<{0}\n{1}\n>".format(self.__class__.__name__, indent(str(self), ' '))

def __gluestate__(self, context):
return {'values': dict((key, context.id(value)) for key, value in self.as_dict().items())}

Expand Down
32 changes: 32 additions & 0 deletions glue/core/tests/test_state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ def test_state_serialization():
assert state2.nested[2].nested == []


EXPECTED_STR = """
a: 2
b: hello
flat: <CallbackList with 3 elements>
nested: <CallbackList with 3 elements>
"""

EXPECTED_REPR = """
<SimpleTestState
a: 2
b: hello
flat: <CallbackList with 3 elements>
nested: <CallbackList with 3 elements>
>
"""


def test_state_str_repr():

state1 = SimpleTestState()
state1.a = 2
state1.b = 'hello'
state1.flat = [1, 3, 4]

sub_state = SimpleTestState()

state1.nested = [1, 3, sub_state]

assert str(state1) == EXPECTED_STR.strip()
assert repr(state1) == EXPECTED_REPR.strip()


class TestStateAttributeLimitsHelper():

def setup_method(self, method):
Expand Down
3 changes: 3 additions & 0 deletions glue/external/echo/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def __init__(self, callback, *args, **kwargs):
super(CallbackList, self).__init__(*args, **kwargs)
self.callback = callback

def __repr__(self):
return "<CallbackList with {0} elements>".format(len(self))

def append(self, value):
super(CallbackList, self).append(value)
if isinstance(value, HasCallbackProperties):
Expand Down