From 1ed82116ce655909df649233c25d0cf70189c08e Mon Sep 17 00:00:00 2001 From: rfeistenauer Date: Tue, 20 Oct 2020 14:40:17 +0200 Subject: [PATCH] Moved supression for keyboard interrupt to display --- cellular_automaton/display.py | 22 ++++++++++++---------- examples/conways_game_of_life.py | 4 +--- examples/simple_star_fall.py | 4 +--- setup.py | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/cellular_automaton/display.py b/cellular_automaton/display.py index c9ef8a1..0d81879 100644 --- a/cellular_automaton/display.py +++ b/cellular_automaton/display.py @@ -19,6 +19,7 @@ limitations under the License. import time import operator import collections +import contextlib from typing import Sequence from . import CellularAutomaton @@ -94,16 +95,17 @@ class CAWindow: :param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop Warning: is blocking until finished """ - while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step): - time_ca_start = time.time() - self._cellular_automaton.evolve(evolutions_per_draw) - time_ca_end = time.time() - self._redraw_dirty_cells() - time_ds_end = time.time() - self.print_process_info(evolve_duration=(time_ca_end - time_ca_start), - draw_duration=(time_ds_end - time_ca_end), - evolution_step=self._cellular_automaton.evolution_step) - self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) + with contextlib.suppress(KeyboardInterrupt): + while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step): + time_ca_start = time.time() + self._cellular_automaton.evolve(evolutions_per_draw) + time_ca_end = time.time() + self._redraw_dirty_cells() + time_ds_end = time.time() + self.print_process_info(evolve_duration=(time_ca_end - time_ca_start), + draw_duration=(time_ds_end - time_ca_end), + evolution_step=self._cellular_automaton.evolution_step) + self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover if evolutions_per_second > 0: diff --git a/examples/conways_game_of_life.py b/examples/conways_game_of_life.py index 2b63cc4..b4e40cf 100644 --- a/examples/conways_game_of_life.py +++ b/examples/conways_game_of_life.py @@ -19,7 +19,6 @@ limitations under the License. # pylint: disable=missing-function-docstring import random -import contextlib import sys import os sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) @@ -65,5 +64,4 @@ class ConwaysCA(CellularAutomaton): if __name__ == "__main__": - with contextlib.suppress(KeyboardInterrupt): - CAWindow(cellular_automaton=ConwaysCA()).run(evolutions_per_second=40) + CAWindow(cellular_automaton=ConwaysCA()).run(evolutions_per_second=40) diff --git a/examples/simple_star_fall.py b/examples/simple_star_fall.py index 3a5e8ac..3effad9 100644 --- a/examples/simple_star_fall.py +++ b/examples/simple_star_fall.py @@ -20,7 +20,6 @@ limitations under the License. # pylint: disable=no-self-use import random -import contextlib import sys import os from typing import Sequence @@ -53,5 +52,4 @@ def state_to_color(current_state: Sequence) -> Sequence: if __name__ == "__main__": - with contextlib.suppress(KeyboardInterrupt): - CAWindow(cellular_automaton=StarFallAutomaton(), state_to_color_cb=state_to_color).run() + CAWindow(cellular_automaton=StarFallAutomaton(), state_to_color_cb=state_to_color).run() diff --git a/setup.py b/setup.py index fca3a90..970d04e 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('README.md') as f: setup( name="cellular_automaton", - version="1.0.0", + version="1.0.1", author="Richard Feistenauer", author_email="r.feistenauer@web.de", packages=find_packages(exclude=('tests', 'docs', 'examples')),