Chris Mutel avatar Chris Mutel committed 2cee725

Add optional caching of inventory databases

Comments (0)

Files changed (4)

brightway2/_config.py

 import tempfile
 
 
+class Cache(object):
+    def __init__(self, use_me):
+        self.use = use_me
+        self.data = {}
+
+    def __getitem__(self, key):
+        if not self.use:
+            return False
+        return self.data[key]
+
+    def __setitem__(self, key, value):
+        if not self.use:
+            return False
+        else:
+            self.data[key] = value
+            return True
+
+    def __contains__(self, key):
+        return self.use and key in self.data
+
+
 class Config(object):
     """A singleton that store configuration settings. Default data directory is ``brightway`` in the user's home directory, and is stored as ``config.dir``. Other configuration settings can also be assigned as needed.
 
     def __init__(self, path=None):
         self.is_temp_dir = False
         self.reset(path)
+        self.cache = Cache(self.p["use_cache"])
 
     def check_dir(self, dir=None):
         """Check is directory is writeable."""
             self.p = json.load(open(os.path.join(
                 self.dir, "preferences.json")))
         except:
-            self.p = {}
+            self.p = {"use_cache": True}
 
     def save_preferences(self):
         """Serialize preferences to disk."""

brightway2/database.py

         """
         if self.database not in databases:
             raise UnknownObject("This database is not yet registered")
+        if version == None and config.p["use_cache"] and \
+                self.database in config.cache:
+            return config.cache[self.database]
         try:
-            return pickle.load(open(os.path.join(config.dir, "intermediate",
+            data = pickle.load(open(os.path.join(config.dir, "intermediate",
                 self.filename(version)), "rb"))
+            if version == None and config.p["use_cache"]:
+                config.cache[self.database] = data
+            return data
         except OSError:
             raise MissingIntermediateData("This version (%i) not found" % version)
 
 # The short X.Y version.
 version = '0.7'
 # The full version, including alpha/beta/rc tags.
-release = '0.7'
+release = '0.7.1'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
 
 setup(
   name='brightway2',
-  version="0.7",
+  version="0.7.1",
   packages=packages,
   author="Chris Mutel",
   author_email="cmutel@gmail.com",
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.