[h5py] 25/38: Fix lint issues in _hl/base, refactor

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


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

ghisvail-guest pushed a commit to branch master
in repository h5py.

commit 6fc3a71decad3c4f659e40c416fae3ff4d71592f
Author: Andrew Collette <andrew.collette at gmail.com>
Date:   Sat May 30 18:10:31 2015 -0600

    Fix lint issues in _hl/base, refactor
---
 h5py/_hl/attrs.py |  2 +-
 h5py/_hl/base.py  | 84 +++++++++++++++++++++++++++++++++----------------------
 h5py/_hl/dims.py  |  2 +-
 h5py/_hl/group.py |  4 +--
 4 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/h5py/_hl/attrs.py b/h5py/_hl/attrs.py
index d0b604b..f68e4de 100644
--- a/h5py/_hl/attrs.py
+++ b/h5py/_hl/attrs.py
@@ -25,7 +25,7 @@ from .dataset import readtime_dtype
 from .datatype import Datatype
 
 
-class AttributeManager(base.MutableMappingWithLock, base.CommonStateObject):
+class AttributeManager(base.MutableMappingHDF5, base.CommonStateObject):
 
     """
         Allows dictionary-style access to an HDF5 object's attributes.
diff --git a/h5py/_hl/base.py b/h5py/_hl/base.py
index 71e4e56..7a606ba 100644
--- a/h5py/_hl/base.py
+++ b/h5py/_hl/base.py
@@ -7,17 +7,18 @@
 # License:  Standard 3-clause BSD; see "license.txt" for full license terms
 #           and contributor agreement.
 
+"""
+    Implements operations common to all high-level objects (File, etc.).
+"""
+
 from __future__ import absolute_import
 
 import posixpath
-import warnings
 import os
 import sys
-from collections import (
-    Mapping, MutableMapping, MappingView, KeysView, ValuesView, ItemsView
-)
-
 import six
+from collections import (Mapping, MutableMapping, KeysView, 
+                         ValuesView, ItemsView)
 
 from .. import h5d, h5i, h5r, h5p, h5f, h5t
 
@@ -112,6 +113,7 @@ class CommonStateObject(object):
         If name is None, returns either None or (None, None) appropriately.
         """
         def get_lcpl(coding):
+            """ Create an appropriate link creation property list """
             lcpl = self._lcpl.copy()
             lcpl.set_char_encoding(coding)
             return lcpl
@@ -179,7 +181,7 @@ class _RegionProxy(object):
             raise TypeError("Region references can only be made to datasets")
         from . import selections
         selection = selections.select(self.id.shape, args, dsid=self.id)
-        return h5r.create(self.id, b'.', h5r.DATASET_REGION, selection._id)
+        return h5r.create(self.id, b'.', h5r.DATASET_REGION, selection.id)
 
     def shape(self, ref):
         """ Get the shape of the target dataspace referred to by *ref*. """
@@ -284,24 +286,26 @@ class HLObject(CommonStateObject):
             return bool(self.id)
     __nonzero__ = __bool__
 
-class MappingViewWithLock(MappingView):
-
-    def __len__(self):
-        with phil:
-            return super(MappingViewWithLock, self).__len__()
 
+# --- Dictionary-style interface ----------------------------------------------
 
-class KeysViewWithLock(MappingViewWithLock, KeysView):
-    def __contains__(self, item):
-        with phil:
-            return super(KeysViewWithLock, self).__contains__(item)
+# To implement the dictionary-style interface from groups and attributes,
+# we inherit from the appropriate abstract base classes in collections.
+#
+# All locking is taken care of by the subclasses.
+# We have to override ValuesView and ItemsView here because Group and
+# AttributeManager can only test for key names.
 
-    def __iter__(self):
-        with phil:
-            return super(KeysViewWithLock, self).__iter__()
 
+class ValuesViewHDF5(ValuesView):
 
-class ValuesViewWithLock(MappingViewWithLock, ValuesView):
+    """
+        Wraps e.g. a Group or AttributeManager to provide a value view.
+        
+        Note that __contains__ will have poor performance as it has
+        to scan all the links or attributes.
+    """
+    
     def __contains__(self, value):
         with phil:
             for key in self._mapping:
@@ -315,7 +319,12 @@ class ValuesViewWithLock(MappingViewWithLock, ValuesView):
                 yield self._mapping.get(key)
 
 
-class ItemsViewWithLock(MappingViewWithLock, ItemsView):
+class ItemsViewHDF5(ItemsView):
+
+    """
+        Wraps e.g. a Group or AttributeManager to provide an items view.
+    """
+        
     def __contains__(self, item):
         with phil:
             key, val = item
@@ -329,30 +338,28 @@ class ItemsViewWithLock(MappingViewWithLock, ItemsView):
                 yield (key, self._mapping.get(key))
 
 
-class MappingWithLock(Mapping):
+class MappingHDF5(Mapping):
+
     """
-    Subclass of collections.Mapping with locks.
+        Wraps a Group, AttributeManager or DimensionManager object to provide
+        an immutable mapping interface.
+        
+        We don't inherit directly from MutableMapping because certain
+        subclasses, for example DimensionManager, are read-only.
     """
-    def get(self, name, default=None):
-        """ Retrieve the member, or return default if it doesn't exist """
-        with phil:
-            try:
-                return self[name]
-            except KeyError:
-                return default
-
+    
     if six.PY3:
         def keys(self):
             """ Get a view object on member names """
-            return KeysViewWithLock(self)
+            return KeysView(self)
 
         def values(self):
             """ Get a view object on member objects """
-            return ValuesViewWithLock(self)
+            return ValuesViewHDF5(self)
 
         def items(self):
             """ Get a view object on member items """
-            return ItemsViewWithLock(self)
+            return ItemsViewHDF5(self)
 
     else:
         def keys(self):
@@ -379,6 +386,15 @@ class MappingWithLock(Mapping):
             """ Get an iterator over (name, object) pairs """
             for x in self:
                 yield (x, self.get(x))
+                
+
+class MutableMappingHDF5(MappingHDF5, MutableMapping):
+
+    """
+        Wraps a Group or AttributeManager object to provide a mutable
+        mapping interface, in contrast to the read-only mapping of
+        MappingHDF5.
+    """
 
-class MutableMappingWithLock(MappingWithLock,MutableMapping):
     pass
+    
\ No newline at end of file
diff --git a/h5py/_hl/dims.py b/h5py/_hl/dims.py
index d11abc1..2afde6b 100644
--- a/h5py/_hl/dims.py
+++ b/h5py/_hl/dims.py
@@ -114,7 +114,7 @@ class DimensionProxy(base.CommonStateObject):
                % (self.label, self._dimension, id(self._id)))
 
 
-class DimensionManager(base.MappingWithLock, base.CommonStateObject):
+class DimensionManager(base.MappingHDF5, base.CommonStateObject):
 
     """
     """
diff --git a/h5py/_hl/group.py b/h5py/_hl/group.py
index 4d469e2..bc13c18 100644
--- a/h5py/_hl/group.py
+++ b/h5py/_hl/group.py
@@ -18,12 +18,12 @@ import collections
 
 from .. import h5g, h5i, h5o, h5r, h5t, h5l, h5p
 from . import base
-from .base import HLObject, MutableMappingWithLock, phil, with_phil
+from .base import HLObject, MutableMappingHDF5, phil, with_phil
 from . import dataset
 from . import datatype
 
 
-class Group(HLObject, MutableMappingWithLock):
+class Group(HLObject, MutableMappingHDF5):
 
     """ Represents an HDF5 group.
     """

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