[h5py] 195/455: Implement boolean type

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:32 UTC 2015


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

ghisvail-guest pushed a commit to annotated tag 1.3.0
in repository h5py.

commit 8f91a80ebd56be930fcba8d443dcd4a6309819dc
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Mon Jan 26 00:14:47 2009 +0000

    Implement boolean type
---
 h5py/h5.pxd            |  2 ++
 h5py/h5.pyx            | 29 ++++++++++++++++++++++++-----
 h5py/h5t.pyx           | 26 ++++++++++++++++++++++++++
 h5py/tests/test_h5t.py |  6 ++++++
 4 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/h5py/h5.pxd b/h5py/h5.pxd
index ae97d1e..89379d7 100644
--- a/h5py/h5.pxd
+++ b/h5py/h5.pxd
@@ -27,6 +27,8 @@ cdef class H5PYConfig:
 
     cdef object _r_name
     cdef object _i_name
+    cdef object _f_name
+    cdef object _t_name
     cdef readonly object API_16
     cdef readonly object API_18
     cdef readonly object DEBUG
diff --git a/h5py/h5.pyx b/h5py/h5.pyx
index cc3e472..ab86113 100644
--- a/h5py/h5.pyx
+++ b/h5py/h5.pyx
@@ -84,9 +84,13 @@ cdef class H5PYConfig:
         self.DEBUG = H5PY_DEBUG
         self._r_name = 'r'
         self._i_name = 'i'
+        self._f_name = 'FALSE'
+        self._t_name = 'TRUE'
 
     property complex_names:
         """ Settable 2-tuple controlling how complex numbers are saved.
+
+        Format is (real_name, imag_name), defaulting to ('r','i').
         """
 
         def __get__(self):
@@ -94,15 +98,30 @@ cdef class H5PYConfig:
 
         def __set__(self, val):
             try:
-                r = val[0]
-                i = val[1]
-                if not (isinstance(r, str) and isinstance(i, str)):
-                    raise TypeError
+                r = str(val[0])
+                i = str(val[1])
             except Exception:
-                raise TypeError("complex_names must be a 2-tuple (real, img)")
+                raise TypeError("complex_names must be a 2-tuple of strings (real, img)")
             self._r_name = r
             self._i_name = i
 
+    property bool_names:
+        """ Settable 2-tuple controlling HDF5 ENUM names for boolean types.
+
+        Format is (false_name, real_name), defaulting to ('FALSE', 'TRUE').
+        """
+        def __get__(self):
+            return (self._f_name, self._t_name)
+
+        def __set__(self, val):
+            try:
+                f = str(val[0])
+                t = str(val[1])
+            except Exception:
+                raise TypeError("bool_names must be a 2-tuple of strings (false, true)")
+            self._f_name = f
+            self._t_name = t
+
     def __repr__(self):
         rstr =  \
 """\
diff --git a/h5py/h5t.pyx b/h5py/h5t.pyx
index 18c100a..918c727 100644
--- a/h5py/h5t.pyx
+++ b/h5py/h5t.pyx
@@ -1158,6 +1158,16 @@ cdef class TypeEnumID(TypeCompositeID):
 
         cdef TypeID tmp_type
         tmp_type = self.get_super()
+
+        if self.get_nmembers() == 2:
+            members = {}
+            ref = {cfg._f_name: 0, cfg._t_name: 1}
+            for idx in range(2):
+                name = self.get_member_name(idx)
+                val = self.get_member_value(idx)
+                members[name] = val
+            if members == ref:
+                return dtype('bool')
         return tmp_type.py_dtype()
 
 
@@ -1227,6 +1237,18 @@ cdef TypeEnumID _c_enum(dtype dt, dict vals):
         out.enum_insert(name, vals[name])
     return out
 
+cdef TypeEnumID _c_bool(dtype dt):
+    # Booleans
+    global cfg
+
+    cdef TypeEnumID out
+    out = TypeEnumID(H5Tenum_create(H5T_NATIVE_INT8))
+
+    out.enum_insert(cfg._f_name, 0)
+    out.enum_insert(cfg._t_name, 1)
+
+    return out
+
 cdef TypeArrayID _c_array(dtype dt):
     # Arrays
     cdef dtype base
@@ -1361,6 +1383,10 @@ cpdef TypeID py_create(object dtype_in, dict enum_vals=None):
         elif kind == c'S':
             return _c_string(dt)
 
+        # Boolean
+        elif kind == c'b':
+            return _c_bool(dt)
+
         # Unrecognized
         else:
             raise TypeError("No conversion path for dtype: %s" % repr(dt))
diff --git a/h5py/tests/test_h5t.py b/h5py/tests/test_h5t.py
index de4cec7..dfd32dd 100644
--- a/h5py/tests/test_h5t.py
+++ b/h5py/tests/test_h5t.py
@@ -222,6 +222,12 @@ class TestH5T(HDF5TestCase):
         self.assert_(htype.get_size() < total_len)
         self.assertEqual(htype.get_nmembers(), len(names))
 
+    def test_bool(self):
+        out = h5t.py_create('bool')
+        self.assert_(isinstance(out, h5t.TypeEnumID))
+        self.assertEqual(out.get_nmembers(),2)
+        self.assertEqual(out.dtype, dtype('bool'))
+
     # === Tests for py_create =================================================
 
     def test_py_create_simple(self):

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



More information about the debian-science-commits mailing list