Skip to content

Commit

Permalink
improved context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Pullusb committed Jan 20, 2025
1 parent cd2d022 commit b5bc797
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions snippets/dev/attr_set_context_manager_store_restore.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
## context manager to store restore

class attr_set():
'''Receive a list of tuple [(data_path, "attribute" [, wanted value)] ]
entering with-statement : Store existing values, assign wanted value (if any)
exiting with-statement: Restore values to their old values
'''Store-set-restore attributes for context manager.
Receive a list of tuple [(data_path, "attribute" [, new value)] ]
Entering with-statement : Store current attributes values, assign new value (if provided)
Exiting with-statement: Restore attributes old values in reverse order
'''

def __init__(self, attrib_list):
Expand All @@ -19,7 +20,7 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, exc_traceback):
for prop, attr, old_val in self.store:
for prop, attr, old_val in reversed(self.store):
setattr(prop, attr, old_val)


Expand Down

0 comments on commit b5bc797

Please sign in to comment.