Beispiel #1
0
def xml_to_object(elem, base_object=None, set_fn=None, file_version=1, blacklist=[]):
    """Convert the XML elements into python objects, and place these into the base object.

    @param elem:            The element to extract all python objects from.
    @type elem:             xml.dom.minidom.Element instance
    @keyword base_object:   The python class instance to place the objects into.
    @type base_object:      instance
    @keyword set_fn:        A function used to replace setattr for placing the object into the base
                            object.
    @type set_fn:           function
    @keyword file_version:  The relax XML version of the XML file.
    @type file_version:     int
    @keyword blacklist:     A list of object names to exclude.
    @type blacklist:        list of str
    """

    # Loop over the nodes of the element
    for node in elem.childNodes:
        # Skip empty nodes.
        if node.localName == None:
            continue

        # The name of the python object to recreate.
        name = str(node.localName)

        # Skip blacklisted objects.
        if name in blacklist:
            continue

        # The value - original file version.
        if file_version == 1:
            # IEEE-754 floats (for full precision restoration).
            ieee_array = node.getAttribute('ieee_754_byte_array')
            if ieee_array:
                value = packBytesAsPyFloat(eval(ieee_array))

            # Get the node contents.
            else:
                value = node_value_to_python(node.childNodes[0])

        # The value - second file version.
        elif file_version == 2:
            # Get the type.
            py_type = node.getAttribute('type')
            if not search('dtype', py_type):
                py_type = eval(py_type)

            # Loop over the info nodes of the Python object.
            ieee_value = None
            for sub_node in node.childNodes:
                # Get the value.
                if sub_node.localName == 'value':
                    value = node_value_to_python(sub_node.childNodes[0])

                # IEEE-754 floats (for full precision restoration).
                if sub_node.localName == 'ieee_754_byte_array':
                    ieee_value = node_value_to_python(sub_node.childNodes[0])

            # Use IEEE-754 floats when possible.
            if ieee_value:
                # Simple float.
                if py_type == float:
                    value = packBytesAsPyFloat(ieee_value)

                # Convert dictionaries.
                elif py_type == dict:
                    for key in ieee_value:
                        value[key] = packBytesAsPyFloat(ieee_value[key])

                # Convert lists.
                elif py_type == list:
                    # Loop over the first dimension.
                    for i in range(len(value)):
                        # List of lists.
                        if isinstance(value[i], list) or isinstance(value[i], ndarray):
                            for j in range(len(value[i])):
                                value[i][j] = packBytesAsPyFloat(ieee_value[i][j])

                        # None values.
                        elif ieee_value[i] == None:
                            value[i] = None

                        # Normal list.
                        else:
                            value[i] = packBytesAsPyFloat(ieee_value[i])

                # Numpy types.
                elif search('dtype', py_type):
                    # The specific type.
                    numpy_type = getattr(numpy, py_type[7:-2])

                    # Build a new array of the correct type.
                    value = zeros(value.shape, numpy_type)

                    # A matrix.
                    if isinstance(value[0], ndarray):
                        for i in range(len(value)):
                            for j in range(len(value[i])):
                                value[i, j] = packBytesAsPyFloat(ieee_value[i][j])

                    # A vector.
                    else:
                        for i in range(len(value)):
                            value[i] = packBytesAsPyFloat(ieee_value[i])

        # Set the value.
        setattr(base_object, name, value)
Beispiel #2
0
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

# Python module imports.
from unittest import TestCase
from copy import copy

# relax module imports.
from lib.float import CLASS_POS_DENORMAL, CLASS_POS_INF, CLASS_POS_NORMAL, CLASS_POS_ZERO, CLASS_NEG_DENORMAL, CLASS_NEG_INF, CLASS_NEG_NORMAL, CLASS_NEG_ZERO, CLASS_QUIET_NAN, getFloatClass, isPositive, isZero, nan, neg_inf, packBytesAsPyFloat, pos_inf

# Some constants for the tests.
FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 0])
NEG_FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 128])
FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 65])
NEG_FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 193])
ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 0])
NEG_ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 128])


def make_dict_by_id(elements):
    """Convert the list into a dictionary of pointer:value pairs."""

    # Convert.
    result = {}
    for element in elements:
        result[id(element)] = element
Beispiel #3
0
def xml_to_object(elem, base_object=None, set_fn=None, file_version=1, blacklist=[]):
    """Convert the XML elements into python objects, and place these into the base object.

    @param elem:            The element to extract all python objects from.
    @type elem:             xml.dom.minidom.Element instance
    @keyword base_object:   The python class instance to place the objects into.
    @type base_object:      instance
    @keyword set_fn:        A function used to replace setattr for placing the object into the base
                            object.
    @type set_fn:           function
    @keyword file_version:  The relax XML version of the XML file.
    @type file_version:     int
    @keyword blacklist:     A list of object names to exclude.
    @type blacklist:        list of str
    """

    # Loop over the nodes of the element
    for node in elem.childNodes:
        # Skip empty nodes.
        if node.localName == None:
            continue

        # The name of the python object to recreate.
        name = str(node.localName)

        # Skip blacklisted objects.
        if name in blacklist:
            continue

        # The value - original file version.
        if file_version == 1:
            # IEEE-754 floats (for full precision restoration).
            ieee_array = node.getAttribute('ieee_754_byte_array')
            if ieee_array:
                value = packBytesAsPyFloat(eval(ieee_array))

            # Get the node contents.
            else:
                value = node_value_to_python(node.childNodes[0])

        # The value - second file version.
        elif file_version == 2:
            # Get the type.
            py_type = node.getAttribute('type')
            if not search('dtype', py_type):
                py_type = eval(py_type)

            # Loop over the info nodes of the Python object.
            ieee_value = None
            for sub_node in node.childNodes:
                # Get the value.
                if sub_node.localName == 'value':
                    value = node_value_to_python(sub_node.childNodes[0])

                # IEEE-754 floats (for full precision restoration).
                if sub_node.localName == 'ieee_754_byte_array':
                    ieee_value = node_value_to_python(sub_node.childNodes[0])

            # Use IEEE-754 floats when possible.
            if ieee_value:
                # Simple float.
                if py_type == float:
                    value = packBytesAsPyFloat(ieee_value)

                # Convert dictionaries.
                elif py_type == dict:
                    for key in ieee_value:
                        value[key] = packBytesAsPyFloat(ieee_value[key])

                # Convert lists.
                elif py_type == list:
                    # Loop over the first dimension.
                    for i in range(len(value)):
                        # List of lists.
                        if isinstance(value[i], list) or isinstance(value[i], ndarray):
                            for j in range(len(value[i])):
                                value[i][j] = packBytesAsPyFloat(ieee_value[i][j])

                        # Normal list.
                        else:
                            value[i] = packBytesAsPyFloat(ieee_value[i])

                # Numpy types.
                elif search('dtype', py_type):
                    # The specific type.
                    numpy_type = getattr(numpy, py_type[7:-2])

                    # Build a new array of the correct type.
                    value = zeros(value.shape, numpy_type)

                    # A matrix.
                    if isinstance(value[0], ndarray):
                        for i in range(len(value)):
                            for j in range(len(value[i])):
                                value[i, j] = packBytesAsPyFloat(ieee_value[i][j])

                    # A vector.
                    else:
                        for i in range(len(value)):
                            value[i] = packBytesAsPyFloat(ieee_value[i])

        # Set the value.
        setattr(base_object, name, value)
Beispiel #4
0
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

# Python module imports.
from unittest import TestCase
from copy import copy

# relax module imports.
from lib.float import CLASS_POS_DENORMAL, CLASS_POS_INF, CLASS_POS_NORMAL, CLASS_POS_ZERO, CLASS_NEG_DENORMAL, CLASS_NEG_INF, CLASS_NEG_NORMAL, CLASS_NEG_ZERO, CLASS_QUIET_NAN, getFloatClass, isPositive, isZero, nan, neg_inf, packBytesAsPyFloat, pos_inf


# Some constants for the tests.
FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 0])
NEG_FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 128])
FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 65])
NEG_FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 193])
ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 0])
NEG_ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 128])


def make_dict_by_id(elements):
    """Convert the list into a dictionary of pointer:value pairs."""

    # Convert.
    result = {}
    for element in elements:
        result[id(element)] = element