Skip to content

Commit

Permalink
Replace dynamic use of "on_trait_change" handlers with "observe" (#373)
Browse files Browse the repository at this point in the history
* DEV : Replace dynamic on_trait_change handlers with observe

This commit replaces the use of dynamic on_trait_change handlers e.g.
(object.on_trait_change(...)) with the use of observe
(object.observe(...)).

With these changes, the transition of traits-futures from
on_trait_change to observe is complete.

fixes #356

	modified:   traits_futures/asyncio/event_loop_helper.py
	modified:   traits_futures/qt/event_loop_helper.py
	modified:   traits_futures/tests/common_future_tests.py
	modified:   traits_futures/tests/traits_executor_tests.py
	modified:   traits_futures/wx/event_loop_helper.py

* DOC : Document the dependence on traits>= 6.2.0

	modified:   docs/source/changes.rst
	modified:   setup.py
  • Loading branch information
Poruri Sai Rahul committed Jul 6, 2021
1 parent d7b2e4d commit 1bde590
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ of Traits Futures.
Other Changes
~~~~~~~~~~~~~

* Traits Futures now requires Traits 6.2.0 or later.
* Python 3.5 is no longer supported. Traits Futures requires Python 3.6
or later.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_long_description():

install_requires = [
"pyface",
"traits",
"traits>=6.2.0",
]


Expand Down
6 changes: 3 additions & 3 deletions traits_futures/asyncio/event_loop_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def stop_on_timeout():
timed_out.append(True)
event_loop.stop()

def stop_if_condition():
def stop_if_condition(event):
if condition(object):
event_loop.stop()

object.on_trait_change(stop_if_condition, trait)
object.observe(stop_if_condition, trait)
try:
# The condition may have become True before we
# started listening to changes. So start with a check.
Expand All @@ -111,7 +111,7 @@ def stop_if_condition():
finally:
timer_handle.cancel()
finally:
object.on_trait_change(stop_if_condition, trait, remove=True)
object.observe(stop_if_condition, trait, remove=True)

if timed_out:
raise RuntimeError(
Expand Down
6 changes: 3 additions & 3 deletions traits_futures/qt/event_loop_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ def run_until(self, object, trait, condition, timeout):
def stop_on_timeout():
qt_app.exit(1)

def stop_if_condition():
def stop_if_condition(event):
if condition(object):
qt_app.exit(0)

object.on_trait_change(stop_if_condition, trait)
object.observe(stop_if_condition, trait)
try:
# The condition may have become True before we
# started listening to changes. So start with a check.
Expand All @@ -143,7 +143,7 @@ def stop_if_condition():
timeout_timer.stop()
timeout_timer.timeout.disconnect(stop_on_timeout)
finally:
object.on_trait_change(stop_if_condition, trait, remove=True)
object.observe(stop_if_condition, trait, remove=True)

if timed_out:
raise RuntimeError(
Expand Down
8 changes: 4 additions & 4 deletions traits_futures/tests/common_future_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ def test_cancellable_and_done_consistent_with_state(self):
# Triples (state, cancellable, done)
states = []

def record_states():
def record_states(event=None):
"""Record the future's state and derived traits."""
states.append((future.state, future.cancellable, future.done))

# Record state when any of the three traits changes.
future = self.future_class()
future._executor_initialized(dummy_cancel_callback)

future.on_trait_change(record_states, "cancellable")
future.on_trait_change(record_states, "done")
future.on_trait_change(record_states, "state")
future.observe(record_states, "cancellable")
future.observe(record_states, "done")
future.observe(record_states, "state")

# Record initial, synthesize some state changes, then record final.
record_states()
Expand Down
12 changes: 6 additions & 6 deletions traits_futures/tests/traits_executor_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ def test_submit_iteration_method(self):
)

results = []
future.on_trait_change(
lambda result: results.append(result), "result_event"
future.observe(
lambda event: results.append(event.new), "result_event"
)

self.wait_until_done(future)
Expand Down Expand Up @@ -295,7 +295,7 @@ def test_states_consistent(self):
# Triples (state, running, stopped).
states = []

def record_states():
def record_states(event=None):
states.append(
(
self.executor.state,
Expand All @@ -304,9 +304,9 @@ def record_states():
)
)

self.executor.on_trait_change(record_states, "running")
self.executor.on_trait_change(record_states, "stopped")
self.executor.on_trait_change(record_states, "state")
self.executor.observe(record_states, "running")
self.executor.observe(record_states, "stopped")
self.executor.observe(record_states, "state")
submit_call(self.executor, int)

# Record states before, during, and after stopping.
Expand Down
6 changes: 3 additions & 3 deletions traits_futures/wx/event_loop_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ def run_until(self, object, trait, condition, timeout):

timeout_timer = TimeoutTimer(timeout, lambda: wx_app.exit(1))

def stop_if_condition():
def stop_if_condition(event):
if condition(object):
wx_app.exit(0)

object.on_trait_change(stop_if_condition, trait)
object.observe(stop_if_condition, trait)
try:
# The condition may have become True before we
# started listening to changes. So start with a check.
Expand All @@ -230,7 +230,7 @@ def stop_if_condition():
timed_out = wx_app.exit_code
timeout_timer.stop()
finally:
object.on_trait_change(stop_if_condition, trait, remove=True)
object.observe(stop_if_condition, trait, remove=True)

if timed_out:
raise RuntimeError(
Expand Down

0 comments on commit 1bde590

Please sign in to comment.