Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #246 by Schedule functions with arguments #318

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ repos:
hooks:
- id: flake8
exclude: "^(doc|examples)/"
args:
- --ignore=E303
2 changes: 1 addition & 1 deletion pgzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

"""

__version__ = '1.3.dev0'
__version__ = '1.3.dev1'
2 changes: 1 addition & 1 deletion pgzero/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,4 @@ def distance_to(self, target):
return sqrt(dx * dx + dy * dy)

def unload_image(self):
loaders.images.unload(self._image_name)
loaders.images.unload(self._image_name)
25 changes: 22 additions & 3 deletions pgzero/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from weakref import ref
from functools import total_ordering
from types import MethodType
from functools import partial
from collections import defaultdict

__all__ = [
'Clock', 'schedule', 'schedule_interval', 'unschedule'
Expand All @@ -34,6 +36,11 @@ def weakref():
def mkref(o):
if isinstance(o, MethodType):
return weak_method(o)
elif isinstance(o, partial):
if o.args or o.keywords:
return o
else:
return o.func
else:
try:
return ref(o)
Expand Down Expand Up @@ -66,7 +73,13 @@ def __eq__(self, ano):

@property
def callback(self):
return self.cb()
cb = self.cb()
if cb is None:
return None
if isinstance(cb, partial):
return cb.func if cb.args or cb.keywords else cb
return cb.__func__ if isinstance(cb, MethodType) else cb



class Clock:
Expand All @@ -89,6 +102,7 @@ def __init__(self):
self.fired = False
self.events = []
self._each_tick = []
self._scheduled_callbacks = defaultdict(int)

def clear(self):
"""Remove all handlers from this clock."""
Expand All @@ -113,8 +127,13 @@ def schedule_unique(self, callback, delay):
:param delay: The delay before the call (in clock time / seconds).

"""
self.unschedule(callback)
self.schedule(callback, delay)
count = self._scheduled_callbacks[callback]
if count == 0:
self._scheduled_callbacks[callback] = 1
self.schedule(callback, delay)
else:
self._scheduled_callbacks[callback] = count + 1


def schedule_interval(self, callback, delay):
"""Schedule callback to be called every `delay` seconds.
Expand Down
2 changes: 1 addition & 1 deletion pgzero/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ def fadeout(seconds):
get_volume = _music.get_volume
set_volume = _music.set_volume
get_pos = _music.get_pos
set_pos = _music.set_pos
set_pos = _music.set_pos