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

simplify patterns/behavioral/memento, changing Transactional from des… #408

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 11 additions & 23 deletions patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,20 @@ def rollback(self):
a_state()


class Transactional:
def Transactional(method):
"""Adds transactional semantics to methods. Methods decorated with
@Transactional will roll back to entry-state upon exceptions.

@Transactional will rollback to entry-state upon exceptions.
:param method: The function to be decorated.
"""

def __init__(self, method):
self.method = method

def __get__(self, obj, T):
"""
A decorator that makes a function transactional.

:param method: The function to be decorated.
"""

def transaction(*args, **kwargs):
state = memento(obj)
try:
return self.method(obj, *args, **kwargs)
except Exception as e:
state()
raise e

return transaction

def transaction(obj, *args, **kwargs):
state = memento(obj)
try:
return method(obj, *args, **kwargs)
except Exception as e:
state()
raise e
return transaction

class NumObj:
def __init__(self, value):
Expand Down