[python-hdf5storage] 27/152: Added Python string read support and changed it so bytearrays are written as numpy.string_ instead of numpy.uint8.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:31 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 553f04441ef8d21fc713edaacf1761000be85746
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sat Jan 25 16:55:19 2014 -0500

    Added Python string read support and changed it so bytearrays are written as numpy.string_ instead of numpy.uint8.
---
 hdf5storage/Marshallers.py | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index d9719a7..b2dbece 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -632,14 +632,8 @@ class PythonStringMarshaller(NumpyScalarArrayMarshaller):
         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
-        # is a bytearray in which case it needs to be converted to a
-        # uint8 array.
-
-        if isinstance(data, bytearray):
-            cdata = np.uint8(data)
-        else:
-            cdata = np.string_(data)
+        # data just needs to be converted to a numpy string.
+        cdata = np.string_(data)
 
         # Now pass it to the parent version of this function to write
         # it. The proper type_string needs to be grabbed now as the
@@ -649,6 +643,25 @@ class PythonStringMarshaller(NumpyScalarArrayMarshaller):
                                          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 = NumpyScalarArrayMarshaller.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). Otherwise, return it
+        # as is.
+        type_string = get_attribute_string(grp[name], 'CPython.Type')
+        if type_string == 'str':
+            return data.decode()
+        elif type_string == 'bytes':
+            return data.tostring()
+        elif type_string == 'bytearray':
+            return bytearray(data.tostring())
+        else:
+            return data
+
 
 class PythonNoneMarshaller(NumpyScalarArrayMarshaller):
     def __init__(self):

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