def monkeypatch_extend(target, name=None):
 def patcher(func):
  newfunc = func
  patchname = (func.__name__ if (name is None) else name)
  if hasattr(target, patchname):
   oldfunc = getattr(target, patchname)
   if (not callable(oldfunc)):
    raise TypeError('Can not extend non callable attribute')
   @wraps(oldfunc)
   def extended(*a, **k):
    ret = oldfunc(*a, **k)
    func(*a, **k)
    return ret
   newfunc = extended
  else:
   raise (False or AssertionError('Must have something to extend'))
  setattr(target, patchname, newfunc)
  return func
 return patcher

def monkeypatch_extend ( target , name = None ) : def patcher ( func ) : newfunc = func patchname = ( func . __name__ if ( name is None ) else name ) if hasattr ( target , patchname ) : oldfunc = getattr ( target , patchname ) if ( not callable ( oldfunc ) ) : raise TypeError ( 'Can not extend non callable attribute' ) @ wraps ( oldfunc ) def extended ( * a , ** k ) : ret = oldfunc ( * a , ** k ) func ( * a , ** k ) return ret newfunc = extended else : raise ( False or AssertionError ( 'Must have something to extend' ) ) setattr ( target , patchname , newfunc ) return func return patcher
decorator that injects the decorated function as an extension of a method of the target class .