FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 47ef63c4 authored by Mark Driver's avatar Mark Driver
Browse files

add class for output information.

parent 86e8d0ec
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
# phasecalculator calculates FGIPs, solvent similarity and VLE with SSIMPLE.
# Copyright (C) 2019 Mark D. Driver
#
# phasecalculator is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Class for output information.
@author: Mark
"""
import logging
from lxml import etree
from phasecalculator.classes.xmlnamespacing import PHASE_CALCULATOR, PHASE_CALC_NAMESPACE_DICT
logging.basicConfig()
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.WARN)
class OutputInformation(object):
"""Class representation of output information."""
def __init__(self, fgip_output, similarity_output, similarity_output_type, vle_output):
self.fgip_output = fgip_output
self.similarity_output = similarity_output
self.similarity_output_type = similarity_output_type
self.vle_output = vle_output
def __eq__(self, other):
"""Overload equality comparison operator.
Parameters
----------
other : object
object to compare.
Returns
-------
boolean
is equal.
"""
if other is None:
return False
if isinstance(other, type(self)):
return (self.fgip_output == other.fgip_output and self.similarity_output == other.similarity_output and self.vle_output == other.vle_output)
else:
return False
@classmethod
def parse_xml(cls, output_xml):
fgip_output = OutputInformation.parse_fgip_output(output_xml)
similarity_output, similarity_output_type = OutputInformation.parse_similarity_output(output_xml)
vle_output = OutputInformation.parse_vle_output(output_xml)
return OutputInformation(fgip_output, similarity_output, similarity_output_type, vle_output)
@classmethod
def parse_fgip_output(cls, output_xml):
xpath_expression = "phasecalc:FGIPOutput/text()"
fgip_output = bool(output_xml.xpath(xpath_expression, namespaces=PHASE_CALC_NAMESPACE_DICT)[0])
return fgip_output
@classmethod
def parse_similarity_output(cls, output_xml):
xpath_expression = "phasecalc:SimilarityOutput/text()"
similarity_output = bool(output_xml.xpath(xpath_expression, namespaces=PHASE_CALC_NAMESPACE_DICT)[0])
if similarity_output:
type_xpath = "phasecalc:SimilarityOutput/@phasecalc:outputType"
similarity_output_type = output_xml.xpath(type_xpath, namespaces=PHASE_CALC_NAMESPACE_DICT)[0]
else:
similarity_output_type = None
return similarity_output, similarity_output_type
@classmethod
def parse_vle_output(cls, output_xml):
xpath_expression = "phasecalc:FGIPOutput/text()"
vle_output = bool(output_xml.xpath(xpath_expression, namespaces=PHASE_CALC_NAMESPACE_DICT)[0])
return vle_output
def write_to_xml(self):
"""Write information to Etree representation of XML.
Returns
-------
out_element : lxml.etree.Element
XML representation of OutputInformation.
"""
out_element = etree.Element(PHASE_CALCULATOR + "OutputInformation", nsmap=PHASE_CALC_NAMESPACE_DICT)
out_element.append(self.write_fgip_output())
out_element.append(self.write_similarity_output())
out_element.append(self.write_vle_output())
return out_element
def write_fgip_output(self):
fgip_element = etree.Element(PHASE_CALCULATOR + "FGIPOutput", nsmap=PHASE_CALC_NAMESPACE_DICT)
fgip_element.text = str(self.fgip_output)
return fgip_element
def write_similarity_output(self):
sim_element = etree.Element(PHASE_CALCULATOR + "SimilarityOutput", nsmap=PHASE_CALC_NAMESPACE_DICT)
sim_element.text = str(self.similarity_output)
if self.similarity_output_type is not None:
sim_element.set(PHASE_CALCULATOR + "outputType", self.similarity_output_type)
return sim_element
def write_vle_output(self):
vle_element = etree.Element(PHASE_CALCULATOR + "VLEOutput", nsmap=PHASE_CALC_NAMESPACE_DICT)
vle_element.text = str(self.vle_output)
return vle_element
\ No newline at end of file
# -*- coding: utf-8 -*-
# phasecalculator calculates FGIPs, solvent similarity and VLE with SSIMPLE.
# Copyright (C) 2019 Mark D. Driver
#
# phasecalculator is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Script for output information class tests.
@author: Mark
"""
import logging
from lxml import etree
import unittest
from phasecalculator.classes.outputinformation import OutputInformation
logging.basicConfig()
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.WARN)
class OutputInformationTestCase(unittest.TestCase):
"""Test case for OutputInformation class."""
def setUp(self):
"""Set up for tests.
Returns
-------
None.
"""
self.out_inf = OutputInformation(True, True, "all", True)
def tearDown(self):
"""Clean up after tests.
Returns
-------
None.
"""
def test_parse_xml(self):
"""Test expected information is parsed.
Returns
-------
None.
"""
actual_output = OutputInformation.parse_xml(self.out_inf.write_to_xml())
self.assertEqual(self.out_inf, actual_output)
def test_parse_fgip_output(self):
"""Test expected information is parsed.
Returns
-------
None.
"""
actual_output = OutputInformation.parse_fgip_output(self.out_inf.write_to_xml())
self.assertTrue(actual_output)
def test_parse_similarity_output(self):
"""Test expected information is parsed.
Returns
-------
None.
"""
actual_output, actual_type = OutputInformation.parse_similarity_output(self.out_inf.write_to_xml())
self.assertTrue(actual_output)
self.assertEqual("all", actual_type)
def test_parse_vle_output(self):
"""Test expected information is parsed.
Returns
-------
None.
"""
actual_output = OutputInformation.parse_vle_output(self.out_inf.write_to_xml())
self.assertTrue(actual_output)
def test_write_to_xml(self):
"""Test expected XML produced.
Returns
-------
None.
"""
expected_xml = """<phasecalc:OutputInformation xmlns:phasecalc="http://www-hunter.ch.cam.ac.uk/schema/PhaseCalculatorSchema.xsd">
<phasecalc:FGIPOutput>True</phasecalc:FGIPOutput>
<phasecalc:SimilarityOutput phasecalc:outputType="all">True</phasecalc:SimilarityOutput>
<phasecalc:VLEOutput>True</phasecalc:VLEOutput>
</phasecalc:OutputInformation>
"""
actual_xml = self.out_inf.write_to_xml()
self.assertMultiLineEqual(expected_xml, etree.tounicode(actual_xml, pretty_print=True))
def test_write_fgip_output(self):
"""Test expected XML produced.
Returns
-------
None.
"""
expected_xml = """<phasecalc:FGIPOutput xmlns:phasecalc="http://www-hunter.ch.cam.ac.uk/schema/PhaseCalculatorSchema.xsd">True</phasecalc:FGIPOutput>
"""
actual_xml = self.out_inf.write_fgip_output()
self.assertMultiLineEqual(expected_xml, etree.tounicode(actual_xml, pretty_print=True))
def test_write_similarity_output(self):
"""Test expected XML produced.
Returns
-------
None.
"""
expected_xml = """<phasecalc:SimilarityOutput xmlns:phasecalc="http://www-hunter.ch.cam.ac.uk/schema/PhaseCalculatorSchema.xsd" phasecalc:outputType="all">True</phasecalc:SimilarityOutput>
"""
actual_xml = self.out_inf.write_similarity_output()
self.assertMultiLineEqual(expected_xml, etree.tounicode(actual_xml, pretty_print=True))
def test_write_vle_output(self):
"""Test expected XML produced.
Returns
-------
None.
"""
expected_xml = """<phasecalc:VLEOutput xmlns:phasecalc="http://www-hunter.ch.cam.ac.uk/schema/PhaseCalculatorSchema.xsd">True</phasecalc:VLEOutput>
"""
actual_xml = self.out_inf.write_vle_output()
self.assertMultiLineEqual(expected_xml, etree.tounicode(actual_xml, pretty_print=True))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment