Commits

Chris Mutel  committed 02b9bd8

Create CompoundJSONDict subclass

  • Participants
  • Parent commits f6009b3

Comments (0)

Files changed (2)

File bw2data/meta.py

-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)

File 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])