]> git.parisson.com Git - timeside.git/commitdiff
timeside/analyzer/core.py: all to_[format] functions to take an optional output_file...
authorPaul Brossier <piem@piem.org>
Tue, 3 Dec 2013 16:45:52 +0000 (11:45 -0500)
committerPaul Brossier <piem@piem.org>
Tue, 3 Dec 2013 16:45:52 +0000 (11:45 -0500)
timeside/analyzer/core.py

index 609bc9455858096f1b0daa23b42ccc6f58a8c679..e7ff54a0c498e822cbcd75fd7718e47b0875a144 100644 (file)
@@ -821,7 +821,7 @@ class AnalyzerResultContainer(dict):
                          analyzer_result)
         #self.results += [analyzer_result]
 
-    def to_xml(self):
+    def to_xml(self, output_file = None):
 
         import xml.etree.ElementTree as ET
         # TODO : cf. telemeta util
@@ -831,7 +831,9 @@ class AnalyzerResultContainer(dict):
             if result is not None:
                 root.append(ET.fromstring(result.to_xml()))
 
-        return ET.tostring(root, encoding="utf-8", method="xml")
+        xml_str = ET.tostring(root, encoding="utf-8", method="xml")
+        if output_file: open(output_file, 'w').write(xml_str)
+        else: return xml_str
 
     @staticmethod
     def from_xml(xml_string):
@@ -847,7 +849,7 @@ class AnalyzerResultContainer(dict):
 
         return results
 
-    def to_json(self):
+    def to_json(self, output_file = None):
         #if data_list == None: data_list = self.results
         import simplejson as json
 
@@ -858,8 +860,10 @@ class AnalyzerResultContainer(dict):
                         'dtype': obj.dtype.__str__()}
             raise TypeError(repr(obj) + " is not JSON serializable")
 
-        return json.dumps([res.as_dict() for res in self.values()],
+        json_str = json.dumps([res.as_dict() for res in self.values()],
                           default=NumpyArrayEncoder)
+        if output_file: open(output_file, 'w').write(json_str)
+        else: return json_str
 
     @staticmethod
     def from_json(json_str):
@@ -887,7 +891,7 @@ class AnalyzerResultContainer(dict):
             results.add(res)
         return results
 
-    def to_yaml(self):
+    def to_yaml(self, output_file):
         #if data_list == None: data_list = self.results
         import yaml
 
@@ -899,7 +903,9 @@ class AnalyzerResultContainer(dict):
 
         yaml.add_representer(numpy.ndarray, numpyArray_representer)
 
-        return yaml.dump([res.as_dict() for res in self.values()])
+        yaml_str = yaml.dump([res.as_dict() for res in self.values()])
+        if output_file: open(output_file, 'w').write(yaml_str)
+        else: return yaml_str
 
     @staticmethod
     def from_yaml(yaml_str):