From 2244cd929354fb4157eaa78204ad6bb3bebea9bf Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sun, 1 Jul 2018 15:13:21 -0700 Subject: MAINT: Move add_newdocs into core, since it only adds docs to those pieces --- numpy/core/function_base.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'numpy/core/function_base.py') diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 82de1a36e..fb72bada5 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -6,6 +6,7 @@ import operator from . import numeric as _nx from .numeric import (result_type, NaN, shares_memory, MAY_SHARE_BOUNDS, TooHardError,asanyarray) +from numpy.core.multiarray import add_docstring __all__ = ['logspace', 'linspace', 'geomspace'] @@ -356,3 +357,38 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None): endpoint=endpoint, base=10.0, dtype=dtype) return result.astype(dtype) + + +#always succeed +def add_newdoc(place, obj, doc): + """ + Adds documentation to obj which is in module place. + + If doc is a string add it to obj as a docstring + + If doc is a tuple, then the first element is interpreted as + an attribute of obj and the second as the docstring + (method, docstring) + + If doc is a list, then each element of the list should be a + sequence of length two --> [(method1, docstring1), + (method2, docstring2), ...] + + This routine never raises an error. + + This routine cannot modify read-only docstrings, as appear + in new-style classes or built-in functions. Because this + routine never raises an error the caller must check manually + that the docstrings were changed. + """ + try: + new = getattr(__import__(place, globals(), {}, [obj]), obj) + if isinstance(doc, str): + add_docstring(new, doc.strip()) + elif isinstance(doc, tuple): + add_docstring(getattr(new, doc[0]), doc[1].strip()) + elif isinstance(doc, list): + for val in doc: + add_docstring(getattr(new, val[0]), val[1].strip()) + except Exception: + pass -- cgit v1.2.1