[python-hdf5storage] 56/152: Added read/write support for tuple, set, frozenset, and collections.deque.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:34 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 9d8a8e9dd6ce8f4b3f6d857d7a8f35366cb2066a
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Tue Jan 28 02:59:55 2014 -0500

    Added read/write support for tuple, set, frozenset, and collections.deque.
---
 README.rst                             |  8 ++++++-
 doc/source/hdf5storage.Marshallers.rst | 26 +++++++++++++++++++++++
 hdf5storage/Marshallers.py             | 39 ++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/README.rst b/README.rst
index 79d1bcd..05792f4 100644
--- a/README.rst
+++ b/README.rst
@@ -49,8 +49,10 @@ read and write support for a limited set of Python and MATLAB types.
 Supported Types
 ===============
 
-The supported Python and MATLAB types are given in the tables below.  The tables assume that one has imported numpy as::
+The supported Python and MATLAB types are given in the tables below.
+The tables assume that one has imported collections and numpy as::
 
+    import collections as cl
     import numpy as np
 
 The table gives which Python types can be read and written, the first
@@ -85,6 +87,10 @@ np.bytes\_     0.1                          char     0.1
 np.object\_    0.1                          cell     0.1
 dict           0.1                          struct   0.1 [3]_
 list           0.1      np.object\_         cell     0.1
+tuple          0.1      np.object\_         cell     0.1
+set            0.1      np.object\_         cell     0.1
+frozenset      0.1      np.object\_         cell     0.1
+cl.deque       0.1      np.object\_         cell     0.1
 =============  =======  ==================  =======  ========
 
 .. [1] Converted to ASCII, so characters outside of that set are lost.
diff --git a/doc/source/hdf5storage.Marshallers.rst b/doc/source/hdf5storage.Marshallers.rst
index b30f3c0..25d6ce2 100644
--- a/doc/source/hdf5storage.Marshallers.rst
+++ b/doc/source/hdf5storage.Marshallers.rst
@@ -195,3 +195,29 @@ PythonListMarshaller
    .. autoinstanceattribute:: PythonListMarshaller.matlab_classes
       :annotation: = []
 
+
+PythonTupleSetDequeMarshaller
+----------------------
+
+.. autoclass:: PythonTupleSetDequeMarshaller
+   :members: read, write
+   :show-inheritance:
+
+   .. autoinstanceattribute:: PythonTupleSetDequeMarshaller.cpython_attributes
+      :annotation: = {'CPython.Type', 'CPython.Shape', 'CPython.Empty',
+		      'CPython.numpy.UnderlyingType',
+                      'CPython.numpy.Container'}
+
+   .. autoinstanceattribute:: PythonTupleSetDequeMarshaller.matlab_attributes
+      :annotation: = {'H5PATH', 'MATLAB_class', 'MATLAB_empty',
+		      'MATLAB_int_decode'}
+
+   .. autoinstanceattribute:: PythonTupleSetDequeMarshaller.types
+      :annotation: = [tuple, set, frozenset, collections.deque]
+
+   .. autoinstanceattribute:: PythonTupleSetDequeMarshaller.cpython_type_strings
+      :annotation: = ['tuple', 'set', 'frozenset', 'collections.deque']
+
+   .. autoinstanceattribute:: PythonTupleSetDequeMarshaller.matlab_classes
+      :annotation: = []
+
diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 04ea6ee..c94fb1e 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -29,6 +29,7 @@
 """
 
 import posixpath
+import collections
 
 import numpy as np
 import h5py
@@ -991,3 +992,41 @@ class PythonListMarshaller(NumpyScalarArrayMarshaller):
         # Passing it through list does all the work of making it a list
         # again.
         return list(data)
+
+
+class PythonTupleSetDequeMarshaller(PythonListMarshaller):
+    def __init__(self):
+        PythonListMarshaller.__init__(self)
+        self.types = [tuple, set, frozenset, collections.deque]
+        self.cpython_type_strings = ['tuple', 'set', 'frozenset',
+                                     'collections.deque']
+        # 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 list and then pass it to
+        # the parent version of this function. The proper type_string
+        # needs to be grabbed now as the parent function will have a
+        # modified form of data to guess from if not given the right one
+        # explicitly.
+        PythonListMarshaller.write(self, f, grp, name, list(data),
+                                  self.get_type_string(data,
+                                  type_string), options)
+
+    def read(self, f, grp, name, options):
+        # Use the parent class version to read it and do most of the
+        # work.
+        data = PythonListMarshaller.read(self, f, grp, name,
+                                        options)
+
+        # The type string determines how to convert it back to a Python
+        # type (just look up the entry in types).
+        type_string = get_attribute_string(grp[name], 'CPython.Type')
+        if type_string in self.cpython_type_strings:
+            tp = self.types[self.cpython_type_strings.index(
+                            type_string)]
+            return tp(data)
+        else:
+            # Must be some other type, so return it as is.
+            return data

-- 
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