4

Here's an example method that's inside a Python Class:

def publish_aggregate_account_group_stats(self, account_group_token): message = { "type": "metrics-aggregate-account-group-stats", "accountGroupToken": account_group_token } try: self._get_writer().write(message) except: self._put_cache(message) 

There is a handful of methods in my class that all run the try/except, that I think could be DRYed up or cleaned up by simply creating a decorator that handles that for me. I'm just unsure how the decorator would look/work by accessing self.

0

1 Answer 1

1

something like this will work:

from contextlib import contextmanager class Test(object): def __init__(self): self.j = set() @contextmanager def handle_exc(self, msg): try: yield except: print('adding to internal structure:', msg) self.j.add(msg) def test(self): m = 'snth' with self.handle_exc(m): raise Exception('error') 

a decorator is tough to use here because you're creating values within the function itself, so the outside decorator will never know about them unless you find a way to propagate them up (via a certain exception or something.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.