My tools of the trade for python programming.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

# $Id:,v 1.2 2011-08-31 18:26:18 wirawan Exp $
# module
# Created: 20110608
# Wirawan Purwanto
# Module space for advanced tools requiring low-level python hacks.
import sys
import new
""" module
Collection of low-level pythonic hacks
Instance method hacks
One possible reason of using these routines is to provide an
alterable calling point with different implementations--somewhat like
virtual methods, but something that can be changed dynamically
NOTE: The trick provided by class is
a better way to accomplish the same thing.
def make_unbound_instance_method(method):
"""Generates an unbound instance method from a possibly bound
instance method.
return new.instancemethod(method.im_func, None, method.im_class)
def make_unbound_method(method):
"""Generates an unbound instance method from a possibly bound
instance method, or even user-defined function.
This is useful for injecting external, completely unrelated function
as an instance procedure into a class, without having to subclass
the whole thing.
CAVEAT: This is a haram trick. Know what you are doing."""
return new.instancemethod(method.im_func, None, method.im_class)
except AttributeError:
# Assume this is a static method or user-defined external method
# injected into this class.
return method
def make_weakly_bound_method(method, instance):
"""Creates a bound instance method, where the instance is weakly
This trick is necessary if the bound method is to be attached
to that instance's attribute list (e.g. via instance.__dict__),
because otherwise a circular reference occurs:
bound_method -> instance -> bound_method (via __dict__)
CAVEAT: Know what you are doing! In general, this is a haram trick.
We circumvent the standard class type-safety mechanism in python here:
if the `method` actually belongs to a completely unrelated class,
this routine still accepts it and allow the function to be called.
# NOTE: This is an identical trick to;
# the latter (the OO interface) is better because
# it does not need function closure, and that the object data is
# easier to diagnose.
from weakref import ref
instance_ref = ref(instance)
instance_cls = instance.__class__
im_func, im_class = method.im_func, method.im_class
#im_method = new.instancemethod(im_func, None, instance_cls)
except AttributeError:
im_func = method
# Assume this is a function defined outside a class, which is then
# injected into this instance.
# The first argument must be the usual `self` argument.
return lambda *args, **kwargs: method(instance_ref(), *args, **kwargs)
return lambda *args, **kwargs: im_func(instance_ref(), *args, **kwargs)
def function_name(f):
"""Returns the given name of a function (or callable object)."""
# Regular function
return f.func_name
# Instance method
return "%s.%s" % (f.im_class, f.im_func.func_name)
# Callable class instance:
return f.__class__.__name__