Commits

Chris Mutel committed ece9303

Make data extractors with class methods

Comments (0)

Files changed (2)

bw2data/io/import_ecospold.py

 
 
 class Ecospold1DataExtractor(object):
-    def extract(self, path, log):
+    @classmethod
+    def extract(cls, path, log):
         data = []
         if os.path.isdir(path):
             files = [os.path.join(path, y) for y in filter(
                 continue
 
             for dataset in root.iterchildren():
-                data.append(self.process_dataset(dataset))
+                data.append(cls.process_dataset(dataset))
 
             pbar.update(index)
         pbar.finish()
         return data
 
-    def process_dataset(self, dataset):
+    @classmethod
+    def process_dataset(cls, dataset):
         ref_func = dataset.metaInformation.processInformation.\
             referenceFunction
         data = {
                 geography.get("location"),
             "code": int(dataset.get("number")),
             "unit": normalize_units(ref_func.get("unit")),
-            "exchanges": self.process_exchanges(dataset)
+            "exchanges": cls.process_exchanges(dataset)
             }
         # Convert ("foo", "unspecified") to ("foo",)
         while data["categories"] and data["categories"][-1] in (
             data["categories"] = data["categories"][:-1]
         return data
 
-    def process_exchanges(self, dataset):
+    @classmethod
+    def process_exchanges(cls, dataset):
         data = []
         # Skip definitional exchange - we assume this already
         for exc in dataset.flowData.iterchildren():
             if exc.tag in (
                     "{http://www.EcoInvent.org/EcoSpold01}exchange",
                     "exchange"):
-                data.append(self.process_exchange(exc, dataset))
+                data.append(cls.process_exchange(exc, dataset))
             elif exc.tag in (
                     "{http://www.EcoInvent.org/EcoSpold01}allocation",
                     "allocation"):
-                data.append(self.process_allocation(exc, dataset))
+                data.append(cls.process_allocation(exc, dataset))
             else:
                 raise ValueError("Flow data type %s not understood" % exc.tag)
         return data
 
-    def process_allocation(self, exc, dataset):
+    @classmethod
+    def process_allocation(cls, exc, dataset):
         return {
             "reference": int(exc.get("referenceToCoProduct")),
             "fraction": float(exc.get("fraction")),
             "exchanges": [int(c.text) for c in exc.iterchildren()]
         }
 
-    def process_exchange(self, exc, dataset):
+    @classmethod
+    def process_exchange(cls, exc, dataset):
         # if exc.get("name") == dataset.metaInformation.processInformation.\
         #         referenceFunction.get("name") != None and float(
         #         exc.get("meanValue", 0.)) == 1.0:
 
         if exc.get("generalComment"):
             data["comment"] = exc.get("generalComment")
-        return self.process_uncertainty_fields(exc, data)
+        return cls.process_uncertainty_fields(exc, data)
 
-    def process_uncertainty_fields(self, exc, data):
+    @classmethod
+    def process_uncertainty_fields(cls, exc, data):
         uncertainty = int(exc.get("uncertaintyType", 0))
 
         def floatish(x):
         """
 
         if LognormalUncertainty is None:
-            print "``stats_array`` not installed!"
+            warnings.warn("``stats_array`` not installed!")
             return
 
         self.log, self.logfile = get_io_logger("lci-import")
         self.new_activities = []
         self.new_biosphere = []
 
-        data = Ecospold1DataExtractor().extract(path, self.log)
+        data = Ecospold1DataExtractor.extract(path, self.log)
         data = self.allocate_datasets(data)
         data = self.apply_transforms(data)
         data = self.add_hashes(data)

bw2data/io/import_ecospold2.py

 
 
 class Ecospold2DataExtractor(object):
-    def extract_metadata(self, dirpath):
+    @classmethod
+    def extract_metadata(cls, dirpath):
         for filename in (
             "IntermediateExchanges.xml",
             "ElementaryExchanges.xml"
         ):
             assert os.path.exists(os.path.join(dirpath, filename))
-        biosphere = self.extract_biosphere_metadata(dirpath)
-        technosphere = self.extract_technosphere_metadata(dirpath)
+        biosphere = cls.extract_biosphere_metadata(dirpath)
+        technosphere = cls.extract_technosphere_metadata(dirpath)
         return biosphere, technosphere
 
-    def extract_technosphere_metadata(self, dirpath):
+    @classmethod
+    def extract_technosphere_metadata(cls, dirpath):
         def extract_metadata(o):
             return {
                 'name': o.name.text,
         ).getroot()
         return [extract_metadata(ds) for ds in root.iterchildren()]
 
-    def extract_biosphere_metadata(self, dirpath):
+
+    @classmethod
+    def extract_biosphere_metadata(cls, dirpath):
         def extract_metadata(o):
             return {
                 'name': o.name.text,
         ).getroot()
         return [extract_metadata(ds) for ds in root.iterchildren()]
 
-    def extract_activities(self, dirpath):
+    @classmethod
+    def extract_activities(cls, dirpath):
         assert os.path.exists(dirpath)
         filelist = [filename for filename in os.listdir(dirpath)
                     if os.path.isfile(os.path.join(dirpath, filename))
 
         data = []
         for index, filename in enumerate(filelist):
-            data.append(self.extract_activity(dirpath, filename))
+            data.append(cls.extract_activity(dirpath, filename))
             pbar.update(index)
         pbar.finish()
 
         return data
 
-    def extract_activity(self, dirpath, filename):
+    @classmethod
+    def extract_activity(cls, dirpath, filename):
         root = objectify.parse(open(os.path.join(dirpath, filename))).getroot()
         if hasattr(root, "activityDataset"):
             stem = root.activityDataset
         data = {
             'name': stem.activityDescription.activity.activityName.text,
             'location': stem.activityDescription.geography.shortname.text,
-            'exchanges': [self.extract_exchange(exc) for exc in stem.flowData.iterchildren()],
+            'exchanges': [cls.extract_exchange(exc) for exc in stem.flowData.iterchildren()],
             'filename': filename,
             'activity': stem.activityDescription.activity.get('id')
         }
 
         return data
 
-    def extract(self, files_dir, meta_dir):
-        biosphere, technosphere = self.extract_metadata(meta_dir)
-        activities = self.extract_activities(files_dir)
+    @classmethod
+    def extract(cls, files_dir, meta_dir):
+        biosphere, technosphere = cls.extract_metadata(meta_dir)
+        activities = cls.extract_activities(files_dir)
         return activities, biosphere, technosphere
 
 
     def importer(self):
         self.log, self.logfile = get_io_logger("es3-import")
         # Note: Creates biosphere3 database
-        activities, biosphere, technosphere = Ecospold2DataExtractor().extract(
+        activities, biosphere, technosphere = Ecospold2DataExtractor.extract(
             self.datapath,
             self.metadatapath
         )