-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
006cb85
commit 7bc5798
Showing
2 changed files
with
25 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,38 @@ | ||
def model_attribute(obj, attribute_name): | ||
"""Extensions module for decorators and subclasses. | ||
Example use for decorators: | ||
>>> from wsimod.extensions import extensions as extend | ||
>>> @extend.node_attribute(obj=my_fwtw, attribute_name="pull_distributed") | ||
>>> def new_distributed(pull_distributed, vqip): | ||
>>> return pull_distributed(vqip, tag="FWTW") | ||
""" | ||
|
||
|
||
def node_attribute(obj, attribute_name: str): | ||
""" | ||
Decorator to extend or modify a model attribute. | ||
Decorator to extend or modify a node attribute. | ||
Args: | ||
obj: The model object whose attribute should be modified. | ||
obj: The node object whose attribute should be modified. | ||
attribute_name (str): The name of the attribute to modify. | ||
Returns: | ||
A decorator function that takes the extension function as an argument. | ||
""" | ||
def decorator(func): | ||
|
||
def decorator(func: callable): | ||
""" | ||
Decorator function that applies the extension function to the model attribute. | ||
Decorator function that applies the extension function to the node attribute. | ||
Args: | ||
func: The extension function that modifies the model attribute. | ||
func (callable): The extension function that modifies the node attribute. | ||
""" | ||
attribute = getattr(obj, attribute_name) | ||
|
||
def wrapped_attribute(*args, **kwargs): | ||
return func(attribute, *args, **kwargs) | ||
|
||
setattr(obj, attribute_name, wrapped_attribute) | ||
return wrapped_attribute | ||
|
||
return decorator | ||
return decorator |