Skip to content

Commit

Permalink
Make some Clock methods private (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephine-wolf-oberholtzer authored Feb 27, 2025
1 parent 9cdd45f commit 86dfe0a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions supriya/clocks/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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)

Expand Down
41 changes: 20 additions & 21 deletions supriya/clocks/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]]:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
21 changes: 11 additions & 10 deletions supriya/clocks/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions supriya/clocks/threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions tests/clocks/test_AsyncClock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = []
Expand Down Expand Up @@ -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])
]
Expand All @@ -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])
]
Expand Down
12 changes: 6 additions & 6 deletions tests/clocks/test_Clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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])
]
Expand All @@ -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])
]
Expand Down

0 comments on commit 86dfe0a

Please sign in to comment.