Skip to content
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

Connet set attribute to signal (alternate) #38

Closed
wants to merge 4 commits into from

Conversation

Czaki
Copy link
Contributor

@Czaki Czaki commented Nov 7, 2021

This PR implements #36

@codecov
Copy link

codecov bot commented Nov 7, 2021

Codecov Report

Merging #38 (ddef76b) into main (82ebda3) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main       #38   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines          355       405   +50     
=========================================
+ Hits           355       405   +50     
Impacted Files Coverage Δ
psygnal/_signal.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 82ebda3...ddef76b. Read the comment docs.

Comment on lines 1036 to 1084
class PartialWeakrefCallback:
def __init__(self, slot_partial: PartialMethod):
self.obj, self.name = _get_proper_name(slot_partial.func)
self.args = slot_partial.args
self.kwargs = slot_partial.keywords

def alive(self) -> bool:
return self.obj() is not None

def __call__(self, *args: Any, **kwargs: Any) -> Any:
return getattr(self.obj(), self.name)(
*self.args, *args, **self.kwargs, **kwargs
)

def __eq__(self, other: Any) -> bool:
try:
return (
isinstance(other, PartialWeakrefCallback)
and self.name == other.name
and self.obj() == other.obj()
and self.args == other.args
and self.kwargs == other.kwargs
)
except: # noqa: E722
return False


class PropertyWeakrefCallback:
def __init__(self, obj: Union[weakref.ref, object], name: str):
if not isinstance(obj, weakref.ref):
obj = weakref.ref(obj)
self.obj = obj
self.name = name

def alive(self) -> bool:
return self.obj() is not None

def __call__(self, *args: Any) -> None:
if len(args) == 1:
setattr(self.obj(), self.name, args[0])
else:
setattr(self.obj(), self.name, args)

def __eq__(self, other: Any) -> bool:
return (
isinstance(other, PropertyWeakrefCallback)
and self.name == other.name
and self.obj() == other.obj()
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decide to switch to class to allow compare two objects

@tlambert03 tlambert03 changed the title Connet set atribute to signal Connet set attribute to signal (alternate) Nov 7, 2021
@tlambert03 tlambert03 marked this pull request as draft November 7, 2021 20:42
@tlambert03
Copy link
Member

merged #39, so converted this to draft. Some nice class patterns in here that we should remember. perhaps we can figure out the performance stuff later and use it then

@tlambert03 tlambert03 closed this Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants