1

理想情况下,我想要一个如下工作的函数(适用于各种 numpy 函数):

parameter_types('np.random.binomial')

并返回:

{'a: 'int', 'b':'float', 'size':'int'}

我了解 jedi 支持从文档字符串中提取此信息,但我无法使其工作。使用绝地可以得到这样的东西吗?

4

1 回答 1

1

正如在这个答案中发现的那样,最好的选择是安装numpydoc及其要求。

import numpydoc
import numpy as np

doc = numpydoc.docscrape.NumpyDocString(np.random.binomial.__doc__)

然后可以检查

In [45]: doc['Parameters']
Out[45]: 
[('n',
  'int or array_like of ints',
  ['Parameter of the distribution, >= 0. Floats are also accepted,',
   'but they will be truncated to integers.']),
 ('p',
  'float or array_like of floats',
  ['Parameter of the distribution, >= 0 and <=1.']),
 ('size',
  'int or tuple of ints, optional',
  ['Output shape.  If the given shape is, e.g., ``(m, n, k)``, then',
   '``m * n * k`` samples are drawn.  If size is ``None`` (default),',
   'a single value is returned if ``n`` and ``p`` are both scalars.',
   'Otherwise, ``np.broadcast(n, p).size`` samples are drawn.'])]

请注意,您必须进行一些后处理,例如将元组列表转换为字典。

In [46]: {t[0]: t[1] for t in doc['Parameters']}
Out[46]: 
{'n': 'int or array_like of ints',
 'p': 'float or array_like of floats',
 'size': 'int or tuple of ints, optional'}
于 2017-03-17T15:25:51.150 回答