One of my classes has a logical numpy array as parameter in many methods repeated (idx_vector=None).
How can I use a decorator to:
- automatically specify idx_vector
- automatically insert the description into the docstring
Example without decorator:
import numpy as np
class myarray(object):
def __init__(self, data):
self.data = np.array(data)
def get_sum(self, idx_vector=None):
"""
Input parameter:
``idx_vector``: logical indexing
...
... (further description)
"""
if idx_vector == None:
idx_vector = np.ones(len(self.data))
return sum(self.data*idx_vector)
def get_max(self, idx_vector=None):
"""
Input parameter:
``idx_vector``: logical indexing
...
... (further description)
"""
if idx_vector == None:
idx_vector = np.ones(len(self.data))
return max(self.data*idx_vector)
Usage:
a = [1,2,3,4,5.0]
b = np.array([True, True, False, False, False])
ma = myarray(a)
print(ma.get_max())
# ----> 5.0
print(ma.get_max(b))
# ----> 2.0