# Path: infi/pyutils/lazy.py
# def cached_function(func):
#     """Decorator that caches a function's return value each time it is called.
#     If called later with the same arguments, the cached value is returned, and
#     not re-evaluated.
#     """
#     @wraps(func)
#     def callee(*args, **kwargs):
#         key = _get_function_cache_entry(args, kwargs)
#         try:
#             value = func._cache[key]
#         except (KeyError, AttributeError):
#             value = func(*args, **kwargs)
#             if not hasattr(func, '_cache'):
#                 setattr(func, '_cache', {})
#             func._cache[key] = value
#         return value
# 
#     callee._cache = func._cache = dict()
#     callee.__cached_method__ = True
#     return callee


from .lazy import cached_function
    def get_header(self, name):
        ...
class UserAuth(object):
    def is_authenticated(self):
        auth_header = self.get_header("Authorization")
        ...

Now, to add the mixin to the object we'll do::

request = HttpRequest(...)

# Find out that there's a need for authentication
...

install_mixin(request, UserAuth)
"""

__all__ = [ "install_mixin", "install_mixin_if" ]

def install_mixin_if(obj, mixin, condition):
    """
    Same as install_mixin, but installs the mixin only if *condition* evaluates to truth.
    """
    if not condition:
        return
    install_mixin(obj, mixin)

def install_mixin(obj, mixin):
    obj.__class__ = _replace_class(type(obj), mixin)


# Path: infi/pyutils/lazy.py
# def cached_function(func):
#     """Decorator that caches a function's return value each time it is called.
#     If called later with the same arguments, the cached value is returned, and
#     not re-evaluated.
#     """
#     @wraps(func)
#     def callee(*args, **kwargs):
#         key = _get_function_cache_entry(args, kwargs)
#         try:
#             value = func._cache[key]
#         except (KeyError, AttributeError):
#             value = func(*args, **kwargs)
#             if not hasattr(func, '_cache'):
#                 setattr(func, '_cache', {})
#             func._cache[key] = value
#         return value
# 
#     callee._cache = func._cache = dict()
#     callee.__cached_method__ = True
#     return callee


# Path: infi/pyutils/mixin.py
from .lazy import cached_function

    def get_header(self, name):
        ...
class UserAuth(object):
    def is_authenticated(self):
        auth_header = self.get_header("Authorization")
        ...

Now, to add the mixin to the object we'll do::

request = HttpRequest(...)

# Find out that there's a need for authentication
...

install_mixin(request, UserAuth)
"""

__all__ = [ "install_mixin", "install_mixin_if" ]

def install_mixin_if(obj, mixin, condition):
    """
    Same as install_mixin, but installs the mixin only if *condition* evaluates to truth.
    """
    if not condition:
        return
    install_mixin(obj, mixin)

def install_mixin(obj, mixin):
    obj.__class__ = _replace_class(type(obj), mixin)


@cached_function