From 86dfe0a52623d732487d9a639c66b206645bc4f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9phine=20Wolf=20Oberholtzer?= <166141926+josephine-wolf-oberholtzer@users.noreply.github.com> Date: Thu, 27 Feb 2025 12:25:22 -0500 Subject: [PATCH] Make some Clock methods private (#451) --- supriya/clocks/asynchronous.py | 4 ++-- supriya/clocks/bases.py | 41 ++++++++++++++++----------------- supriya/clocks/offline.py | 21 +++++++++-------- supriya/clocks/threaded.py | 4 ++-- tests/clocks/test_AsyncClock.py | 12 +++++----- tests/clocks/test_Clock.py | 12 +++++----- 6 files changed, 47 insertions(+), 47 deletions(-) diff --git a/supriya/clocks/asynchronous.py b/supriya/clocks/asynchronous.py index 11af54e45..76f9f3459 100644 --- a/supriya/clocks/asynchronous.py +++ b/supriya/clocks/asynchronous.py @@ -119,7 +119,7 @@ async def _wait_for_event_async(self, sleep_time: float) -> None: pass async def _wait_for_moment_async(self, offline: bool = False) -> Optional[Moment]: - current_time = self.get_current_time() + current_time = self._get_current_time() next_time = self._event_queue.peek().seconds logger.debug( f"[{self.name}] ... Waiting for next moment at {next_time} from {current_time}" @@ -131,7 +131,7 @@ async def _wait_for_moment_async(self, offline: bool = False) -> Optional[Moment return None self._process_command_deque() next_time = self._event_queue.peek().seconds - current_time = self.get_current_time() + current_time = self._get_current_time() self._event.clear() return self._seconds_to_moment(current_time) diff --git a/supriya/clocks/bases.py b/supriya/clocks/bases.py index 7e24d2eea..5ec1d79c5 100644 --- a/supriya/clocks/bases.py +++ b/supriya/clocks/bases.py @@ -94,7 +94,7 @@ def _get_cue_point( offset = self._measure_to_offset(measure) else: measure = None - fraction_grid = self.quantization_to_beats(quantization) + fraction_grid = self._quantization_to_beats(quantization) div, mod = divmod(moment.offset, fraction_grid) offset = float(div * fraction_grid) if mod: @@ -106,6 +106,9 @@ def _get_cue_point( ) return seconds, offset, measure + def _get_current_time(self) -> float: + return time.time() + def _get_schedule_point( self, schedule_at: float, time_unit: TimeUnit ) -> Tuple[float, Optional[float], Optional[int]]: @@ -169,6 +172,18 @@ def _offset_to_seconds(self, offset: float) -> float: beat_duration=1 / self._state.time_signature[1], ) + def _peek(self) -> Optional[Event]: + try: + return self._event_queue.peek() + except queue.Empty: + return None + + def _quantization_to_beats(self, quantization: Quantization) -> float: + fraction = fractions.Fraction(quantization.replace("T", "")) + if "T" in quantization: + fraction *= fractions.Fraction(2, 3) + return float(fraction) + def _seconds_to_moment(self, seconds: float) -> Moment: offset = self._seconds_to_offset(seconds) measure, measure_offset = divmod( @@ -495,7 +510,7 @@ def _start( if self._is_running: raise RuntimeError("Already started") if initial_time is None: - initial_time = self.get_current_time() + initial_time = self._get_current_time() self._state = ClockState( beats_per_minute=beats_per_minute or self._state.beats_per_minute, initial_seconds=initial_time, @@ -538,7 +553,7 @@ def change( beats_per_minute=beats_per_minute, time_signature=time_signature, quantization=None, - schedule_at=self.get_current_time(), + schedule_at=self._get_current_time(), time_unit=None, ) self._enqueue_command(command) @@ -565,7 +580,7 @@ def cue( kwargs=kwargs, procedure=procedure, quantization=quantization, - schedule_at=self.get_current_time() if self.is_running else 0, + schedule_at=self._get_current_time() if self.is_running else 0, time_unit=None, ) self._enqueue_command(command) @@ -586,29 +601,13 @@ def cue_change( event_id=event_id, event_type=EventType.CHANGE, quantization=quantization, - schedule_at=self.get_current_time() if self.is_running else 0, + schedule_at=self._get_current_time() if self.is_running else 0, time_signature=time_signature, time_unit=None, ) self._enqueue_command(command) return event_id - def get_current_time(self) -> float: - return time.time() - - def peek(self) -> Optional[Event]: - try: - return self._event_queue.peek() - except queue.Empty: - return None - - @classmethod - def quantization_to_beats(cls, quantization: Quantization) -> float: - fraction = fractions.Fraction(quantization.replace("T", "")) - if "T" in quantization: - fraction *= fractions.Fraction(2, 3) - return float(fraction) - def reschedule( self, event_id: int, diff --git a/supriya/clocks/offline.py b/supriya/clocks/offline.py index d07072665..39a9a1bbd 100644 --- a/supriya/clocks/offline.py +++ b/supriya/clocks/offline.py @@ -16,6 +16,11 @@ def __init__(self) -> None: ### SCHEDULING METHODS ### + def _get_current_time(self) -> float: + if not self._is_running: + return 0.0 + return self._state.previous_seconds + def _perform_callback_event( self, event: CallbackEvent, current_moment: Moment, desired_moment: Moment ) -> None: @@ -66,11 +71,6 @@ def _wait_for_queue(self, offline: bool = False) -> bool: ### PUBLIC METHODS ### - def get_current_time(self) -> float: - if not self._is_running: - return 0.0 - return self._state.previous_seconds - def start( self, initial_time: Optional[float] = None, @@ -102,6 +102,12 @@ def stop(self) -> None: class AsyncOfflineClock(AsyncClock): + + def _get_current_time(self) -> float: + if not self._is_running: + return 0.0 + return self._state.previous_seconds + async def _run_async(self, offline: bool = False) -> None: logger.debug(f"[{self.name}] Coroutine start") self._process_command_deque(first_run=True) @@ -136,8 +142,3 @@ async def _wait_for_queue_async(self, offline: bool = False) -> bool: self._process_command_deque() self._event.clear() return True - - def get_current_time(self) -> float: - if not self._is_running: - return 0.0 - return self._state.previous_seconds diff --git a/supriya/clocks/threaded.py b/supriya/clocks/threaded.py index a6e2792c7..50c18f5a3 100644 --- a/supriya/clocks/threaded.py +++ b/supriya/clocks/threaded.py @@ -52,7 +52,7 @@ def _run(self, offline: bool = False) -> None: logger.debug(f"[{self.name}] Terminating") def _wait_for_moment(self, offline: bool = False) -> Optional[Moment]: - current_time = self.get_current_time() + current_time = self._get_current_time() next_time = self._event_queue.peek().seconds logger.debug( f"[{self.name}] ... Waiting for next moment at {next_time} from {current_time}" @@ -64,7 +64,7 @@ def _wait_for_moment(self, offline: bool = False) -> Optional[Moment]: return None self._process_command_deque() next_time = self._event_queue.peek().seconds - current_time = self.get_current_time() + current_time = self._get_current_time() self._event.clear() return self._seconds_to_moment(current_time) diff --git a/tests/clocks/test_AsyncClock.py b/tests/clocks/test_AsyncClock.py index 7896a09e2..01dea56cf 100644 --- a/tests/clocks/test_AsyncClock.py +++ b/tests/clocks/test_AsyncClock.py @@ -29,7 +29,7 @@ async def wait_for_event(self, sleep_time): clock = AsyncClock() clock.slop = 0.01 monkeypatch.setattr(AsyncClock, "_wait_for_event_async", wait_for_event) - mock_time = mocker.patch.object(AsyncClock, "get_current_time") + mock_time = mocker.patch.object(AsyncClock, "_get_current_time") mock_time.return_value = 0.0 yield clock await clock.stop() @@ -56,7 +56,7 @@ def callback( async def set_time_and_check(time_to_advance, clock, store): logger.info(f"Setting time to {time_to_advance}") - clock.get_current_time.return_value = time_to_advance + clock._get_current_time.return_value = time_to_advance clock._event.set() await asyncio.sleep(0.01) moments = [] @@ -474,10 +474,10 @@ async def test_reschedule_earlier(clock): assert await set_time_and_check(0.5, clock, store) == [] event_id = clock.cue(callback, quantization="1M", args=[store], kwargs={"limit": 0}) await asyncio.sleep(0) - assert clock.peek().seconds == 2.0 + assert clock._peek().seconds == 2.0 clock.reschedule(event_id, schedule_at=0.5) await asyncio.sleep(0) - assert clock.peek().seconds == 1.0 + assert clock._peek().seconds == 1.0 assert await set_time_and_check(2.0, clock, store) == [ (["4/4", 120.0], [2, 0.0, 1.0, 2.0], [1, 0.5, 0.5, 1.0]) ] @@ -490,10 +490,10 @@ async def test_reschedule_later(clock): assert await set_time_and_check(0.5, clock, store) == [] event_id = clock.cue(callback, quantization="1M", args=[store], kwargs={"limit": 0}) await asyncio.sleep(0) - assert clock.peek().seconds == 2.0 + assert clock._peek().seconds == 2.0 clock.reschedule(event_id, schedule_at=1.5) await asyncio.sleep(0) - assert clock.peek().seconds == 3.0 + assert clock._peek().seconds == 3.0 assert await set_time_and_check(3.0, clock, store) == [ (["4/4", 120.0], [2, 0.5, 1.5, 3.0], [2, 0.5, 1.5, 3.0]) ] diff --git a/tests/clocks/test_Clock.py b/tests/clocks/test_Clock.py index 513497ddc..7aa02d260 100644 --- a/tests/clocks/test_Clock.py +++ b/tests/clocks/test_Clock.py @@ -15,7 +15,7 @@ def clock(mocker): clock = Clock() clock.slop = 0.001 - mock = mocker.patch.object(Clock, "get_current_time") + mock = mocker.patch.object(Clock, "_get_current_time") mock.return_value = 0.0 yield clock clock.stop() @@ -41,7 +41,7 @@ def callback( def set_time_and_check(time_to_advance, clock, store): - clock.get_current_time.return_value = time_to_advance + clock._get_current_time.return_value = time_to_advance multiplier = 4 if platform.system() == "Windows": multiplier = 40 # Windows CI is really slow @@ -458,10 +458,10 @@ def test_reschedule_earlier(clock): assert set_time_and_check(0.5, clock, store) == [] event_id = clock.cue(callback, quantization="1M", args=[store], kwargs={"limit": 0}) time.sleep(clock.slop * 2) - assert clock.peek().seconds == 2.0 + assert clock._peek().seconds == 2.0 clock.reschedule(event_id, schedule_at=0.5) time.sleep(clock.slop * 2) - assert clock.peek().seconds == 1.0 + assert clock._peek().seconds == 1.0 assert set_time_and_check(2.0, clock, store) == [ (["4/4", 120.0], [2, 0.0, 1.0, 2.0], [1, 0.5, 0.5, 1.0]) ] @@ -474,10 +474,10 @@ def test_reschedule_later(clock): assert set_time_and_check(0.5, clock, store) == [] event_id = clock.cue(callback, quantization="1M", args=[store], kwargs={"limit": 0}) time.sleep(clock.slop * 2) - assert clock.peek().seconds == 2.0 + assert clock._peek().seconds == 2.0 clock.reschedule(event_id, schedule_at=1.5) time.sleep(clock.slop * 2) - assert clock.peek().seconds == 3.0 + assert clock._peek().seconds == 3.0 assert set_time_and_check(3.0, clock, store) == [ (["4/4", 120.0], [2, 0.5, 1.5, 3.0], [2, 0.5, 1.5, 3.0]) ]