From 07bbd9d86920bc9d12f0f0860b15dd760d8cd63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Thu, 26 Jun 2025 19:42:11 +0900 Subject: [PATCH] =?UTF-8?q?(api-break)=20sequence=E3=81=AE=E5=87=BA?= =?UTF-8?q?=E5=8A=9B=E3=81=AB=E7=94=A8=E3=81=84=E3=82=8B=E5=9E=8B=E3=82=92?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=99=E3=82=8B=E9=81=B8=E6=8A=9E=E8=82=A2?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/asyncgui_ext/clock.py | 55 ++++++++++++++-------------------- tests/clock/test_anim_attrs.py | 14 --------- 2 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/asyncgui_ext/clock.py b/src/asyncgui_ext/clock.py index 2192b30..df2ea8c 100644 --- a/src/asyncgui_ext/clock.py +++ b/src/asyncgui_ext/clock.py @@ -341,9 +341,9 @@ async def interpolate_scalar(self, start, end, *, duration, step=0, transition=_ .. versionadded:: 0.5.2 ''' - async def interpolate_sequence(self, start, end, *, duration, step=0, transition=_linear, output_type=tuple) -> AsyncIterator: + async def interpolate_sequence(self, start, end, *, duration, step=0, transition=_linear) -> AsyncIterator: ''' - Same as :meth:`interpolate_scalar` except this one is for sequence type. + Same as :meth:`interpolate_scalar` except this one is for sequence types. .. code-block:: @@ -353,37 +353,37 @@ async def interpolate_sequence(self, start, end, *, duration, step=0, transition ============ ========== elapsed time output ============ ========== - 0 (0, 50) - 30 (30, 65) - 60 (60, 80) - 90 (90, 95) - **120** (100, 100) + 0 [0, 50] + 30 [30, 65] + 60 [60, 80] + 90 [90, 95] + **120** [100, 100] ============ ========== ''' zip_ = zip - slope = tuple(end_elem - start_elem for end_elem, start_elem in zip_(end, start)) + slope = [end_elem - start_elem for end_elem, start_elem in zip_(end, start)] p = transition(0.) - yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start)) + yield [p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start)] if duration: async for p in self.anim_with_ratio(step=step, base=duration): if p >= 1.0: break p = transition(p) - yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start)) + yield [p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start)] else: await self.sleep(0) p = transition(1.) - yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start)) + yield [p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start)] interpolate_seq = interpolate_sequence ''' An alias for :meth:`interpolate_sequence`. ''' - def _update(setattr, zip, min, obj, duration, transition, output_seq_type, anim_params, task, p_time, dt): + def _update(setattr, zip, min, obj, duration, transition, anim_params, task, p_time, dt): time = p_time[0] + dt p_time[0] = time @@ -394,10 +394,10 @@ def _update(setattr, zip, min, obj, duration, transition, output_seq_type, anim_ # apply progression on obj for attr_name, org_value, slope, is_seq in anim_params: if is_seq: - new_value = output_seq_type( + new_value = [ slope_elem * t + org_elem for org_elem, slope_elem in zip(org_value, slope) - ) + ] setattr(obj, attr_name, new_value) else: setattr(obj, attr_name, slope * t + org_value) @@ -410,26 +410,26 @@ def _update(setattr, zip, min, obj, duration, transition, output_seq_type, anim_ @types.coroutine def _anim_attrs( - self, obj, duration, step, transition, output_seq_type, animated_properties, + self, obj, duration, step, transition, animated_properties, getattr=getattr, isinstance=isinstance, tuple=tuple, partial=partial, native_seq_types=(tuple, list), zip=zip, _update=_update, _current_task=_current_task, _sleep_forever=_sleep_forever, /): # get current values & calculate slopes - anim_params = tuple( + anim_params = [ ( org_value := getattr(obj, attr_name), is_seq := isinstance(org_value, native_seq_types), ( org_value := tuple(org_value), - slope := tuple(goal_elem - org_elem for goal_elem, org_elem in zip(goal_value, org_value)), + slope := [goal_elem - org_elem for goal_elem, org_elem in zip(goal_value, org_value)], ) if is_seq else (slope := goal_value - org_value), ) and (attr_name, org_value, slope, is_seq, ) for attr_name, goal_value in animated_properties.items() - ) + ] try: event = self.schedule_interval( - partial(_update, obj, duration, transition, output_seq_type, anim_params, (yield _current_task)[0][0], [0, ]), + partial(_update, obj, duration, transition, anim_params, (yield _current_task)[0][0], [0, ]), step, ) yield _sleep_forever @@ -438,8 +438,7 @@ def _anim_attrs( del _update - def anim_attrs(self, obj, *, duration, step=0, transition=_linear, output_seq_type=tuple, - **animated_properties) -> Awaitable: + def anim_attrs(self, obj, *, duration, step=0, transition=_linear, **animated_properties) -> Awaitable: ''' Animates attibutes of any object. @@ -449,22 +448,14 @@ def anim_attrs(self, obj, *, duration, step=0, transition=_linear, output_seq_ty obj = types.SimpleNamespace(x=0, size=(200, 300)) await clock.anim_attrs(obj, x=100, size=(400, 400), duration=2) - - The ``output_seq_type`` parameter. - - .. code-block:: - - obj = types.SimpleNamespace(size=(200, 300)) - await clock.anim_attrs(obj, size=(400, 400), duration=2, output_seq_type=list) - assert type(obj.size) is list ''' - return self._anim_attrs(obj, duration, step, transition, output_seq_type, animated_properties) + return self._anim_attrs(obj, duration, step, transition, animated_properties) - def anim_attrs_abbr(self, obj, *, d, s=0, t=_linear, output_seq_type=tuple, **animated_properties) -> Awaitable: + def anim_attrs_abbr(self, obj, *, d, s=0, t=_linear, **animated_properties) -> Awaitable: ''' :meth:`anim_attrs` cannot animate attributes named ``step``, ``duration`` and ``transition`` but this one can. ''' - return self._anim_attrs(obj, d, s, t, output_seq_type, animated_properties) + return self._anim_attrs(obj, d, s, t, animated_properties) class _repeat_sleeping: diff --git a/tests/clock/test_anim_attrs.py b/tests/clock/test_anim_attrs.py index 79211e9..e585a2b 100644 --- a/tests/clock/test_anim_attrs.py +++ b/tests/clock/test_anim_attrs.py @@ -38,17 +38,3 @@ def test_sequence(clock): clock.tick(30) assert obj.pos == approx([100, 0]) assert task.finished - - -@pytest.mark.parametrize('output_seq_type', [list, tuple]) -def test_seq_type_parameter(clock, output_seq_type): - from types import SimpleNamespace - import asyncgui - - obj = SimpleNamespace(size=(0, 0), pos=[0, 0]) - task = asyncgui.start(clock.anim_attrs(obj, size=[10, 10], pos=(10, 10), duration=10, output_seq_type=output_seq_type)) - clock.tick(0) - assert type(obj.size) is output_seq_type - assert type(obj.pos) is output_seq_type - assert not task.finished - task.cancel()