Chris Mutel avatar Chris Mutel committed 02b9bd8

Create CompoundJSONDict subclass

Comments (0)

Files changed (2)

-from serialization import SerializedDict, PickledDict
+from serialization import SerializedDict, PickledDict, CompoundJSONDict
 
 
 class Mapping(PickledDict):
             self.data)
 
 
-class Methods(SerializedDict):
+class Methods(CompoundJSONDict):
     """A dictionary for method metadata. File data is saved in ``methods.json``."""
     _filename = "methods.json"
 
-    def pack(self, data):
-        """Transform the dictionary to a list because JSON can't handle lists as keys"""
-        return [(k, v) for k, v in data.iteritems()]
-
-    def unpack(self, data):
-        """Transform data back to a dictionary"""
-        return dict([(tuple(x[0]), x[1]) for x in data])
-
     def __unicode__(self):
         return u"Brightway2 methods metadata with %i objects" % len(
             self.data)

bw2data/serialization.py

 
     def deserialize(self):
         return self.unpack(pickle.load(open(self._filepath, "rb")))
+
+
+class CompoundJSONDict(SerializedDict):
+    """Subclass of ``SerializedDict`` that allows tuples as dictionary keys."""
+    def pack(self, data):
+        """Transform the dictionary to a list because JSON can't handle lists as keys"""
+        return [(k, v) for k, v in data.iteritems()]
+
+    def unpack(self, data):
+        """Transform data back to a dictionary"""
+        return dict([(tuple(x[0]), x[1]) for x in data])
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.