summaryrefslogtreecommitdiff
path: root/numpy/lib/utils.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2014-07-30 18:06:28 -0600
committerJulian Taylor <jtaylor.debian@googlemail.com>2014-07-31 21:21:17 +0200
commit01b0d7e82211b581aaff925e3ccc36cff9ac1895 (patch)
tree8ec68353d5f09b9f0411948f1345ec79f5443b4c /numpy/lib/utils.py
parentdec6658cdc10a23ad0e733fb52a814306033d88c (diff)
downloadnumpy-01b0d7e82211b581aaff925e3ccc36cff9ac1895.tar.gz
STY: Make files in numpy/lib PEP8 compliant.
The rules enforced are the same as those used for scipy.
Diffstat (limited to 'numpy/lib/utils.py')
-rw-r--r--numpy/lib/utils.py130
1 files changed, 81 insertions, 49 deletions
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py
index 17c5b9743..df0052493 100644
--- a/numpy/lib/utils.py
+++ b/numpy/lib/utils.py
@@ -60,6 +60,7 @@ class _Deprecate(object):
deprecate
"""
+
def __init__(self, old_name=None, new_name=None, message=None):
self.old_name = old_name
self.new_name = new_name
@@ -122,16 +123,16 @@ def deprecate(*args, **kwargs):
func : function
The function to be deprecated.
old_name : str, optional
- The name of the function to be deprecated. Default is None, in which
- case the name of `func` is used.
+ The name of the function to be deprecated. Default is None, in
+ which case the name of `func` is used.
new_name : str, optional
- The new name for the function. Default is None, in which case
- the deprecation message is that `old_name` is deprecated. If given,
- the deprecation message is that `old_name` is deprecated and `new_name`
+ The new name for the function. Default is None, in which case the
+ deprecation message is that `old_name` is deprecated. If given, the
+ deprecation message is that `old_name` is deprecated and `new_name`
should be used instead.
message : str, optional
- Additional explanation of the deprecation. Displayed in the docstring
- after the warning.
+ Additional explanation of the deprecation. Displayed in the
+ docstring after the warning.
Returns
-------
@@ -140,7 +141,8 @@ def deprecate(*args, **kwargs):
Examples
--------
- Note that ``olduint`` returns a value after printing Deprecation Warning:
+ Note that ``olduint`` returns a value after printing Deprecation
+ Warning:
>>> olduint = np.deprecate(np.uint)
>>> olduint(6)
@@ -183,14 +185,16 @@ def byte_bounds(a):
Parameters
----------
a : ndarray
- Input array. It must conform to the Python-side of the array interface.
+ Input array. It must conform to the Python-side of the array
+ interface.
Returns
-------
(low, high) : tuple of 2 integers
- The first integer is the first byte of the array, the second integer is
- just past the last byte of the array. If `a` is not contiguous it
- will not use every byte between the (`low`, `high`) values.
+ The first integer is the first byte of the array, the second
+ integer is just past the last byte of the array. If `a` is not
+ contiguous it will not use every byte between the (`low`, `high`)
+ values.
Examples
--------
@@ -210,11 +214,11 @@ def byte_bounds(a):
a_data = ai['data'][0]
astrides = ai['strides']
ashape = ai['shape']
-
bytes_a = asarray(a).dtype.itemsize
-
+
a_low = a_high = a_data
- if astrides is None: # contiguous case
+ if astrides is None:
+ # contiguous case
a_high += a.size * bytes_a
else:
for shape, stride in zip(ashape, astrides):
@@ -251,8 +255,8 @@ def who(vardict=None):
Notes
-----
- Prints out the name, shape, bytes and type of all of the ndarrays present
- in `vardict`.
+ Prints out the name, shape, bytes and type of all of the ndarrays
+ present in `vardict`.
Examples
--------
@@ -286,11 +290,11 @@ def who(vardict=None):
idv = id(var)
if idv in cache.keys():
namestr = name + " (%s)" % cache[idv]
- original=0
+ original = 0
else:
cache[idv] = name
namestr = name
- original=1
+ original = 1
shapestr = " x ".join(map(str, var.shape))
bytestr = str(var.nbytes)
sta.append([namestr, shapestr, bytestr, var.dtype.name,
@@ -333,9 +337,9 @@ def who(vardict=None):
# NOTE: pydoc defines a help function which works simliarly to this
# except it uses a pager to take over the screen.
-# combine name and arguments and split to multiple lines of
-# width characters. End lines on a comma and begin argument list
-# indented with the rest of the arguments.
+# combine name and arguments and split to multiple lines of width
+# characters. End lines on a comma and begin argument list indented with
+# the rest of the arguments.
def _split_line(name, arguments, width):
firstwidth = len(name)
k = firstwidth
@@ -413,7 +417,10 @@ def _info(obj, output=sys.stdout):
print("aligned: ", bp(obj.flags.aligned), file=output)
print("contiguous: ", bp(obj.flags.contiguous), file=output)
print("fortran: ", obj.flags.fortran, file=output)
- print("data pointer: %s%s" % (hex(obj.ctypes._as_parameter_.value), extra), file=output)
+ print(
+ "data pointer: %s%s" % (hex(obj.ctypes._as_parameter_.value), extra),
+ file=output
+ )
print("byteorder: ", end=' ', file=output)
if endian in ['|', '=']:
print("%s%s%s" % (tic, sys.byteorder, tic), file=output)
@@ -428,7 +435,7 @@ def _info(obj, output=sys.stdout):
print("type: %s" % obj.dtype, file=output)
-def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
+def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'):
"""
Get help information for a function, class, or module.
@@ -437,13 +444,13 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
object : object or str, optional
Input object or name to get information about. If `object` is a
numpy object, its docstring is given. If it is a string, available
- modules are searched for matching objects.
- If None, information about `info` itself is returned.
+ modules are searched for matching objects. If None, information
+ about `info` itself is returned.
maxwidth : int, optional
Printing width.
output : file like object, optional
- File like object that the output is written to, default is ``stdout``.
- The object has to be opened in 'w' or 'a' mode.
+ File like object that the output is written to, default is
+ ``stdout``. The object has to be opened in 'w' or 'a' mode.
toplevel : str, optional
Start search at this level.
@@ -453,8 +460,9 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
Notes
-----
- When used interactively with an object, ``np.info(obj)`` is equivalent to
- ``help(obj)`` on the Python prompt or ``obj?`` on the IPython prompt.
+ When used interactively with an object, ``np.info(obj)`` is equivalent
+ to ``help(obj)`` on the Python prompt or ``obj?`` on the IPython
+ prompt.
Examples
--------
@@ -478,10 +486,11 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
"""
global _namedict, _dictlist
# Local import to speed up numpy's import time.
- import pydoc, inspect
+ import pydoc
+ import inspect
- if hasattr(object, '_ppimport_importer') or \
- hasattr(object, '_ppimport_module'):
+ if (hasattr(object, '_ppimport_importer') or
+ hasattr(object, '_ppimport_module')):
object = object._ppimport_module
elif hasattr(object, '_ppimport_attr'):
object = object._ppimport_attr
@@ -499,7 +508,10 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
try:
obj = _namedict[namestr][object]
if id(obj) in objlist:
- print("\n *** Repeat reference found in %s *** " % namestr, file=output)
+ print("\n "
+ "*** Repeat reference found in %s *** " % namestr,
+ file=output
+ )
else:
objlist.append(id(obj))
print(" *** Found in %s ***" % namestr, file=output)
@@ -511,7 +523,10 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
if numfound == 0:
print("Help for %s not found." % object, file=output)
else:
- print("\n *** Total of %d references found. ***" % numfound, file=output)
+ print("\n "
+ "*** Total of %d references found. ***" % numfound,
+ file=output
+ )
elif inspect.isfunction(object):
name = object.__name__
@@ -530,7 +545,9 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
arguments = "()"
try:
if hasattr(object, '__init__'):
- arguments = inspect.formatargspec(*inspect.getargspec(object.__init__.__func__))
+ arguments = inspect.formatargspec(
+ *inspect.getargspec(object.__init__.__func__)
+ )
arglist = arguments.split(', ')
if len(arglist) > 1:
arglist[1] = "("+arglist[1]
@@ -559,7 +576,9 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
continue
thisobj = getattr(object, meth, None)
if thisobj is not None:
- methstr, other = pydoc.splitdoc(inspect.getdoc(thisobj) or "None")
+ methstr, other = pydoc.splitdoc(
+ inspect.getdoc(thisobj) or "None"
+ )
print(" %s -- %s" % (meth, methstr), file=output)
elif (sys.version_info[0] < 3
@@ -569,7 +588,9 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
print("Instance of class: ", object.__class__.__name__, file=output)
print(file=output)
if hasattr(object, '__call__'):
- arguments = inspect.formatargspec(*inspect.getargspec(object.__call__.__func__))
+ arguments = inspect.formatargspec(
+ *inspect.getargspec(object.__call__.__func__)
+ )
arglist = arguments.split(', ')
if len(arglist) > 1:
arglist[1] = "("+arglist[1]
@@ -597,7 +618,9 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'):
elif inspect.ismethod(object):
name = object.__name__
- arguments = inspect.formatargspec(*inspect.getargspec(object.__func__))
+ arguments = inspect.formatargspec(
+ *inspect.getargspec(object.__func__)
+ )
arglist = arguments.split(', ')
if len(arglist) > 1:
arglist[1] = "("+arglist[1]
@@ -628,7 +651,8 @@ def source(object, output=sys.stdout):
Parameters
----------
object : numpy object
- Input object. This can be any object (function, class, module, ...).
+ Input object. This can be any object (function, class, module,
+ ...).
output : file object, optional
If `output` not supplied then source code is printed to screen
(sys.stdout). File object must be created with either write 'w' or
@@ -669,7 +693,8 @@ def source(object, output=sys.stdout):
# and index: index in breadth-first namespace traversal
_lookfor_caches = {}
-# regexp whose match indicates that the string may contain a function signature
+# regexp whose match indicates that the string may contain a function
+# signature
_function_signature_re = re.compile(r"[a-z0-9_]+\(.*[,=].*\)", re.I)
def lookfor(what, module=None, import_modules=True, regenerate=False,
@@ -727,7 +752,8 @@ def lookfor(what, module=None, import_modules=True, regenerate=False,
# XXX: maybe using a real stemming search engine would be better?
found = []
whats = str(what).lower().split()
- if not whats: return
+ if not whats:
+ return
for name, (docstring, kind, index) in cache.items():
if kind in ('module', 'object'):
@@ -856,7 +882,8 @@ def _lookfor_generate_cache(module, import_modules, regenerate):
stack = [(module.__name__, module)]
while stack:
name, item = stack.pop(0)
- if id(item) in seen: continue
+ if id(item) in seen:
+ continue
seen[id(item)] = True
index += 1
@@ -875,7 +902,8 @@ def _lookfor_generate_cache(module, import_modules, regenerate):
for mod_path in os.listdir(pth):
this_py = os.path.join(pth, mod_path)
init_py = os.path.join(pth, mod_path, '__init__.py')
- if os.path.isfile(this_py) and mod_path.endswith('.py'):
+ if (os.path.isfile(this_py) and
+ mod_path.endswith('.py')):
to_import = mod_path[:-3]
elif os.path.isfile(init_py):
to_import = mod_path
@@ -935,7 +963,8 @@ def _lookfor_generate_cache(module, import_modules, regenerate):
try:
doc = inspect.getdoc(item)
- except NameError: # ref SWIG's NameError: Unknown C global variable
+ except NameError:
+ # ref SWIG's NameError: Unknown C global variable
doc = None
if doc is not None:
cache[name] = (doc, kind, index)
@@ -999,7 +1028,9 @@ class SafeEval(object):
return node.value
def visitDict(self, node,**kw):
- return dict([(self.visit(k), self.visit(v)) for k, v in node.items])
+ return dict(
+ [(self.visit(k), self.visit(v)) for k, v in node.items]
+ )
def visitTuple(self, node, **kw):
return tuple([self.visit(i) for i in node.nodes])
@@ -1097,8 +1128,8 @@ def safe_eval(source):
Raises
------
SyntaxError
- If the code has invalid Python syntax, or if it contains non-literal
- code.
+ If the code has invalid Python syntax, or if it contains
+ non-literal code.
Examples
--------
@@ -1124,7 +1155,8 @@ def safe_eval(source):
import warnings
with warnings.catch_warnings():
- # compiler package is deprecated for 3.x, which is already solved here
+ # compiler package is deprecated for 3.x, which is already solved
+ # here
warnings.simplefilter('ignore', DeprecationWarning)
try:
import compiler