2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222
23+ """Provides for caching evaluated results for faster re-evaluation."""
24+
2325from typing import NamedTuple
2426
2527
2628def class_handling_cache (cls ):
2729 """Class decorator used to handle cache.
30+
2831 To use it, add a ''_to_cache'' static attribute in the given class.
2932 This private dictionary should map class getters to their list of setters.
3033 At initialization, this decorator add a ''_cache'' property to the class.
@@ -53,11 +56,14 @@ def get_handler(mesh):
5356
5457
5558class MethodIdentifier (NamedTuple ):
59+ """Provides for identifying a method."""
60+
5661 method_name : str
5762 args : list
5863 kwargs : dict
5964
6065 def __eq__ (self , other ):
66+ """Compare two methods for equality."""
6167 if isinstance (other , str ):
6268 return self .method_name == other
6369 else :
@@ -68,6 +74,7 @@ def __eq__(self, other):
6874 )
6975
7076 def __hash__ (self ):
77+ """Fetch the hash corresponding to a method."""
7178 hash = self .method_name .__hash__ ()
7279 if self .args :
7380 hash += self .args .__hash__ ()
@@ -77,7 +84,8 @@ def __hash__(self):
7784
7885
7986class CacheHandler :
80- """ "Handle cache complexity.
87+ """Handle cache complexity.
88+
8189 Is initialized by a class and a dictionary mapping the getters
8290 which support caching to their setters.
8391 When the getters of the dictionary are called, their parameters
@@ -112,6 +120,7 @@ def __init__(self, cls, getters_to_setters_dict):
112120 self .cached = {}
113121
114122 def handle (self , object , func , * args , ** kwargs ):
123+ """Recover data which has already been cached."""
115124 identifier = MethodIdentifier (func .__name__ , args , kwargs )
116125 if identifier in self .cached :
117126 return self .cached [identifier ]
@@ -128,19 +137,19 @@ def handle(self, object, func, *args, **kwargs):
128137 return func (object , * args , ** kwargs )
129138
130139 def clear (self ):
140+ """Clear cached data."""
131141 self .cached = {}
132142
133143
134144def _handle_cache (func ):
135- """Calls the cache handler to either recover cached data, either cache the data
136- or clear some cached data if the method is a setter.
145+ """Call the cache handler to either recover cached data, either cache the data or clear some cached data if the method is a setter.
137146
138147 .. note::
139148 The method must be used as a decorator.
140149 """
141150
142151 def wrapper (self , * args , ** kwargs ):
143- """Call the original function"""
152+ """Call the original function. """
144153 if hasattr (self , "_cache" ):
145154 return self ._cache .handle (self , func , * args , ** kwargs )
146155 else :
@@ -150,15 +159,14 @@ def wrapper(self, *args, **kwargs):
150159
151160
152161def _setter (func ):
153- """Add a private attribute to the class (``self._is_set = True``)
154- when a method with this decorator is used.
162+ """Add a private attribute to the class (``self._is_set = True``) when a method with this decorator is used.
155163
156164 .. note::
157165 The method must be used as a decorator.
158166 """
159167
160168 def wrapper (self , * args , ** kwargs ):
161- """Call the original function"""
169+ """Call the original function. """
162170 if hasattr (self , "_is_set" ):
163171 self ._is_set = True
164172 else :
0 commit comments