Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/bokeh/driving.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def update(i):
from __future__ import annotations

import logging # isort:skip

log = logging.getLogger(__name__)

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -153,7 +154,8 @@ def f(i: float) -> float:
return partial(force, sequence=_advance(f))

def repeat(sequence: Sequence[int]) -> partial[Callable[[], None]]:
''' Return a driver function that can advance a repeated of values.
""" Return a driver function that can advance a repeated of values.


.. code-block:: none

Expand All @@ -164,10 +166,12 @@ def repeat(sequence: Sequence[int]) -> partial[Callable[[], None]]:
Args:
sequence (seq) : a sequence of values for the driver to bounce

'''
"""
# Precompute len(sequence)
N = len(sequence)
def f(i: int) -> int:
return sequence[i%N]
# Use a function instead of a closure to avoid slow closure cell dereference
def f(i: int, sequence=sequence, N=N) -> int:
return sequence[i % N]
return partial(force, sequence=_advance(f))

def sine(w: float, A: float = 1, phi: float = 0, offset: float = 0) -> partial[Callable[[], None]]:
Expand Down Expand Up @@ -200,7 +204,7 @@ def f(i: float) -> float:
T = TypeVar("T")

def _advance(f: Callable[[int], T]) -> Iterator[T]:
''' Yield a sequence generated by calling a given function with
""" Yield a sequence generated by calling a given function with
successively incremented integer values.

Args:
Expand All @@ -210,7 +214,8 @@ def _advance(f: Callable[[int], T]) -> Iterator[T]:
Yields:
f(i) where i increases each call

'''
"""
# Use a local variable for faster 'i' access
i = 0
while True:
yield f(i)
Expand Down