[python-hdf5storage] 15/152: API changes in preparation for read support, especially in regard to MATLAB types.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:30 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 0.1
in repository python-hdf5storage.

commit d3a5afad577a26aeb7041517fdf057252c413f00
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Jan 12 02:25:06 2014 -0500

    API changes in preparation for read support, especially in regard to MATLAB types.
---
 hdf5storage/Marshallers.py | 69 ++++++++++++++--------------------------------
 hdf5storage/core.py        | 58 +++++++++++++++++++++++++++++++-------
 2 files changed, 69 insertions(+), 58 deletions(-)

diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 68298e4..98d9191 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -59,10 +59,8 @@ class TypeMarshaller(object):
         Types the marshaller can work on.
     cpython_type_strings : list of str
         Type strings of readable types.
-    makes_datasets : bool
-        At least one type marshalled as Dataset.
-    makes_groups : bool
-        At least one type marshalled as Group.
+    matlab_classes : list of str
+        Readable MATLAB classes.
 
     See Also
     --------
@@ -107,23 +105,13 @@ class TypeMarshaller(object):
         #: able to read it back correctly. Default value is ``[]``.
         self.cpython_type_strings = []
 
-        #: At least one type marshalled as Dataset.
+        #: MATLAB class strings of readable types.
         #:
-        #: bool
-        #:
-        #: Whether or not at least one of the types that this marshaller
-        #: works with get put into HDF5 Datasets. Default value is
-        #: ``False``.
-        self.makes_datasets = False
-
-        #: At least one type marshalled as Group.
-        #:
-        #: bool
+        #: list of str
         #:
-        #: Whether or not at least one of the types that this marshaller
-        #: works with get put into HDF5 Groups. Default value is
-        #: ``False``.
-        self.makes_groups = False
+        #: ``list`` of the MATLAB class ``str`` that the marshaller can
+        #: read into Python objects. Default value is ``[]``.
+        self.matlab_classes = []
 
     def get_type_string(self, data, type_string):
         """ Gets type string.
@@ -257,34 +245,6 @@ class TypeMarshaller(object):
         for attribute in (set(grp[name].attrs.keys()) - attributes_used):
             del_attribute(grp[name], attribute)
 
-    def can_read(self, f, grp, name, options):
-        """ Whether the marshaller can read the object from file.
-
-        Parameters
-        ----------
-        f : h5py.File
-            The HDF5 file handle that is open.
-        grp : h5py.Group or h5py.File
-            The parent HDF5 Group (or File if at '/') that contains the
-            object with the specified name.
-        name : str
-            Name of the object.
-        options : hdf5storage.core.Options
-            hdf5storage options object.
-
-        Returns
-        -------
-        bool
-            Whether this marshaller can read the object from file or
-            not.
-
-        Notes
-        -----
-        Unless replaced in a subclass, it is always ``False``.
-
-        """
-        return False
-
     def read(self, f, grp, name, options):
         """ Read a Python object from file.
 
@@ -366,6 +326,9 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                                  np.string_: 'char',
                                  np.unicode: 'char'}
 
+        # Set matlab_classes to the supported classes (the values).
+        self.matlab_classes = list(self.__MATLAB_classes.values())
+
 
     def write(self, f, grp, name, data, type_string, options):
         # Need to make a set of data that will be stored. It will start
@@ -504,6 +467,9 @@ class PythonScalarMarshaller(NumpyScalarArrayMarshaller):
         NumpyScalarArrayMarshaller.__init__(self)
         self.types = [bool, int, float, complex]
         self.cpython_type_strings = ['bool', 'int', 'float', 'complex']
+        # As the parent class already has MATLAB strings handled, there
+        # are no MATLAB classes that this marshaller should be used for.
+        self.matlab_classes = []
 
     def write(self, f, grp, name, data, type_string, options):
         # data just needs to be converted to the appropriate numpy type
@@ -524,6 +490,9 @@ class PythonStringMarshaller(NumpyScalarArrayMarshaller):
         NumpyScalarArrayMarshaller.__init__(self)
         self.types = [str, bytes, bytearray]
         self.cpython_type_strings = ['str', 'bytes', 'bytearray']
+        # As the parent class already has MATLAB strings handled, there
+        # are no MATLAB classes that this marshaller should be used for.
+        self.matlab_classes = []
 
     def write(self, f, grp, name, data, type_string, options):
         # data just needs to be converted to a numpy string, unless it
