Added a lazy-loading dict of known, named libraries.
authorFredrik Tolf <fredrik@dolda2000.com>
Sat, 11 May 2013 14:22:05 +0000 (16:22 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Sat, 11 May 2013 14:22:05 +0000 (16:22 +0200)
manga/lib.py

index ffea7a3..2a11cb1 100644 (file)
@@ -171,3 +171,18 @@ class cursor(object):
 
     def __iter__(self):
         return self
+
+def _lazymod(name):
+    return __import__(name, fromlist=["dummy"])
+class _lazydict(object):
+    def __init__(self):
+        self.bk = {}
+    def __setitem__(self, key, val):
+        self.bk[key] = "u", val
+    def __getitem__(self, key):
+        st, v = self.bk[key]
+        if st == "u":
+            v = self.bk[key] = v()
+        return v
+libraries = _lazydict()
+libraries["mf"] = lambda: _lazymod("manga.mangafox").library()