From 945d739d3bd43619d405d81c7fa19749002249cb Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 3 Dec 2013 11:45:52 -0500 Subject: [PATCH] timeside/analyzer/core.py: all to_[format] functions to take an optional output_file argument --- timeside/analyzer/core.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/timeside/analyzer/core.py b/timeside/analyzer/core.py index 609bc94..e7ff54a 100644 --- a/timeside/analyzer/core.py +++ b/timeside/analyzer/core.py @@ -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): -- 2.39.5