Hakan Ardo avatar Hakan Ardo committed 707e5b6

simplify stateless case

Comments (0)

Files changed (3)

talk/dls2012/demo/analytics.py

 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)

talk/dls2012/demo/foreground.py

-from reloader import ReloadHack
+from reloader import autoreload
 
-class Foreground(ReloadHack):
-    def update(self, img, bkg):
-        return ((bkg - img) ** 2) > 100
+@autoreload
+def foreground(img, bkg):
+    return ((bkg - img) ** 2) > 100
 

talk/dls2012/demo/reloader.py

 
             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
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.