-
Notifications
You must be signed in to change notification settings - Fork 1
Allow multiple delegations to stack, as well as optional prefixes #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asross
wants to merge
1
commit into
dscottboggs:master
Choose a base branch
from
asross:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,18 +1,21 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| def bind(instance, func, name): | ||
| """Turn a function into a bound method on instance""" | ||
| setattr(instance, name, func.__get__(instance, instance.__class__)) | ||
|
|
||
| def delegate(*args, **named_args): | ||
| dest = named_args.get('to') | ||
| if dest is None: | ||
| _dest = named_args.get('to') | ||
| if _dest is None: | ||
| raise ValueError( | ||
| "the named argument 'to' is required on the delegate function") | ||
|
|
||
| _prefix = named_args.get('prefix', '') | ||
|
|
||
| print(_dest, _prefix) | ||
|
|
||
| def wraps(cls, *wrapped_args, **wrapped_opts): | ||
| """Wrap the target class up in something that modifies.""" | ||
| class Wrapped(cls): | ||
|
|
||
| _delegates = [(a, _dest, _prefix) for a in args] + getattr(cls, '_delegates', []) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clever |
||
|
|
||
| def __getattr__(self, name): | ||
| """ | ||
| Return the selected name from the destination if the name is one | ||
|
|
@@ -21,27 +24,34 @@ def __getattr__(self, name): | |
| error when `name` is not one of the selected args to be | ||
| delegated. | ||
| """ | ||
| if name in args: return getattr(self.__dict__[dest], name) | ||
| for attr, dest, prefix in self._delegates: | ||
| if name == prefix + attr: | ||
| return getattr(self.__dict__[dest], name[len(prefix):]) | ||
|
|
||
| raise AttributeError(f"'{self.__class__.__name__}' has no attribute '{name}'") | ||
|
|
||
| def __setattr__(self, name, value): | ||
| """ | ||
| If this name is one of those selected, set it on the destination | ||
| property. Otherwise, set it on self. | ||
| """ | ||
| if name in args: setattr(getattr(self, dest), name, value) | ||
| else: self.__dict__[name] = value | ||
| for attr, dest, prefix in self._delegates: | ||
| if name == prefix + attr: | ||
| setattr(getattr(self, dest), name[len(prefix):], value) | ||
| return | ||
| self.__dict__[name] = value | ||
|
|
||
| def __delattr__(self, name): | ||
| """Delete name from `dest` or `self`""" | ||
| if name in args: delattr(getattr(self, dest), name) | ||
| else: del self.__dict__[name] | ||
| for attr, dest, prefix in self._delegates: | ||
| if name == prefix + attr: | ||
| delattr(getattr(self, dest), name[len(prefix):]) | ||
| return | ||
|
|
||
| def __init__(self, *wrapped_args, **wrapped_opts): | ||
| super().__init__(*wrapped_args, **wrapped_opts) | ||
|
Comment on lines
-40
to
-41
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this not necessary? |
||
| del self.__dict__[name] | ||
|
|
||
| Wrapped.__doc__ = cls.__doc__ or \ | ||
| f"{cls.__class__} wrapped to delegate {args} to its {dest} property" | ||
| f"{cls.__class__} wrapped to delegate {args} to its {_dest} property" | ||
| Wrapped.__repr__ = cls.__repr__ | ||
| Wrapped.__str__ = cls.__str__ | ||
| Wrapped.__name__ = cls.__name__ | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
printcall should be removed