图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1071 lines
32 KiB

  1. import os
  2. import sys
  3. import textwrap
  4. import types
  5. import re
  6. import warnings
  7. from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype
  8. from numpy.core.overrides import set_module
  9. from numpy.core import ndarray, ufunc, asarray
  10. import numpy as np
  11. __all__ = [
  12. 'issubclass_', 'issubsctype', 'issubdtype', 'deprecate',
  13. 'deprecate_with_doc', 'get_include', 'info', 'source', 'who',
  14. 'lookfor', 'byte_bounds', 'safe_eval'
  15. ]
  16. def get_include():
  17. """
  18. Return the directory that contains the NumPy \\*.h header files.
  19. Extension modules that need to compile against NumPy should use this
  20. function to locate the appropriate include directory.
  21. Notes
  22. -----
  23. When using ``distutils``, for example in ``setup.py``.
  24. ::
  25. import numpy as np
  26. ...
  27. Extension('extension_name', ...
  28. include_dirs=[np.get_include()])
  29. ...
  30. """
  31. import numpy
  32. if numpy.show_config is None:
  33. # running from numpy source directory
  34. d = os.path.join(os.path.dirname(numpy.__file__), 'core', 'include')
  35. else:
  36. # using installed numpy core headers
  37. import numpy.core as core
  38. d = os.path.join(os.path.dirname(core.__file__), 'include')
  39. return d
  40. def _set_function_name(func, name):
  41. func.__name__ = name
  42. return func
  43. class _Deprecate:
  44. """
  45. Decorator class to deprecate old functions.
  46. Refer to `deprecate` for details.
  47. See Also
  48. --------
  49. deprecate
  50. """
  51. def __init__(self, old_name=None, new_name=None, message=None):
  52. self.old_name = old_name
  53. self.new_name = new_name
  54. self.message = message
  55. def __call__(self, func, *args, **kwargs):
  56. """
  57. Decorator call. Refer to ``decorate``.
  58. """
  59. old_name = self.old_name
  60. new_name = self.new_name
  61. message = self.message
  62. if old_name is None:
  63. try:
  64. old_name = func.__name__
  65. except AttributeError:
  66. old_name = func.__name__
  67. if new_name is None:
  68. depdoc = "`%s` is deprecated!" % old_name
  69. else:
  70. depdoc = "`%s` is deprecated, use `%s` instead!" % \
  71. (old_name, new_name)
  72. if message is not None:
  73. depdoc += "\n" + message
  74. def newfunc(*args,**kwds):
  75. """`arrayrange` is deprecated, use `arange` instead!"""
  76. warnings.warn(depdoc, DeprecationWarning, stacklevel=2)
  77. return func(*args, **kwds)
  78. newfunc = _set_function_name(newfunc, old_name)
  79. doc = func.__doc__
  80. if doc is None:
  81. doc = depdoc
  82. else:
  83. lines = doc.expandtabs().split('\n')
  84. indent = _get_indent(lines[1:])
  85. if lines[0].lstrip():
  86. # Indent the original first line to let inspect.cleandoc()
  87. # dedent the docstring despite the deprecation notice.
  88. doc = indent * ' ' + doc
  89. else:
  90. # Remove the same leading blank lines as cleandoc() would.
  91. skip = len(lines[0]) + 1
  92. for line in lines[1:]:
  93. if len(line) > indent:
  94. break
  95. skip += len(line) + 1
  96. doc = doc[skip:]
  97. depdoc = textwrap.indent(depdoc, ' ' * indent)
  98. doc = '\n\n'.join([depdoc, doc])
  99. newfunc.__doc__ = doc
  100. try:
  101. d = func.__dict__
  102. except AttributeError:
  103. pass
  104. else:
  105. newfunc.__dict__.update(d)
  106. return newfunc
  107. def _get_indent(lines):
  108. """
  109. Determines the leading whitespace that could be removed from all the lines.
  110. """
  111. indent = sys.maxsize
  112. for line in lines:
  113. content = len(line.lstrip())
  114. if content:
  115. indent = min(indent, len(line) - content)
  116. if indent == sys.maxsize:
  117. indent = 0
  118. return indent
  119. def deprecate(*args, **kwargs):
  120. """
  121. Issues a DeprecationWarning, adds warning to `old_name`'s
  122. docstring, rebinds ``old_name.__name__`` and returns the new
  123. function object.
  124. This function may also be used as a decorator.
  125. Parameters
  126. ----------
  127. func : function
  128. The function to be deprecated.
  129. old_name : str, optional
  130. The name of the function to be deprecated. Default is None, in
  131. which case the name of `func` is used.
  132. new_name : str, optional
  133. The new name for the function. Default is None, in which case the
  134. deprecation message is that `old_name` is deprecated. If given, the
  135. deprecation message is that `old_name` is deprecated and `new_name`
  136. should be used instead.
  137. message : str, optional
  138. Additional explanation of the deprecation. Displayed in the
  139. docstring after the warning.
  140. Returns
  141. -------
  142. old_func : function
  143. The deprecated function.
  144. Examples
  145. --------
  146. Note that ``olduint`` returns a value after printing Deprecation
  147. Warning:
  148. >>> olduint = np.deprecate(np.uint)
  149. DeprecationWarning: `uint64` is deprecated! # may vary
  150. >>> olduint(6)
  151. 6
  152. """
  153. # Deprecate may be run as a function or as a decorator
  154. # If run as a function, we initialise the decorator class
  155. # and execute its __call__ method.
  156. if args:
  157. fn = args[0]
  158. args = args[1:]
  159. return _Deprecate(*args, **kwargs)(fn)
  160. else:
  161. return _Deprecate(*args, **kwargs)
  162. def deprecate_with_doc(msg):
  163. """
  164. Deprecates a function and includes the deprecation in its docstring.
  165. This function is used as a decorator. It returns an object that can be
  166. used to issue a DeprecationWarning, by passing the to-be decorated
  167. function as argument, this adds warning to the to-be decorated function's
  168. docstring and returns the new function object.
  169. See Also
  170. --------
  171. deprecate : Decorate a function such that it issues a `DeprecationWarning`
  172. Parameters
  173. ----------
  174. msg : str
  175. Additional explanation of the deprecation. Displayed in the
  176. docstring after the warning.
  177. Returns
  178. -------
  179. obj : object
  180. """
  181. return _Deprecate(message=msg)
  182. #--------------------------------------------
  183. # Determine if two arrays can share memory
  184. #--------------------------------------------
  185. def byte_bounds(a):
  186. """
  187. Returns pointers to the end-points of an array.
  188. Parameters
  189. ----------
  190. a : ndarray
  191. Input array. It must conform to the Python-side of the array
  192. interface.
  193. Returns
  194. -------
  195. (low, high) : tuple of 2 integers
  196. The first integer is the first byte of the array, the second
  197. integer is just past the last byte of the array. If `a` is not
  198. contiguous it will not use every byte between the (`low`, `high`)
  199. values.
  200. Examples
  201. --------
  202. >>> I = np.eye(2, dtype='f'); I.dtype
  203. dtype('float32')
  204. >>> low, high = np.byte_bounds(I)
  205. >>> high - low == I.size*I.itemsize
  206. True
  207. >>> I = np.eye(2); I.dtype
  208. dtype('float64')
  209. >>> low, high = np.byte_bounds(I)
  210. >>> high - low == I.size*I.itemsize
  211. True
  212. """
  213. ai = a.__array_interface__
  214. a_data = ai['data'][0]
  215. astrides = ai['strides']
  216. ashape = ai['shape']
  217. bytes_a = asarray(a).dtype.itemsize
  218. a_low = a_high = a_data
  219. if astrides is None:
  220. # contiguous case
  221. a_high += a.size * bytes_a
  222. else:
  223. for shape, stride in zip(ashape, astrides):
  224. if stride < 0:
  225. a_low += (shape-1)*stride
  226. else:
  227. a_high += (shape-1)*stride
  228. a_high += bytes_a
  229. return a_low, a_high
  230. #-----------------------------------------------------------------------------
  231. # Function for output and information on the variables used.
  232. #-----------------------------------------------------------------------------
  233. def who(vardict=None):
  234. """
  235. Print the NumPy arrays in the given dictionary.
  236. If there is no dictionary passed in or `vardict` is None then returns
  237. NumPy arrays in the globals() dictionary (all NumPy arrays in the
  238. namespace).
  239. Parameters
  240. ----------
  241. vardict : dict, optional
  242. A dictionary possibly containing ndarrays. Default is globals().
  243. Returns
  244. -------
  245. out : None
  246. Returns 'None'.
  247. Notes
  248. -----
  249. Prints out the name, shape, bytes and type of all of the ndarrays
  250. present in `vardict`.
  251. Examples
  252. --------
  253. >>> a = np.arange(10)
  254. >>> b = np.ones(20)
  255. >>> np.who()
  256. Name Shape Bytes Type
  257. ===========================================================
  258. a 10 80 int64
  259. b 20 160 float64
  260. Upper bound on total bytes = 240
  261. >>> d = {'x': np.arange(2.0), 'y': np.arange(3.0), 'txt': 'Some str',
  262. ... 'idx':5}
  263. >>> np.who(d)
  264. Name Shape Bytes Type
  265. ===========================================================
  266. x 2 16 float64
  267. y 3 24 float64
  268. Upper bound on total bytes = 40
  269. """
  270. if vardict is None:
  271. frame = sys._getframe().f_back
  272. vardict = frame.f_globals
  273. sta = []
  274. cache = {}
  275. for name in vardict.keys():
  276. if isinstance(vardict[name], ndarray):
  277. var = vardict[name]
  278. idv = id(var)
  279. if idv in cache.keys():
  280. namestr = name + " (%s)" % cache[idv]
  281. original = 0
  282. else:
  283. cache[idv] = name
  284. namestr = name
  285. original = 1
  286. shapestr = " x ".join(map(str, var.shape))
  287. bytestr = str(var.nbytes)
  288. sta.append([namestr, shapestr, bytestr, var.dtype.name,
  289. original])
  290. maxname = 0
  291. maxshape = 0
  292. maxbyte = 0
  293. totalbytes = 0
  294. for k in range(len(sta)):
  295. val = sta[k]
  296. if maxname < len(val[0]):
  297. maxname = len(val[0])
  298. if maxshape < len(val[1]):
  299. maxshape = len(val[1])
  300. if maxbyte < len(val[2]):
  301. maxbyte = len(val[2])
  302. if val[4]:
  303. totalbytes += int(val[2])
  304. if len(sta) > 0:
  305. sp1 = max(10, maxname)
  306. sp2 = max(10, maxshape)
  307. sp3 = max(10, maxbyte)
  308. prval = "Name %s Shape %s Bytes %s Type" % (sp1*' ', sp2*' ', sp3*' ')
  309. print(prval + "\n" + "="*(len(prval)+5) + "\n")
  310. for k in range(len(sta)):
  311. val = sta[k]
  312. print("%s %s %s %s %s %s %s" % (val[0], ' '*(sp1-len(val[0])+4),
  313. val[1], ' '*(sp2-len(val[1])+5),
  314. val[2], ' '*(sp3-len(val[2])+5),
  315. val[3]))
  316. print("\nUpper bound on total bytes = %d" % totalbytes)
  317. return
  318. #-----------------------------------------------------------------------------
  319. # NOTE: pydoc defines a help function which works similarly to this
  320. # except it uses a pager to take over the screen.
  321. # combine name and arguments and split to multiple lines of width
  322. # characters. End lines on a comma and begin argument list indented with
  323. # the rest of the arguments.
  324. def _split_line(name, arguments, width):
  325. firstwidth = len(name)
  326. k = firstwidth
  327. newstr = name
  328. sepstr = ", "
  329. arglist = arguments.split(sepstr)
  330. for argument in arglist:
  331. if k == firstwidth:
  332. addstr = ""
  333. else:
  334. addstr = sepstr
  335. k = k + len(argument) + len(addstr)
  336. if k > width:
  337. k = firstwidth + 1 + len(argument)
  338. newstr = newstr + ",\n" + " "*(firstwidth+2) + argument
  339. else:
  340. newstr = newstr + addstr + argument
  341. return newstr
  342. _namedict = None
  343. _dictlist = None
  344. # Traverse all module directories underneath globals
  345. # to see if something is defined
  346. def _makenamedict(module='numpy'):
  347. module = __import__(module, globals(), locals(), [])
  348. thedict = {module.__name__:module.__dict__}
  349. dictlist = [module.__name__]
  350. totraverse = [module.__dict__]
  351. while True:
  352. if len(totraverse) == 0:
  353. break
  354. thisdict = totraverse.pop(0)
  355. for x in thisdict.keys():
  356. if isinstance(thisdict[x], types.ModuleType):
  357. modname = thisdict[x].__name__
  358. if modname not in dictlist:
  359. moddict = thisdict[x].__dict__
  360. dictlist.append(modname)
  361. totraverse.append(moddict)
  362. thedict[modname] = moddict
  363. return thedict, dictlist
  364. def _info(obj, output=sys.stdout):
  365. """Provide information about ndarray obj.
  366. Parameters
  367. ----------
  368. obj : ndarray
  369. Must be ndarray, not checked.
  370. output
  371. Where printed output goes.
  372. Notes
  373. -----
  374. Copied over from the numarray module prior to its removal.
  375. Adapted somewhat as only numpy is an option now.
  376. Called by info.
  377. """
  378. extra = ""
  379. tic = ""
  380. bp = lambda x: x
  381. cls = getattr(obj, '__class__', type(obj))
  382. nm = getattr(cls, '__name__', cls)
  383. strides = obj.strides
  384. endian = obj.dtype.byteorder
  385. print("class: ", nm, file=output)
  386. print("shape: ", obj.shape, file=output)
  387. print("strides: ", strides, file=output)
  388. print("itemsize: ", obj.itemsize, file=output)
  389. print("aligned: ", bp(obj.flags.aligned), file=output)
  390. print("contiguous: ", bp(obj.flags.contiguous), file=output)
  391. print("fortran: ", obj.flags.fortran, file=output)
  392. print(
  393. "data pointer: %s%s" % (hex(obj.ctypes._as_parameter_.value), extra),
  394. file=output
  395. )
  396. print("byteorder: ", end=' ', file=output)
  397. if endian in ['|', '=']:
  398. print("%s%s%s" % (tic, sys.byteorder, tic), file=output)
  399. byteswap = False
  400. elif endian == '>':
  401. print("%sbig%s" % (tic, tic), file=output)
  402. byteswap = sys.byteorder != "big"
  403. else:
  404. print("%slittle%s" % (tic, tic), file=output)
  405. byteswap = sys.byteorder != "little"
  406. print("byteswap: ", bp(byteswap), file=output)
  407. print("type: %s" % obj.dtype, file=output)
  408. @set_module('numpy')
  409. def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'):
  410. """
  411. Get help information for a function, class, or module.
  412. Parameters
  413. ----------
  414. object : object or str, optional
  415. Input object or name to get information about. If `object` is a
  416. numpy object, its docstring is given. If it is a string, available
  417. modules are searched for matching objects. If None, information
  418. about `info` itself is returned.
  419. maxwidth : int, optional
  420. Printing width.
  421. output : file like object, optional
  422. File like object that the output is written to, default is
  423. ``stdout``. The object has to be opened in 'w' or 'a' mode.
  424. toplevel : str, optional
  425. Start search at this level.
  426. See Also
  427. --------
  428. source, lookfor
  429. Notes
  430. -----
  431. When used interactively with an object, ``np.info(obj)`` is equivalent
  432. to ``help(obj)`` on the Python prompt or ``obj?`` on the IPython
  433. prompt.
  434. Examples
  435. --------
  436. >>> np.info(np.polyval) # doctest: +SKIP
  437. polyval(p, x)
  438. Evaluate the polynomial p at x.
  439. ...
  440. When using a string for `object` it is possible to get multiple results.
  441. >>> np.info('fft') # doctest: +SKIP
  442. *** Found in numpy ***
  443. Core FFT routines
  444. ...
  445. *** Found in numpy.fft ***
  446. fft(a, n=None, axis=-1)
  447. ...
  448. *** Repeat reference found in numpy.fft.fftpack ***
  449. *** Total of 3 references found. ***
  450. """
  451. global _namedict, _dictlist
  452. # Local import to speed up numpy's import time.
  453. import pydoc
  454. import inspect
  455. if (hasattr(object, '_ppimport_importer') or
  456. hasattr(object, '_ppimport_module')):
  457. object = object._ppimport_module
  458. elif hasattr(object, '_ppimport_attr'):
  459. object = object._ppimport_attr
  460. if object is None:
  461. info(info)
  462. elif isinstance(object, ndarray):
  463. _info(object, output=output)
  464. elif isinstance(object, str):
  465. if _namedict is None:
  466. _namedict, _dictlist = _makenamedict(toplevel)
  467. numfound = 0
  468. objlist = []
  469. for namestr in _dictlist:
  470. try:
  471. obj = _namedict[namestr][object]
  472. if id(obj) in objlist:
  473. print("\n "
  474. "*** Repeat reference found in %s *** " % namestr,
  475. file=output
  476. )
  477. else:
  478. objlist.append(id(obj))
  479. print(" *** Found in %s ***" % namestr, file=output)
  480. info(obj)
  481. print("-"*maxwidth, file=output)
  482. numfound += 1
  483. except KeyError:
  484. pass
  485. if numfound == 0:
  486. print("Help for %s not found." % object, file=output)
  487. else:
  488. print("\n "
  489. "*** Total of %d references found. ***" % numfound,
  490. file=output
  491. )
  492. elif inspect.isfunction(object) or inspect.ismethod(object):
  493. name = object.__name__
  494. try:
  495. arguments = str(inspect.signature(object))
  496. except Exception:
  497. arguments = "()"
  498. if len(name+arguments) > maxwidth:
  499. argstr = _split_line(name, arguments, maxwidth)
  500. else:
  501. argstr = name + arguments
  502. print(" " + argstr + "\n", file=output)
  503. print(inspect.getdoc(object), file=output)
  504. elif inspect.isclass(object):
  505. name = object.__name__
  506. try:
  507. arguments = str(inspect.signature(object))
  508. except Exception:
  509. arguments = "()"
  510. if len(name+arguments) > maxwidth:
  511. argstr = _split_line(name, arguments, maxwidth)
  512. else:
  513. argstr = name + arguments
  514. print(" " + argstr + "\n", file=output)
  515. doc1 = inspect.getdoc(object)
  516. if doc1 is None:
  517. if hasattr(object, '__init__'):
  518. print(inspect.getdoc(object.__init__), file=output)
  519. else:
  520. print(inspect.getdoc(object), file=output)
  521. methods = pydoc.allmethods(object)
  522. public_methods = [meth for meth in methods if meth[0] != '_']
  523. if public_methods:
  524. print("\n\nMethods:\n", file=output)
  525. for meth in public_methods:
  526. thisobj = getattr(object, meth, None)
  527. if thisobj is not None:
  528. methstr, other = pydoc.splitdoc(
  529. inspect.getdoc(thisobj) or "None"
  530. )
  531. print(" %s -- %s" % (meth, methstr), file=output)
  532. elif hasattr(object, '__doc__'):
  533. print(inspect.getdoc(object), file=output)
  534. @set_module('numpy')
  535. def source(object, output=sys.stdout):
  536. """
  537. Print or write to a file the source code for a NumPy object.
  538. The source code is only returned for objects written in Python. Many
  539. functions and classes are defined in C and will therefore not return
  540. useful information.
  541. Parameters
  542. ----------
  543. object : numpy object
  544. Input object. This can be any object (function, class, module,
  545. ...).
  546. output : file object, optional
  547. If `output` not supplied then source code is printed to screen
  548. (sys.stdout). File object must be created with either write 'w' or
  549. append 'a' modes.
  550. See Also
  551. --------
  552. lookfor, info
  553. Examples
  554. --------
  555. >>> np.source(np.interp) #doctest: +SKIP
  556. In file: /usr/lib/python2.6/dist-packages/numpy/lib/function_base.py
  557. def interp(x, xp, fp, left=None, right=None):
  558. \"\"\".... (full docstring printed)\"\"\"
  559. if isinstance(x, (float, int, number)):
  560. return compiled_interp([x], xp, fp, left, right).item()
  561. else:
  562. return compiled_interp(x, xp, fp, left, right)
  563. The source code is only returned for objects written in Python.
  564. >>> np.source(np.array) #doctest: +SKIP
  565. Not available for this object.
  566. """
  567. # Local import to speed up numpy's import time.
  568. import inspect
  569. try:
  570. print("In file: %s\n" % inspect.getsourcefile(object), file=output)
  571. print(inspect.getsource(object), file=output)
  572. except Exception:
  573. print("Not available for this object.", file=output)
  574. # Cache for lookfor: {id(module): {name: (docstring, kind, index), ...}...}
  575. # where kind: "func", "class", "module", "object"
  576. # and index: index in breadth-first namespace traversal
  577. _lookfor_caches = {}
  578. # regexp whose match indicates that the string may contain a function
  579. # signature
  580. _function_signature_re = re.compile(r"[a-z0-9_]+\(.*[,=].*\)", re.I)
  581. @set_module('numpy')
  582. def lookfor(what, module=None, import_modules=True, regenerate=False,
  583. output=None):
  584. """
  585. Do a keyword search on docstrings.
  586. A list of objects that matched the search is displayed,
  587. sorted by relevance. All given keywords need to be found in the
  588. docstring for it to be returned as a result, but the order does
  589. not matter.
  590. Parameters
  591. ----------
  592. what : str
  593. String containing words to look for.
  594. module : str or list, optional
  595. Name of module(s) whose docstrings to go through.
  596. import_modules : bool, optional
  597. Whether to import sub-modules in packages. Default is True.
  598. regenerate : bool, optional
  599. Whether to re-generate the docstring cache. Default is False.
  600. output : file-like, optional
  601. File-like object to write the output to. If omitted, use a pager.
  602. See Also
  603. --------
  604. source, info
  605. Notes
  606. -----
  607. Relevance is determined only roughly, by checking if the keywords occur
  608. in the function name, at the start of a docstring, etc.
  609. Examples
  610. --------
  611. >>> np.lookfor('binary representation') # doctest: +SKIP
  612. Search results for 'binary representation'
  613. ------------------------------------------
  614. numpy.binary_repr
  615. Return the binary representation of the input number as a string.
  616. numpy.core.setup_common.long_double_representation
  617. Given a binary dump as given by GNU od -b, look for long double
  618. numpy.base_repr
  619. Return a string representation of a number in the given base system.
  620. ...
  621. """
  622. import pydoc
  623. # Cache
  624. cache = _lookfor_generate_cache(module, import_modules, regenerate)
  625. # Search
  626. # XXX: maybe using a real stemming search engine would be better?
  627. found = []
  628. whats = str(what).lower().split()
  629. if not whats:
  630. return
  631. for name, (docstring, kind, index) in cache.items():
  632. if kind in ('module', 'object'):
  633. # don't show modules or objects
  634. continue
  635. doc = docstring.lower()
  636. if all(w in doc for w in whats):
  637. found.append(name)
  638. # Relevance sort
  639. # XXX: this is full Harrison-Stetson heuristics now,
  640. # XXX: it probably could be improved
  641. kind_relevance = {'func': 1000, 'class': 1000,
  642. 'module': -1000, 'object': -1000}
  643. def relevance(name, docstr, kind, index):
  644. r = 0
  645. # do the keywords occur within the start of the docstring?
  646. first_doc = "\n".join(docstr.lower().strip().split("\n")[:3])
  647. r += sum([200 for w in whats if w in first_doc])
  648. # do the keywords occur in the function name?
  649. r += sum([30 for w in whats if w in name])
  650. # is the full name long?
  651. r += -len(name) * 5
  652. # is the object of bad type?
  653. r += kind_relevance.get(kind, -1000)
  654. # is the object deep in namespace hierarchy?
  655. r += -name.count('.') * 10
  656. r += max(-index / 100, -100)
  657. return r
  658. def relevance_value(a):
  659. return relevance(a, *cache[a])
  660. found.sort(key=relevance_value)
  661. # Pretty-print
  662. s = "Search results for '%s'" % (' '.join(whats))
  663. help_text = [s, "-"*len(s)]
  664. for name in found[::-1]:
  665. doc, kind, ix = cache[name]
  666. doclines = [line.strip() for line in doc.strip().split("\n")
  667. if line.strip()]
  668. # find a suitable short description
  669. try:
  670. first_doc = doclines[0].strip()
  671. if _function_signature_re.search(first_doc):
  672. first_doc = doclines[1].strip()
  673. except IndexError:
  674. first_doc = ""
  675. help_text.append("%s\n %s" % (name, first_doc))
  676. if not found:
  677. help_text.append("Nothing found.")
  678. # Output
  679. if output is not None:
  680. output.write("\n".join(help_text))
  681. elif len(help_text) > 10:
  682. pager = pydoc.getpager()
  683. pager("\n".join(help_text))
  684. else:
  685. print("\n".join(help_text))
  686. def _lookfor_generate_cache(module, import_modules, regenerate):
  687. """
  688. Generate docstring cache for given module.
  689. Parameters
  690. ----------
  691. module : str, None, module
  692. Module for which to generate docstring cache
  693. import_modules : bool
  694. Whether to import sub-modules in packages.
  695. regenerate : bool
  696. Re-generate the docstring cache
  697. Returns
  698. -------
  699. cache : dict {obj_full_name: (docstring, kind, index), ...}
  700. Docstring cache for the module, either cached one (regenerate=False)
  701. or newly generated.
  702. """
  703. # Local import to speed up numpy's import time.
  704. import inspect
  705. from io import StringIO
  706. if module is None:
  707. module = "numpy"
  708. if isinstance(module, str):
  709. try:
  710. __import__(module)
  711. except ImportError:
  712. return {}
  713. module = sys.modules[module]
  714. elif isinstance(module, list) or isinstance(module, tuple):
  715. cache = {}
  716. for mod in module:
  717. cache.update(_lookfor_generate_cache(mod, import_modules,
  718. regenerate))
  719. return cache
  720. if id(module) in _lookfor_caches and not regenerate:
  721. return _lookfor_caches[id(module)]
  722. # walk items and collect docstrings
  723. cache = {}
  724. _lookfor_caches[id(module)] = cache
  725. seen = {}
  726. index = 0
  727. stack = [(module.__name__, module)]
  728. while stack:
  729. name, item = stack.pop(0)
  730. if id(item) in seen:
  731. continue
  732. seen[id(item)] = True
  733. index += 1
  734. kind = "object"
  735. if inspect.ismodule(item):
  736. kind = "module"
  737. try:
  738. _all = item.__all__
  739. except AttributeError:
  740. _all = None
  741. # import sub-packages
  742. if import_modules and hasattr(item, '__path__'):
  743. for pth in item.__path__:
  744. for mod_path in os.listdir(pth):
  745. this_py = os.path.join(pth, mod_path)
  746. init_py = os.path.join(pth, mod_path, '__init__.py')
  747. if (os.path.isfile(this_py) and
  748. mod_path.endswith('.py')):
  749. to_import = mod_path[:-3]
  750. elif os.path.isfile(init_py):
  751. to_import = mod_path
  752. else:
  753. continue
  754. if to_import == '__init__':
  755. continue
  756. try:
  757. old_stdout = sys.stdout
  758. old_stderr = sys.stderr
  759. try:
  760. sys.stdout = StringIO()
  761. sys.stderr = StringIO()
  762. __import__("%s.%s" % (name, to_import))
  763. finally:
  764. sys.stdout = old_stdout
  765. sys.stderr = old_stderr
  766. # Catch SystemExit, too
  767. except BaseException:
  768. continue
  769. for n, v in _getmembers(item):
  770. try:
  771. item_name = getattr(v, '__name__', "%s.%s" % (name, n))
  772. mod_name = getattr(v, '__module__', None)
  773. except NameError:
  774. # ref. SWIG's global cvars
  775. # NameError: Unknown C global variable
  776. item_name = "%s.%s" % (name, n)
  777. mod_name = None
  778. if '.' not in item_name and mod_name:
  779. item_name = "%s.%s" % (mod_name, item_name)
  780. if not item_name.startswith(name + '.'):
  781. # don't crawl "foreign" objects
  782. if isinstance(v, ufunc):
  783. # ... unless they are ufuncs
  784. pass
  785. else:
  786. continue
  787. elif not (inspect.ismodule(v) or _all is None or n in _all):
  788. continue
  789. stack.append(("%s.%s" % (name, n), v))
  790. elif inspect.isclass(item):
  791. kind = "class"
  792. for n, v in _getmembers(item):
  793. stack.append(("%s.%s" % (name, n), v))
  794. elif hasattr(item, "__call__"):
  795. kind = "func"
  796. try:
  797. doc = inspect.getdoc(item)
  798. except NameError:
  799. # ref SWIG's NameError: Unknown C global variable
  800. doc = None
  801. if doc is not None:
  802. cache[name] = (doc, kind, index)
  803. return cache
  804. def _getmembers(item):
  805. import inspect
  806. try:
  807. members = inspect.getmembers(item)
  808. except Exception:
  809. members = [(x, getattr(item, x)) for x in dir(item)
  810. if hasattr(item, x)]
  811. return members
  812. def safe_eval(source):
  813. """
  814. Protected string evaluation.
  815. Evaluate a string containing a Python literal expression without
  816. allowing the execution of arbitrary non-literal code.
  817. Parameters
  818. ----------
  819. source : str
  820. The string to evaluate.
  821. Returns
  822. -------
  823. obj : object
  824. The result of evaluating `source`.
  825. Raises
  826. ------
  827. SyntaxError
  828. If the code has invalid Python syntax, or if it contains
  829. non-literal code.
  830. Examples
  831. --------
  832. >>> np.safe_eval('1')
  833. 1
  834. >>> np.safe_eval('[1, 2, 3]')
  835. [1, 2, 3]
  836. >>> np.safe_eval('{"foo": ("bar", 10.0)}')
  837. {'foo': ('bar', 10.0)}
  838. >>> np.safe_eval('import os')
  839. Traceback (most recent call last):
  840. ...
  841. SyntaxError: invalid syntax
  842. >>> np.safe_eval('open("/home/user/.ssh/id_dsa").read()')
  843. Traceback (most recent call last):
  844. ...
  845. ValueError: malformed node or string: <_ast.Call object at 0x...>
  846. """
  847. # Local import to speed up numpy's import time.
  848. import ast
  849. return ast.literal_eval(source)
  850. def _median_nancheck(data, result, axis, out):
  851. """
  852. Utility function to check median result from data for NaN values at the end
  853. and return NaN in that case. Input result can also be a MaskedArray.
  854. Parameters
  855. ----------
  856. data : array
  857. Input data to median function
  858. result : Array or MaskedArray
  859. Result of median function
  860. axis : int
  861. Axis along which the median was computed.
  862. out : ndarray, optional
  863. Output array in which to place the result.
  864. Returns
  865. -------
  866. median : scalar or ndarray
  867. Median or NaN in axes which contained NaN in the input.
  868. """
  869. if data.size == 0:
  870. return result
  871. n = np.isnan(data.take(-1, axis=axis))
  872. # masked NaN values are ok
  873. if np.ma.isMaskedArray(n):
  874. n = n.filled(False)
  875. if result.ndim == 0:
  876. if n == True:
  877. if out is not None:
  878. out[...] = data.dtype.type(np.nan)
  879. result = out
  880. else:
  881. result = data.dtype.type(np.nan)
  882. elif np.count_nonzero(n.ravel()) > 0:
  883. result[n] = np.nan
  884. return result
  885. def _opt_info():
  886. """
  887. Returns a string contains the supported CPU features by the current build.
  888. The string format can be explained as follows:
  889. - dispatched features that are supported by the running machine
  890. end with `*`.
  891. - dispatched features that are "not" supported by the running machine
  892. end with `?`.
  893. - remained features are representing the baseline.
  894. """
  895. from numpy.core._multiarray_umath import (
  896. __cpu_features__, __cpu_baseline__, __cpu_dispatch__
  897. )
  898. if len(__cpu_baseline__) == 0 and len(__cpu_dispatch__) == 0:
  899. return ''
  900. enabled_features = ' '.join(__cpu_baseline__)
  901. for feature in __cpu_dispatch__:
  902. if __cpu_features__[feature]:
  903. enabled_features += f" {feature}*"
  904. else:
  905. enabled_features += f" {feature}?"
  906. return enabled_features
  907. #-----------------------------------------------------------------------------