[pypy-commit] extradoc extradoc: simplify stateless case
hakanardo
noreply at buildbot.pypy.org
Wed Sep 12 09:15:11 CEST 2012
Author: Hakan Ardo <hakan at debian.org>
Branch: extradoc
Changeset: r4793:707e5b6e3650
Date: 2012-09-12 09:14 +0200
http://bitbucket.org/pypy/extradoc/changeset/707e5b6e3650/
Log: simplify stateless case
diff --git a/talk/dls2012/demo/analytics.py b/talk/dls2012/demo/analytics.py
--- a/talk/dls2012/demo/analytics.py
+++ b/talk/dls2012/demo/analytics.py
@@ -1,14 +1,13 @@
from reloader import ReloadHack
from io import view
from background import Background
-from foreground import Foreground
+from foreground import foreground
class Tracker(ReloadHack):
def __init__(self):
self.bkg = Background()
- self.fg = Foreground()
def update(self, img):
background = self.bkg.update(img)
- fg = self.fg.update(img, background)
+ fg = foreground(img, background)
view(255*fg)
diff --git a/talk/dls2012/demo/foreground.py b/talk/dls2012/demo/foreground.py
--- a/talk/dls2012/demo/foreground.py
+++ b/talk/dls2012/demo/foreground.py
@@ -1,6 +1,6 @@
-from reloader import ReloadHack
+from reloader import autoreload
-class Foreground(ReloadHack):
- def update(self, img, bkg):
- return ((bkg - img) ** 2) > 100
+ at autoreload
+def foreground(img, bkg):
+ return ((bkg - img) ** 2) > 100
diff --git a/talk/dls2012/demo/reloader.py b/talk/dls2012/demo/reloader.py
--- a/talk/dls2012/demo/reloader.py
+++ b/talk/dls2012/demo/reloader.py
@@ -12,31 +12,72 @@
def update(self, *args, **kwargs):
while True:
+ while True:
+ try:
+ mtime = os.stat(self.filename).st_mtime
+ except OSError:
+ pass
+ else:
+ try:
+ if mtime > self.mtime:
+ self.mtime = mtime
+ reload(self.module)
+ cls = getattr(self.module, self.name)
+ self.obj = object.__new__(cls)
+ self.obj.__init__(*new_args, **new_kwargs)
+ self.halted = False
+ except Exception as e:
+ print
+ traceback.print_exc()
+ else:
+ if not self.halted:
+ break
try:
- mtime = os.stat(self.filename).st_mtime
- except OSError:
- pass
- else:
- try:
- if mtime > self.mtime:
- self.mtime = mtime
- reload(self.module)
- cls = getattr(self.module, self.name)
- self.obj = object.__new__(cls)
- self.obj.__init__(*new_args, **new_kwargs)
- self.halted = False
- except Exception as e:
- print
- traceback.print_exc()
- else:
- if not self.halted:
- break
- try:
- return self.obj.update(*args, **kwargs)
- except Exception as e:
- print
- traceback.print_exc()
- self.halted = True
+ return self.obj.update(*args, **kwargs)
+ except Exception as e:
+ print
+ traceback.print_exc()
+ self.halted = True
return Wrapper()
+def autoreload(fn):
+ module = sys.modules[fn.__module__]
+ filename = module.__file__
+ if filename.endswith('.pyc'):
+ filename = filename[:-1]
+ name = fn.__name__
+
+ def wrapper(*args, **kwargs):
+ halted = False
+ while True:
+ while True:
+ try:
+ mtime = os.stat(filename).st_mtime
+ except OSError:
+ pass
+ else:
+ try:
+ if mtime > wrapper.last_mtime:
+ wrapper.last_mtime = mtime
+ reload(module)
+ wrapper.fn = getattr(module, name).fn
+ halted = False
+ except Exception as e:
+ print
+ traceback.print_exc()
+ else:
+ if not halted:
+ break
+ try:
+ return wrapper.fn(*args, **kwargs)
+ except Exception as e:
+ print
+ traceback.print_exc()
+ halted = True
+ wrapper.fn = fn
+ wrapper.last_mtime = -1
+
+ return wrapper
+
+
More information about the pypy-commit
mailing list