提交 c54ef356 authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Refactor properties and docstrings in Apply

上级 7e351b93
...@@ -63,20 +63,8 @@ class Node(MetaObject): ...@@ -63,20 +63,8 @@ class Node(MetaObject):
class Apply(Node): class Apply(Node):
"""A `Node` representing the application of an operation to inputs. """A `Node` representing the application of an operation to inputs.
An `Apply` instance serves as a simple structure with three important
attributes:
- :literal:`inputs` : a list of `Variable` nodes that represent the
arguments of the expression,
- :literal:`outputs` : a list of `Variable` nodes that represent the
computed outputs of the expression, and
- :literal:`op` : an `Op` instance that determines the nature of the
expression being applied.
Basically, an `Apply` instance is an object that represents the Basically, an `Apply` instance is an object that represents the
Python statement `outputs = op(*inputs)`. Python statement ``outputs = op(*inputs)``.
This class is typically instantiated by a `Op.make_node` method, which This class is typically instantiated by a `Op.make_node` method, which
is called by `Op.__call__`. is called by `Op.__call__`.
...@@ -88,12 +76,6 @@ class Apply(Node): ...@@ -88,12 +76,6 @@ class Apply(Node):
A `Linker` uses the `Apply` instance's `op` field to compute numeric values A `Linker` uses the `Apply` instance's `op` field to compute numeric values
for the output variables. for the output variables.
Parameters
----------
op : A Op instance
inputs : list of Variable instances
outputs : list of Variable instances
Notes Notes
----- -----
The `Variable.owner` field of each `Apply.outputs` element is set to `self` The `Variable.owner` field of each `Apply.outputs` element is set to `self`
...@@ -102,9 +84,26 @@ class Apply(Node): ...@@ -102,9 +84,26 @@ class Apply(Node):
If an output element has an owner that is neither `None` nor `self`, then a If an output element has an owner that is neither `None` nor `self`, then a
`ValueError` exception will be raised. `ValueError` exception will be raised.
Attributes
----------
op : Op
The operation that produces `outputs` given `inputs`.
inputs : List[Variable]
The arguments of the expression modeled by the `Apply` node.
outputs : List[Variable]
The outputs of the expression modeled by the `Apply` node.
""" """
def __init__(self, op, inputs, outputs): def __init__(self, op, inputs, outputs):
"""
Parameters
----------
op : Op
inputs : List[Variable]
outputs : List[Variable]
"""
self.op = op self.op = op
self.inputs = [] self.inputs = []
self.tag = Scratchpad() self.tag = Scratchpad()
...@@ -187,21 +186,12 @@ class Apply(Node): ...@@ -187,21 +186,12 @@ class Apply(Node):
raise ValueError(f"{self.op}.default_output is out of range.") raise ValueError(f"{self.op}.default_output is out of range.")
return self.outputs[do] return self.outputs[do]
out = property(default_output, doc="alias for self.default_output()")
"""
Alias for self.default_output().
"""
def __str__(self): def __str__(self):
return op_as_string(self.inputs, self) return op_as_string(self.inputs, self)
def __repr__(self): def __repr__(self):
return str(self) return str(self)
def __asapply__(self):
return self
def clone(self): def clone(self):
""" """
Duplicate this Apply instance with inputs = self.inputs. Duplicate this Apply instance with inputs = self.inputs.
...@@ -266,20 +256,24 @@ class Apply(Node): ...@@ -266,20 +256,24 @@ class Apply(Node):
def get_parents(self): def get_parents(self):
return list(self.inputs) return list(self.inputs)
# convenience properties @property
nin = property(lambda self: len(self.inputs), doc="same as len(self.inputs)") def out(self):
""" """An alias for `self.default_output`"""
Property: Number of inputs. return self.default_output()
""" @property
nout = property(lambda self: len(self.outputs), doc="same as len(self.outputs)") def nin(self):
""" """The number of inputs."""
Property: Number of outputs. return len(self.inputs)
""" @property
params_type = property( def nout(self):
lambda self: self.op.params_type, doc="type to use for the params" """The number of outputs."""
) return len(self.outputs)
@property
def params_type(self):
return self.op.params_type
class Variable(Node): class Variable(Node):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论