[docs]defthreaded(func):"""Apply ``func`` to sub--elements of an object, including :class:`Add`. This decorator is intended to make it uniformly possible to apply a function to all elements of composite objects, e.g. matrices, lists, tuples and other iterable containers, or just expressions. This version of :func:`threaded` decorator allows threading over elements of :class:`Add` class. If this behavior is not desirable use :func:`xthreaded` decorator. Functions using this decorator must have the following signature:: @threaded def function(expr, *args, **kwargs): """returnthreaded_factory(func,True)

[docs]defxthreaded(func):"""Apply ``func`` to sub--elements of an object, excluding :class:`Add`. This decorator is intended to make it uniformly possible to apply a function to all elements of composite objects, e.g. matrices, lists, tuples and other iterable containers, or just expressions. This version of :func:`threaded` decorator disallows threading over elements of :class:`Add` class. If this behavior is not desirable use :func:`threaded` decorator. Functions using this decorator must have the following signature:: @xthreaded def function(expr, *args, **kwargs): """returnthreaded_factory(func,False)

[docs]defconserve_mpmath_dps(func):"""After the function finishes, resets the value of mpmath.mp.dps to the value it had before the function was run."""importfunctoolsimportmpmathdeffunc_wrapper():dps=mpmath.mp.dpstry:func()finally:mpmath.mp.dps=dpsfunc_wrapper=functools.update_wrapper(func_wrapper,func)returnfunc_wrapper

[docs]defdoctest_depends_on(exe=None,modules=None,disable_viewers=None):"""Adds metadata about the depenencies which need to be met for doctesting the docstrings of the decorated objects."""pyglet=FalseifmodulesisnotNoneand'pyglet'inmodules:pyglet=Truedefdepends_on_deco(fn):fn._doctest_depends_on=dict(exe=exe,modules=modules,disable_viewers=disable_viewers,pyglet=pyglet)# once we drop py2.5 support and use class decorators this evaluates# to Trueifinspect.isclass(fn):fn._doctest_depdends_on=no_attrs_in_subclass(fn,fn._doctest_depends_on)returnfnreturndepends_on_deco

[docs]defpublic(obj):""" Append ``obj``'s name to global ``__all__`` variable (call site). By using this decorator on functions or classes you achieve the same goal as by filling ``__all__`` variables manually, you just don't have to repeat yourself (object's name). You also know if object is public at definition site, not at some random location (where ``__all__`` was set). Note that in multiple decorator setup (in almost all cases) ``@public`` decorator must be applied before any other decorators, because it relies on the pointer to object's global namespace. If you apply other decorators first, ``@public`` may end up modifying the wrong namespace. Examples ======== >>> from sympy.utilities.decorator import public >>> __all__ Traceback (most recent call last): ... NameError: name '__all__' is not defined >>> @public ... def some_function(): ... pass >>> __all__ ['some_function'] """ifisinstance(obj,types.FunctionType):ns=get_function_globals(obj)name=get_function_name(obj)elifisinstance(obj,(type(type),class_types)):ns=sys.modules[obj.__module__].__dict__name=obj.__name__else:raiseTypeError("expected a function or a class, got %s"%obj)if"__all__"notinns:ns["__all__"]=[name]else:ns["__all__"].append(name)returnobj

[docs]defmemoize_property(storage):"""Create a property, where the lookup is stored in ``storage``"""defdecorator(method):name=method.__name__defwrapper(self):ifnamenotinstorage:storage[name]=method(self)returnstorage[name]returnproperty(update_wrapper(wrapper,method))returndecorator