Skip to content

Commit 328b2d4

Browse files
authored
Merge pull request #408 from LeiYangGH/master
simplify patterns/behavioral/memento, changing Transactional from des…
2 parents fa56fde + 76a5d21 commit 328b2d4

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

patterns/behavioral/memento.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,20 @@ def rollback(self):
4141
a_state()
4242

4343

44-
class Transactional:
44+
def Transactional(method):
4545
"""Adds transactional semantics to methods. Methods decorated with
46+
@Transactional will roll back to entry-state upon exceptions.
4647
47-
@Transactional will rollback to entry-state upon exceptions.
48+
:param method: The function to be decorated.
4849
"""
49-
50-
def __init__(self, method):
51-
self.method = method
52-
53-
def __get__(self, obj, T):
54-
"""
55-
A decorator that makes a function transactional.
56-
57-
:param method: The function to be decorated.
58-
"""
59-
60-
def transaction(*args, **kwargs):
61-
state = memento(obj)
62-
try:
63-
return self.method(obj, *args, **kwargs)
64-
except Exception as e:
65-
state()
66-
raise e
67-
68-
return transaction
69-
50+
def transaction(obj, *args, **kwargs):
51+
state = memento(obj)
52+
try:
53+
return method(obj, *args, **kwargs)
54+
except Exception as e:
55+
state()
56+
raise e
57+
return transaction
7058

7159
class NumObj:
7260
def __init__(self, value):

0 commit comments

Comments
 (0)