All pastes #454680 Raw Edit

mixins

public text v1 · immutable
·published 2007-04-23 19:21 UTC
rendered paste body
class exampleMixin(someOtherMixin):
	
	""" example mixin """
	
	# the following are all mixin internal instructions###########################################
	
	# specify this explicitly if someOtherMixin was abstract.
	_mixin_abstractMixin = False		
	
	# if __called__() with no arguments, this value is necessary.  one is class, 'two' is a fully
	# qualified class name string (in case two is not safe to import directly, strings are allowed)
	_mixin_targetClasses = [one, "two"]	
	
	#
	_mixin_name	     = 'ringMixin'
	
	# the following are all class variables if mixed into a class, instance variables for instance#
	myvar	     = None
	myvar2       = None
	
	# now the methods follow#######################################################################
	@staticmethod
	def postmix(klass=None, instance=None):
		"""if this mixin is called on klass, when it is done postmix(klass=klass) is called"""
	
	def normalMethod(self, arg1, arg2):
		"""a normal method that is moved into whatever object we mix into """
	
	@chainiff('chain_target', when=lambda mix_target: hasattr(mix_target,'special'), how='before')
	def conditionalMethod(self, **kargs):
		""" on __call__(), this conditionalMethod is chained before the method named 
		    'chain_target' (which must already exist inside the mix_target) if and only if
		    mix_target has an attribute called 'special'.  if this method returns anything
		    besides None, the return value is expected to be the modified keyword-arguments
		    which should then passed on to the original chain_target """
		return kargs
	
	@plugiff(lambda mix_target: issubclass(mix_target,some_special_class), overwrite=True)
	def pluggedMethod(self):
		""" on __call__(), this pluggedMethod is plugged into mix_target if and only if
		    mix_target is a subclass of some_special_class.  since overwrite=True, if the name
		    mix_target.pluggedMethod already existed, it would be overwritten silently. """