提交 c9ee9d37 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

A few PEP8 fixes

Also fixed some small typos and replaced a zip by izip.
上级 2068a94b
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
__docformat__ = "restructuredtext en" __docformat__ = "restructuredtext en"
import time, copy, sys, copy_reg, gc, os import time, copy, sys, copy_reg, gc, os
from itertools import izip
from StringIO import StringIO from StringIO import StringIO
import numpy import numpy
...@@ -1931,35 +1932,39 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions ...@@ -1931,35 +1932,39 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions
""" """
Create a function. Create a function.
defaults -> a list matching the inputs list and providing default values defaults -> a list matching the inputs list and providing default
if the default for an input is None, then that input is a values if the default for an input is None, then that input
required input. For an input with an update, the default is a required input. For an input with an update, the
acts as initialization. default acts as initialization.
trustme -> disables some exceptions, used internally trustme -> disables some exceptions, used internally
""" """
if defaults is None: if defaults is None:
defaults = [None] * len(self.inputs) defaults = [None] * len(self.inputs)
input_storage = [] # list of independent one-element lists, will be passed to the linker # List of independent one-element lists, will be passed to the linker.
input_storage = []
_defaults = [] _defaults = []
# The following loop is to fill in the input_storage and _defaults lists. # The following loop is to fill in the input_storage and _defaults
for (input, indices, subinputs), default in zip(self.indices, defaults): # lists.
for (input, indices, subinputs), default in izip(self.indices,
defaults):
__default = default __default = default
if isinstance(default, gof.Container): if isinstance(default, gof.Container):
# If the default is a gof.Container, this means we want to share # If the default is a gof.Container, this means we want to
# the same storage. This is done by appending default.storage # share the same storage. This is done by appending
# to input_storage # default.storage to input_storage.
if indices is not None: if indices is not None:
raise TypeError("Cannot take a Container instance as default for a SymbolicInputKit.") raise TypeError("Cannot take a Container instance as "
"default for a SymbolicInputKit.")
input_storage.append(default.storage) input_storage.append(default.storage)
default = None default = None
required = False required = False
elif isinstance(input, SymbolicInputKit): elif isinstance(input, SymbolicInputKit):
# If the input is a SymbolicInputKit, it represents more than # If the input is a SymbolicInputKit, it represents more than
# one storage unit. The indices and subinputs lists represent which # one storage unit. The indices and subinputs lists represent
# of the kit's inputs are active in this graph, so we make as many # which of the kit's inputs are active in this graph, so we
# storage units as needed # make as many storage units as needed
if isinstance(default, (list, tuple)) \ if isinstance(default, (list, tuple)) \
and all(isinstance(x, gof.Container) for x in default): and all(isinstance(x, gof.Container) for x in default):
if len(default) == len(indices): if len(default) == len(indices):
...@@ -1967,7 +1972,9 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions ...@@ -1967,7 +1972,9 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions
elif len(default) > len(indices): elif len(default) > len(indices):
input_storage += [default[i].storage for i in indices] input_storage += [default[i].storage for i in indices]
else: else:
raise ValueError('Not enough storage for SymbolicInputKit', input, indices, default) raise ValueError(
'Not enough storage for SymbolicInputKit',
input, indices, default)
default = _NODEFAULT default = _NODEFAULT
else: else:
input_storage += [[None] for i in indices] input_storage += [[None] for i in indices]
...@@ -1977,8 +1984,10 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions ...@@ -1977,8 +1984,10 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions
# Filling _defaults. Each entry is a tuple of three elements: # Filling _defaults. Each entry is a tuple of three elements:
# (required, refeed, value) # (required, refeed, value)
# - required means that the user must provide a value when calling the function # - required means that the user must provide a value when calling
# - refeed means that we want to put the default back in the storage after each function call # the function
# - refeed means that we want to put the default back in the
# storage after each function call
# - value is the value that will be put in the storage initially # - value is the value that will be put in the storage initially
# Even though a SymbolicInputKit represents more than one input, # Even though a SymbolicInputKit represents more than one input,
...@@ -2001,7 +2010,9 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions ...@@ -2001,7 +2010,9 @@ class _Maker(FunctionMaker): # inheritance buys a few helper functions
_defaults.append((False, False, None)) _defaults.append((False, False, None))
else: else:
# This might catch some bugs early # This might catch some bugs early
raise ValueError("A default (initial) value is required for an input which can update itself.", input) raise ValueError(
"A default (initial) value is required for an "
"input which can update itself.", input)
else: else:
_defaults.append((False, False, default)) _defaults.append((False, False, default))
else: else:
...@@ -2066,8 +2077,8 @@ class DebugMode(Mode): ...@@ -2066,8 +2077,8 @@ class DebugMode(Mode):
If there are internal errors, this mode will raise an If there are internal errors, this mode will raise an
`DebugModeError` exception. `DebugModeError` exception.
:remark: The work of debugging is implemented by the `_Maker`, `_Linker`, and :remark: The work of debugging is implemented by the `_Maker`, `_Linker`,
`_VariableEquivalenceTracker` classes. and `_VariableEquivalenceTracker` classes.
""" """
...@@ -2084,7 +2095,8 @@ class DebugMode(Mode): ...@@ -2084,7 +2095,8 @@ class DebugMode(Mode):
check_py_code = config.DebugMode.check_py check_py_code = config.DebugMode.check_py
""" """
Should we evaluate (and check) the `perform` implementations? Always checked if no `c_code`. Should we evaluate (and check) the `perform` implementations?
Always checked if no `c_code`.
""" """
check_isfinite = config.DebugMode.check_finite check_isfinite = config.DebugMode.check_finite
...@@ -2102,7 +2114,9 @@ class DebugMode(Mode): ...@@ -2102,7 +2114,9 @@ class DebugMode(Mode):
# This function will be used to create a FunctionMaker in # This function will be used to create a FunctionMaker in
# function_module.function # function_module.function
def function_maker(self, i, o, m, *args, **kwargs): def function_maker(self, i, o, m, *args, **kwargs):
"""Return an instance of `_Maker` which handles much of the debugging work""" """
Return an instance of `_Maker` which handles much of the debugging work
"""
assert m is self assert m is self
return _Maker(i, o, self.optimizer, self, *args, **kwargs) return _Maker(i, o, self.optimizer, self, *args, **kwargs)
...@@ -2114,13 +2128,18 @@ class DebugMode(Mode): ...@@ -2114,13 +2128,18 @@ class DebugMode(Mode):
check_isfinite=None, check_isfinite=None,
require_matching_strides=None, require_matching_strides=None,
linker=None): linker=None):
"""Initialize member variables. """Initialize member variables.
If any of these arguments (except optimizer) is not None, it overrides the class default. If any of these arguments (except optimizer) is not None, it overrides
The linker arguments is not used. It is set their to allow Mode.requiring() and some other fct to work with DebugMode too. the class default.
The linker argument is not used. It is set there to allow
Mode.requiring() and some other fct to work with DebugMode too.
""" """
if linker is not None and not issubclass(linker, _Linker): if linker is not None and not issubclass(linker, _Linker):
raise Exception("DebugMode can use only its own linker! Don't give him one to use it.", linker) raise Exception("DebugMode can only use its own linker! You "
"should not provide one.", linker)
super(DebugMode, self).__init__( super(DebugMode, self).__init__(
optimizer=optimizer, optimizer=optimizer,
...@@ -2142,6 +2161,7 @@ class DebugMode(Mode): ...@@ -2142,6 +2161,7 @@ class DebugMode(Mode):
self.require_matching_strides = require_matching_strides self.require_matching_strides = require_matching_strides
if not (self.check_c_code or self.check_py_code): if not (self.check_c_code or self.check_py_code):
raise ValueError('DebugMode has to check at least one of c and py code') raise ValueError('DebugMode has to check at least one of c and py '
'code')
register_mode('DEBUG_MODE', DebugMode(optimizer='fast_run')) register_mode('DEBUG_MODE', DebugMode(optimizer='fast_run'))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论