"
/ "
False.
, .
If you do not implement anything else, it will be reused.
However, you cannot use a tuple because it is immutable. You need some kind of volatile collection. Dictionaries and class definitions come to mind.
Therefore, the recommended implementation
"which contains certain attributes as an argument to the context manager"
A simple class definition with two attributes is all you need. However, the transaction status is wealthy, and you need to save the state somewhere.
class Counters(dict):
SUCCEED= 0
FAIL= 1
def __init__( self ):
self[ self.SUCCEED ]= 0
self[ self.FAIL ]= 0
def increment( self, status ):
self[status] += 1
class Transaction(object):
def __init__( self, worker, counters ):
self.worker= worker
self.counters= counters
def __enter__( self ):
self.counters.status= None
def process( self, *args, **kw ):
status= self.worker.execute( *args, **kw )
self.counters.increment( status )
def __exit__( self ):
pass
counts= Counters()
for q in queryList:
with Transaction(execQuery,counts) as t:
t.process( q )
print counts
source
share