decorators for creating paramaterized decorators and easy monkeypatching

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from decorator import decorator
from functools import partial

def decoflex(decfac, *args, **kw):
	def wrapped(df, param=None, *args, **kw):
		return partial(df, param, *args, **kw)
	return partial(wrapped, decfac, *args, **kw)

@decoflex
def monkeypatch(cls, f, *args, **kwargs):
	if not hasattr(f, "__name__") and kwargs.get('fname', False):
		setattr(cls, fname, f)
		del kwargs['fname']
	else:
		setattr(cls, f.__name__, f)
	return f

@decorator
def trace(f, *args, **kwargs):
	print "%s :: %s, %s" % (f.__name__, args, kwargs)
	return f(*args, **kwargs)

More like this

  1. Other approach of making middleware (by decorators) by diverman 3 years, 1 month ago
  2. Django Dictionary Model by Morgul 2 years, 10 months ago
  3. safe(r) monkeypatching scheme for django testing by showell 4 years, 5 months ago
  4. DRY custom ModelAdmin.list_display methods with a decorator by exogen 5 years, 7 months ago
  5. EasyFeed class by limodou 7 years, 1 month ago

Comments

(Forgotten your password?)