Skip to content

Commit b3c07a8

Browse files
(api-break) ratio系のメソッドを無限ループするようにし、引数durationをbaseに置き換え
1 parent ad7fd21 commit b3c07a8

File tree

2 files changed

+72
-46
lines changed

2 files changed

+72
-46
lines changed

src/asyncgui_ext/clock.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,19 @@ def callback(dt):
210210

211211
async def anim_with_et(self, *, step=0) -> AsyncIterator[TimeUnit]:
212212
'''
213-
Same as :meth:`anim_with_dt` except this one generates the total elapsed time of the loop instead of the elapsed
214-
time between frames.
215-
216213
.. code-block::
217214
218-
timeout = ...
219215
async for et in clock.anim_with_et():
220-
...
221-
if et > timeout:
222-
break
216+
print(et)
217+
218+
The code above is equivalent to the below.
219+
220+
.. code-block::
221+
222+
et = 0
223+
async for dt in clock.anim_with_dt():
224+
et += dt
225+
print(et)
223226
'''
224227
et = 0
225228
async with _repeat_sleeping(self, step) as sleep:
@@ -243,54 +246,62 @@ async def anim_with_dt_et(self, *, step=0) -> AsyncIterator[tuple[TimeUnit, Time
243246
et += dt
244247
yield dt, et
245248

246-
async def anim_with_ratio(self, *, duration, step=0) -> AsyncIterator[float]:
249+
async def anim_with_ratio(self, *, base, step=0) -> AsyncIterator[float]:
247250
'''
248-
Same as :meth:`anim_with_et` except this one generates the total progression ratio of the loop.
249-
250251
.. code-block::
251252
252-
async for p in self.anim_with_ratio(duration=...):
253+
async for p in clock.anim_with_ratio(base=100):
253254
print(p * 100, "%")
254255
256+
The code above is equivalent to the below.
257+
258+
.. code-block::
259+
260+
base = 100
261+
async for et in clock.anim_with_et():
262+
print(et / base * 100, "%")
263+
255264
If you want to progress at a non-consistant rate, you may find the
256265
`source code <https://github.com/kivy/kivy/blob/master/kivy/animation.py>`__
257266
of the :class:`kivy.animation.AnimationTransition` helpful.
258267
259268
.. code-block::
260269
261-
async for p in clock.anim_with_ratio(duration=...):
270+
async for p in clock.anim_with_ratio(base=...):
262271
p = p * p # quadratic
263272
print(p * 100, "%")
273+
274+
.. versionchanged:: 0.5.0
275+
276+
The ``duration`` parameter was replaced with the ``base`` parameter.
277+
The loop no longer stops when the progression reaches 1.0.
264278
'''
265-
if not duration:
266-
await self.sleep(step)
267-
yield 1.0
268-
return
269279
et = 0
270280
async with _repeat_sleeping(self, step) as sleep:
271-
while et < duration:
281+
while True:
272282
et += await sleep()
273-
yield et / duration
283+
yield et / base
274284

275-
async def anim_with_dt_et_ratio(self, *, duration, step=0) -> AsyncIterator[tuple[TimeUnit, TimeUnit, float]]:
285+
async def anim_with_dt_et_ratio(self, *, base, step=0) -> AsyncIterator[tuple[TimeUnit, TimeUnit, float]]:
276286
'''
277287
:meth:`anim_with_dt`, :meth:`anim_with_et` and :meth:`anim_with_ratio` combined.
278288
279289
.. code-block::
280290
281291
async for dt, et, p in clock.anim_with_dt_et_ratio(...):
282292
...
293+
294+
.. versionchanged:: 0.5.0
295+
296+
The ``duration`` parameter was replaced with the ``base`` parameter.
297+
The loop no longer stops when the progression reaches 1.0.
283298
'''
284299
async with _repeat_sleeping(self, step) as sleep:
285-
if not duration:
286-
dt = await sleep()
287-
yield dt, dt, 1.0
288-
return
289300
et = 0.
290-
while et < duration:
301+
while True:
291302
dt = await sleep()
292303
et += dt
293-
yield dt, et, et / duration
304+
yield dt, et, et / base
294305

295306
def _linear(p):
296307
return p
@@ -316,10 +327,13 @@ async def interpolate_scalar(self, start, end, *, duration, step=0, transition=_
316327
'''
317328
slope = end - start
318329
yield transition(0.) * slope + start
319-
async for p in self.anim_with_ratio(step=step, duration=duration):
320-
if p >= 1.0:
321-
break
322-
yield transition(p) * slope + start
330+
if duration:
331+
async for p in self.anim_with_ratio(step=step, base=duration):
332+
if p >= 1.0:
333+
break
334+
yield transition(p) * slope + start
335+
else:
336+
await self.sleep(0)
323337
yield transition(1.) * slope + start
324338

325339
async def interpolate_sequence(self, start, end, *, duration, step=0, transition=_linear, output_type=tuple) -> AsyncIterator:
@@ -347,11 +361,14 @@ async def interpolate_sequence(self, start, end, *, duration, step=0, transition
347361
p = transition(0.)
348362
yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start))
349363

350-
async for p in self.anim_with_ratio(step=step, duration=duration):
351-
if p >= 1.0:
352-
break
353-
p = transition(p)
354-
yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start))
364+
if duration:
365+
async for p in self.anim_with_ratio(step=step, base=duration):
366+
if p >= 1.0:
367+
break
368+
p = transition(p)
369+
yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start))
370+
else:
371+
await self.sleep(0)
355372

356373
p = transition(1.)
357374
yield output_type(p * slope_elem + start_elem for slope_elem, start_elem in zip_(slope, start))

tests/clock/test_anim_with_xxx.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import pytest
2+
3+
14
def test_anim_with_dt(clock):
25
from asyncgui import start
36
dt_list = []
@@ -50,8 +53,10 @@ def test_anim_with_ratio(clock):
5053
p_list = []
5154

5255
async def async_fn():
53-
async for p in clock.anim_with_ratio(step=10, duration=100):
56+
async for p in clock.anim_with_ratio(step=10, base=100):
5457
p_list.append(p)
58+
if p >= 1:
59+
break
5560

5661
task = start(async_fn())
5762
assert p_list == []
@@ -70,21 +75,22 @@ async def async_fn():
7075
assert task.finished
7176

7277

73-
def test_anim_with_ratio_zero_duration(clock):
78+
def test_anim_with_ratio_zero_base(clock):
7479
from asyncgui import start
7580
p_list = []
7681

7782
async def async_fn():
78-
async for p in clock.anim_with_ratio(step=10, duration=0):
83+
async for p in clock.anim_with_ratio(step=10, base=0):
7984
p_list.append(p)
8085

8186
task = start(async_fn())
8287
assert p_list == []
8388
clock.tick(6)
8489
assert p_list == []
85-
clock.tick(6)
86-
assert p_list == [1.0, ]
87-
assert task.finished
90+
with pytest.raises(ZeroDivisionError):
91+
clock.tick(6)
92+
assert p_list == []
93+
assert task.cancelled
8894

8995

9096
def test_anim_with_dt_et(clock):
@@ -117,8 +123,10 @@ def test_anim_with_dt_et_ratio(clock):
117123
values = []
118124

119125
async def async_fn():
120-
async for v in clock.anim_with_dt_et_ratio(step=10, duration=100):
126+
async for v in clock.anim_with_dt_et_ratio(step=10, base=100):
121127
values.extend(v)
128+
if values[2] >= 1:
129+
break
122130

123131
task = start(async_fn())
124132
assert values == []
@@ -146,18 +154,19 @@ async def async_fn():
146154
assert task.finished
147155

148156

149-
def test_anim_with_dt_et_ratio_zero_duration(clock):
157+
def test_anim_with_dt_et_ratio_zero_base(clock):
150158
from asyncgui import start
151159
values = []
152160

153161
async def async_fn():
154-
async for v in clock.anim_with_dt_et_ratio(step=10, duration=0):
162+
async for v in clock.anim_with_dt_et_ratio(step=10, base=0):
155163
values.extend(v)
156164

157165
task = start(async_fn())
158166
assert values == []
159167
clock.tick(6)
160168
assert values == []
161-
clock.tick(6)
162-
assert values == [12, 12, 1.0]
163-
assert task.finished
169+
with pytest.raises(ZeroDivisionError):
170+
clock.tick(6)
171+
assert values == []
172+
assert task.cancelled

0 commit comments

Comments
 (0)