Created a method decorator to change temporarily the value of an attribute to the instance

remotes/origin/enhancement/email-actions
David Barragán Merino 2013-11-17 16:09:06 +01:00
parent 618b81eb2b
commit e2d1b8ae0d
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from functools import wraps
def change_instance_attr(name, new_value):
"""
Change the attribute value temporarily for a new one. If it raise an AttributeError (if the
instance hasm't the attribute) the attribute will not be changed.
"""
def change_instance_attr(fn):
@wraps(fn)
def wrapper(instance, *args, **kwargs):
try:
old_value = instance.__getattribute__(name)
changed = True
except AttributeError:
changed = False
if changed:
instance.__setattr__(name, new_value)
ret = fn(instance, *args, **kwargs)
if changed:
instance.__setattr__(name, old_value)
return ret
return wrapper
return change_instance_attr