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