提交 c789ce95 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Flake8 for gof/type.py

上级 867bba87
...@@ -45,7 +45,8 @@ class CLinkerType(CLinkerObject): ...@@ -45,7 +45,8 @@ class CLinkerType(CLinkerObject):
- `MethodNotDefined`: Subclass does not implement this method - `MethodNotDefined`: Subclass does not implement this method
""" """
raise MethodNotDefined("c_literal", type(self), self.__class__.__name__) raise MethodNotDefined("c_literal", type(self),
self.__class__.__name__)
def c_declare(self, name, sub, check_input=True): def c_declare(self, name, sub, check_input=True):
"""Required: Return c code to declare variables that will be """Required: Return c code to declare variables that will be
...@@ -56,7 +57,8 @@ class CLinkerType(CLinkerObject): ...@@ -56,7 +57,8 @@ class CLinkerType(CLinkerObject):
return "PyObject ** addr_of_%(name)s;" return "PyObject ** addr_of_%(name)s;"
:param name: the name of the ``PyObject *`` pointer that will the value for this Type :param name: the name of the ``PyObject *`` pointer that will
the value for this Type
:type name: string :type name: string
...@@ -138,7 +140,8 @@ class CLinkerType(CLinkerObject): ...@@ -138,7 +140,8 @@ class CLinkerType(CLinkerObject):
- `MethodNotDefined`: Subclass does not implement this method - `MethodNotDefined`: Subclass does not implement this method
""" """
raise MethodNotDefined("c_extract", type(self), self.__class__.__name__) raise MethodNotDefined("c_extract", type(self),
self.__class__.__name__)
def c_extract_out(self, name, sub, check_input=True): def c_extract_out(self, name, sub, check_input=True):
"""Optional: C code to extract a PyObject * instance. """Optional: C code to extract a PyObject * instance.
...@@ -156,9 +159,9 @@ class CLinkerType(CLinkerObject): ...@@ -156,9 +159,9 @@ class CLinkerType(CLinkerObject):
%(c_extract_code)s %(c_extract_code)s
} }
""" % dict( """ % dict(
name=name, name=name,
c_init_code=self.c_init(name, sub), c_init_code=self.c_init(name, sub),
c_extract_code=self.c_extract(name, sub, check_input)) c_extract_code=self.c_extract(name, sub, check_input))
def c_cleanup(self, name, sub): def c_cleanup(self, name, sub):
"""Return c code to clean up after `c_extract`. """Return c code to clean up after `c_extract`.
...@@ -184,11 +187,12 @@ class CLinkerType(CLinkerObject): ...@@ -184,11 +187,12 @@ class CLinkerType(CLinkerObject):
def c_sync(self, name, sub): def c_sync(self, name, sub):
"""Required: Return c code to pack C types back into a PyObject. """Required: Return c code to pack C types back into a PyObject.
The code returned from this function must be templated using "%(name)s", The code returned from this function must be templated using
representing the name that the caller wants to call this Variable. The "%(name)s", representing the name that the caller wants to
returned code may set "py_%(name)s" to a PyObject* and that PyObject* call this Variable. The returned code may set "py_%(name)s"
will be accessible from Python via variable.data. Do not forget to adjust to a PyObject* and that PyObject* will be accessible from
reference counts if "py_%(name)s" is changed from its original value. Python via variable.data. Do not forget to adjust reference
counts if "py_%(name)s" is changed from its original value.
:Parameters: :Parameters:
- `name`: WRITEME - `name`: WRITEME
...@@ -205,10 +209,11 @@ class CLinkerType(CLinkerObject): ...@@ -205,10 +209,11 @@ class CLinkerType(CLinkerObject):
def c_code_cache_version(self): def c_code_cache_version(self):
"""Return a tuple of integers indicating the version of this Type. """Return a tuple of integers indicating the version of this Type.
An empty tuple indicates an 'unversioned' Type that will not be cached between processes. An empty tuple indicates an 'unversioned' Type that will not
be cached between processes.
The cache mechanism may erase cached modules that have been superceded by newer The cache mechanism may erase cached modules that have been
versions. See `ModuleCache` for details. superceded by newer versions. See `ModuleCache` for details.
""" """
return () return ()
...@@ -221,19 +226,21 @@ class PureType(object): ...@@ -221,19 +226,21 @@ class PureType(object):
- creating `Variable` instances (conventionally, `__call__` does this), and - creating `Variable` instances (conventionally, `__call__` does this), and
- filtering a value assigned to a `Variable` so that the value conforms to restrictions - filtering a value assigned to a `Variable` so that the value
imposed by the type (also known as casting, this is done by `filter`), conforms to restrictions imposed by the type (also known as
casting, this is done by `filter`),
""" """
# the type that will be created by call to make_variable.
Variable = graph.Variable
Variable = graph.Variable # the type that will be created by call to make_variable. # the type that will be created by call to make_constant
Constant = graph.Constant # the type that will be created by call to make_constant Constant = graph.Constant
def filter(self, data, strict=False, allow_downcast=None): def filter(self, data, strict=False, allow_downcast=None):
"""Required: Return data or an appropriately wrapped/converted data. """Required: Return data or an appropriately wrapped/converted data.
Subclass implementation should raise a TypeError exception if the data is not of an Subclass implementation should raise a TypeError exception if
acceptable type. the data is not of an acceptable type.
If strict is True, the data returned must be the same as the If strict is True, the data returned must be the same as the
data passed as an argument. If it is False, and allow_downcast data passed as an argument. If it is False, and allow_downcast
...@@ -272,18 +279,19 @@ class PureType(object): ...@@ -272,18 +279,19 @@ class PureType(object):
if other.type != self: if other.type != self:
raise TypeError( raise TypeError(
'Cannot convert Type %(othertype)s ' 'Cannot convert Type %(othertype)s '
'(of Variable %(other)s) into Type %(self)s. ' '(of Variable %(other)s) into Type %(self)s. '
'You can try to manually convert %(other)s into a %(self)s.' 'You can try to manually convert %(other)s into a %(self)s.'
% dict( % dict(
othertype=other.type, othertype=other.type,
other=other, other=other,
self=self) self=self)
) )
return other return other
def is_valid_value(self, a): def is_valid_value(self, a):
"""Required: Return True for any python object `a` that would be a legal value for a Variable of this Type""" """Required: Return True for any python object `a` that would be a
legal value for a Variable of this Type"""
try: try:
self.filter(a, strict=True) self.filter(a, strict=True)
return True return True
...@@ -291,7 +299,8 @@ class PureType(object): ...@@ -291,7 +299,8 @@ class PureType(object):
return False return False
def value_validity_msg(self, a): def value_validity_msg(self, a):
"""Optional: return a message explaining the output of is_valid_value""" """Optional: return a message explaining the output of
is_valid_value"""
return "none" return "none"
def make_variable(self, name=None): def make_variable(self, name=None):
...@@ -371,7 +380,8 @@ class Type(object2, PureType, CLinkerType): ...@@ -371,7 +380,8 @@ class Type(object2, PureType, CLinkerType):
But you are encouraged to write your own, as described in WRITEME. But you are encouraged to write your own, as described in WRITEME.
The following following code illustrates the use of a Type instance, here tensor.fvector: The following following code illustrates the use of a Type
instance, here tensor.fvector:
.. code-block:: python .. code-block:: python
...@@ -381,17 +391,21 @@ class Type(object2, PureType, CLinkerType): ...@@ -381,17 +391,21 @@ class Type(object2, PureType, CLinkerType):
# Create a second Variable with the same Type instance # Create a second Variable with the same Type instance
c = tensor.fvector() c = tensor.fvector()
Whenever you create a symbolic variable in theano (technically, `Variable`) it will contain a Whenever you create a symbolic variable in theano (technically,
reference to a Type instance. That reference is typically constant during the lifetime of `Variable`) it will contain a reference to a Type instance. That
the Variable. Many variables can refer to a single Type instance, as do b and c above. The reference is typically constant during the lifetime of the
Type instance defines the kind of value which might end up in that variable when executing Variable. Many variables can refer to a single Type instance, as
a `Function`. In this sense, theano is like a strongly-typed language because the types do b and c above. The Type instance defines the kind of value
are included in the graph before the values. In our example above, b is a Variable which is which might end up in that variable when executing a `Function`.
guaranteed to correspond to a numpy.ndarray of rank 1 when we try to do some computations In this sense, theano is like a strongly-typed language because
the types are included in the graph before the values. In our
example above, b is a Variable which is guaranteed to correspond
to a numpy.ndarray of rank 1 when we try to do some computations
with it. with it.
Many `Op` instances will raise an exception if they are applied to inputs with incorrect Many `Op` instances will raise an exception if they are applied to
types. Type references are also useful to do type-checking in pattern-based optimizations. inputs with incorrect types. Type references are also useful to
do type-checking in pattern-based optimizations.
""" """
def convert_variable(self, var): def convert_variable(self, var):
...@@ -451,8 +465,8 @@ class Generic(SingletonType): ...@@ -451,8 +465,8 @@ class Generic(SingletonType):
""" """
Represents a generic Python object. Represents a generic Python object.
This class implements the `PureType` and `CLinkerType` interfaces for generic PyObject This class implements the `PureType` and `CLinkerType` interfaces
instances. for generic PyObject instances.
EXAMPLE of what this means, or when you would use this type. EXAMPLE of what this means, or when you would use this type.
......
...@@ -270,7 +270,6 @@ whitelist_flake8 = [ ...@@ -270,7 +270,6 @@ whitelist_flake8 = [
"gof/destroyhandler.py", "gof/destroyhandler.py",
"gof/unify.py", "gof/unify.py",
"gof/graph.py", "gof/graph.py",
"gof/type.py",
"gof/__init__.py", "gof/__init__.py",
"gof/cc.py", "gof/cc.py",
"gof/opt.py", "gof/opt.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论