utils
classUtils
cached_class(klass)
Decorator to cache class instances by constructor arguments. This results in a class that behaves like a singleton for each set of constructor arguments, ensuring efficiency.
Note that this should be used for immutable classes only. Having a cached mutable class makes very little sense. For efficiency, avoid using this decorator for situations where there are many constructor arguments permutations.
The keywords argument dictionary is converted to a tuple because dicts are mutable; keywords themselves are strings and so are always hashable, but if any arguments (keyword or positional) are non- hashable, that set of arguments is not cached.
Source code in src/easyscience/utils/classUtils.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
singleton(cls)
This decorator can be used to create a singleton out of a class.
Usage::
@singleton
class MySingleton:
def __init__():
pass
Source code in src/easyscience/utils/classUtils.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
decorators
counted(func)
Counts how many times a function has been called and adds a
func.calls to it's properties :param func: Function to be counted
:return: Results from function call.
Source code in src/easyscience/utils/decorators.py
44 45 46 47 48 49 50 51 52 53 54 55 56 | |
deprecated(func)
This is a decorator which can be used to mark functions as deprecated.
It will result in a warning being emitted when the function is used.
Source code in src/easyscience/utils/decorators.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
memoized
Decorator.
Caches a function's return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated).
Source code in src/easyscience/utils/decorators.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
__get__(obj, objtype)
Support instance methods.
Source code in src/easyscience/utils/decorators.py
39 40 41 | |
__repr__()
Return the function's docstring.
Source code in src/easyscience/utils/decorators.py
35 36 37 | |
time_it(func)
Times a function and reports the time either to the class' log or the base logger :param func: function to be timed :return: callable function with timer.
Source code in src/easyscience/utils/decorators.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
string
transformation_to_string(matrix, translation_vec=(0, 0, 0), components=('x', 'y', 'z'), c='', delim=',')
Convenience method.
Given matrix returns string, e.g. x+2y+1/4 :param matrix : param translation_vec :param components: either ('x', 'y', 'z') or ('a', 'b', 'c') :param c: optional additional character to print (used for magmoms) :param delim: delimiter :return: xyz string
Source code in src/easyscience/utils/string.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |