-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Added a decorator to connect to the heat pump #131
Conversation
Shouldn't it be possible to use your |
Unfortunately, a "real" decorator cannot use |
I tried a little bit and it seems to be possible with a "real" decorator: def decorator(func):
def _decorator(self, *args, **kwargs):
self.num_calls += 1
return func(self, *args, **kwargs)
return _decorator
class TestSample:
def __init__(self):
self.num_calls = 0
@decorator
def dummy(self, foo):
print(foo)
t = TestSample()
print(f"t.num_calls: {t.num_calls}")
t.dummy(2)
print(f"t.num_calls: {t.num_calls}") There are also no complaints by pylint (only concerning missing docstrings and so on). |
Have you tried accessing private fields like I previously had the decorator inside the class. If you place it outside - like in your example - you at least bypass the fact that pylint requires a |
Yes, the following is """Random module"""
def decorator(func):
"""Random decorator"""
def _decorator(self, *args, **kwargs):
self._num_calls += 1
return func(self, *args, **kwargs)
return _decorator
class TestSample:
"""Random class"""
def __init__(self):
self._num_calls = 0
def another_method(self):
"""Random member fctn"""
return self._num_calls
@decorator
def dummy(self, foobar):
"""Random member fctn"""
print(foobar)
t = TestSample()
print(t.another_method())
t.dummy(2)
print(t.another_method()) |
@Guzz-T Could you elaborate on what the vision here is? How do you envision to use this in the future? |
Coverage Report
|
3559aab
to
860f861
Compare
@gerw: I followed your suggestions, but now we get several |
This is strange. Pylist does not complain about something like |
I reverted the changes because it would just be a different style of the decorator |
d4664f6
to
bad04b5
Compare
Since we are not saving the decorated function, it seems not necessary to implement this as a decorator. What about something like def _with_lock_and_connect(self, func, *args, **kwargs):
with self._lock:
is_none = self._socket is None
if is_none:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if is_none or is_socket_closed(self._socket):
self._socket.connect((self._host, self._port))
LOGGER.info(
"Connected to Luxtronik heat pump %s:%s", self._host, self._port
)
return func(*args, **kwargs)
def read(self):
return self._with_lock_and_connect(self._read) ? |
There isn't just one way of doing it. My version works like a "@"-decorator. |
bd14ce5
to
2cbd44a
Compare
2cbd44a
to
2a9eaab
Compare
Is there still interest in these changes? |
Using a decorator we can simply re-use the code to connect to the heat pump. This allows us to offers several read / write functions like only read the paramters.
Fixes #130