@@ -549,6 +518,8 @@ class PythonNoneMarshaller(NumpyScalarArrayMarshaller):
         NumpyScalarArrayMarshaller.__init__(self)
         self.types = [type(None)]
         self.cpython_type_strings = ['builtins.NoneType']
+        # None corresponds to no MATLAB class.
+        self.matlab_classes = []
     def write(self, f, grp, name, data, type_string, options):
         # Just going to use the parent function with an empty double
         # (two dimensional so that MATLAB will import it as a []) as the
@@ -567,7 +538,9 @@ class PythonDictMarshaller(TypeMarshaller):
         self.matlab_attributes |= {'MATLAB_class', 'MATLAB_empty'}
         self.types = [dict]
         self.cpython_type_strings = ['dict']
-        self.__MATLAB_classes = ['struct']
+        self.__MATLAB_classes = {dict: 'struct'}
+        # Set matlab_classes to the supported classes (the values).
+        self.matlab_classes = list(self.__MATLAB_classes.values())
 
     def write(self, f, grp, name, data, type_string, options):
         # If the group doesn't exist, it needs to be created. If it
diff --git a/hdf5storage/core.py b/hdf5storage/core.py
index 8149928..5479f0e 100644
--- a/hdf5storage/core.py
+++ b/hdf5storage/core.py
@@ -416,10 +416,12 @@ class MarshallerCollection(object):
 
         # A list of all the marshallers will be needed along with
         # dictionaries to lookup up the marshaller to use for given
-        # types or type strings (they are the keys).
+        # types, type string, or MATLAB class string (they are the
+        # keys).
         self._marshallers = []
-        self._out = dict()
-        self._in = dict()
+        self._types = dict()
+        self._type_strings = dict()
+        self._matlab_classes = dict()
 
         # Add any user given marshallers.
         self.add_marshaller(copy.deepcopy(marshallers))
@@ -440,14 +442,23 @@ class MarshallerCollection(object):
         # Construct the dictionary to look up the appropriate marshaller
         # by type.
 
-        self._out = {tp: m for m in self._marshallers for tp in m.types}
+        self._types = {tp: m for m in self._marshallers for tp in m.types}
 
         # The equivalent one to read data types given type strings needs
         # to be created from it. Basically, we have to make the key be
         # the cpython_type_string from it.
 
-        self._in = {type_string: m for key, m in self._out.items()
-                    for type_string in m.cpython_type_strings}
+        self._type_strings = {type_string: m for key, m in
+                              self._types.items() for type_string in
+                              m.cpython_type_strings}
+
+        # The equivalent one to read data types given MATLAB class
+        # strings needs to be created from it. Basically, we have to
+        # make the key be the matlab_class from it.
+
+        self._matlab_classes = {matlab_class: m for key, m in
+                                self._types.items() for matlab_class in
+                                m.matlab_classes}
 
     def add_marshaller(self, marshallers):
         """ Add a marshaller/s to the user provided list.
@@ -524,8 +535,8 @@ class MarshallerCollection(object):
         hdf5storage.Marshallers.TypeMarshaller.types
 
         """
-        if tp in self._out:
-            return copy.deepcopy(self._out[tp])
+        if tp in self._types:
+            return copy.deepcopy(self._types[tp])
         else:
             return None
 
@@ -551,8 +562,35 @@ class MarshallerCollection(object):
         hdf5storage.Marshallers.TypeMarshaller.cpython_type_strings
 
         """
-        if type_string in self._in:
-            return copy.deepcopy(self._in[type_string])
+        if type_string in self._type_strings:
+            return copy.deepcopy(self._type_strings[type_string])
+        else:
+            return None
+
+    def get_marshaller_for_matlab_class(self, matlab_class):
+        """ Gets the appropriate marshaller for a MATLAB class string.
+
+        Retrieves the marshaller, if any, that can be used to read/write
+        a Python object associated with the given MATLAB class string.
+
+        Parameters
+        ----------
+        matlab_class : str
+            MATLAB class string for a Python object.
+
+        Returns
+        -------
+        marshaller
+            The marshaller that can read/write the type to
+            file. ``None`` if no appropriate marshaller is found.
+
+        See Also
+        --------
+        hdf5storage.Marshallers.TypeMarshaller.cpython_type_strings
+
+        """
+        if type_string in self._matlab_classes:
+            return copy.deepcopy(self._matlab_classes[matlab_class])
         else:
             return None
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-hdf5storage.git



More information about the debian-science-commits mailing list