Skip to content

Commit

Permalink
rewrite numpy docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis committed Nov 18, 2019
1 parent 0112190 commit af2c4ab
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion xarray/ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Once NumPy 1.10 comes out with support for overriding ufuncs, this module will
hopefully no longer be necessary.
"""
import textwrap
import warnings as _warnings

import numpy as _np
Expand Down Expand Up @@ -78,10 +79,36 @@ def __call__(self, *args, **kwargs):
return res


def _skip_signature(doc):
if doc.startswith(name):
signature_end = doc.find("\n\n")
doc = doc[signature_end + 2 :]

return doc


def _remove_unused_reference_labels(doc):
max_references = 5
for num in range(max_references):
label = f".. [{num}]"
reference = f"[{num}]_"
index = f"[{num}]. "

if label not in doc or reference in doc:
continue

doc = doc.replace(label, index)

return doc


def _create_op(name):
func = _UFuncDispatcher(name)
func.__name__ = name
doc = getattr(_np, name).__doc__
doc = _remove_unused_reference_labels(
_skip_signature(textwrap.dedent(getattr(_np, name).__doc__))
)

func.__doc__ = (
"xarray specific variant of numpy.%s. Handles "
"xarray.Dataset, xarray.DataArray, xarray.Variable, "
Expand Down

0 comments on commit af2c4ab

Please sign in to comment.