django_auxilium.utils.functools.lazy module

class django_auxilium.utils.functools.lazy.LazyDecorator(types)[source]

Bases: django_auxilium.utils.functools.decorators.Decorator

Lazy evaluation decorator.

Parameters:types (tuple, list) – Iterable of possible output data-types of the output of the input function

Examples

>>> lazy = LazyDecorator

>>> @lazy([six.string_types, int])
... def a(bar):
...   print('invoking a with {0}'.format(bar))
...   if isinstance(bar, int):
...       return bar + 5
...   else:
...       return bar + 'foo'

>>> l = a('bar')
>>> isinstance(l, six.string_types)
invoking a with bar
True
>>> print(l)
barfoo

>>> l = a(5)
>>> isinstance(l, int)
invoking a with 5
True
>>> print(l)
10
get_lazy_wrapper_class()[source]

Construct lazy wrapper class for the given possible types

Returns:Lazy wrapper class specifically made for the possible types
Return type:type
get_possible_types(possible_types)[source]

Get possible types as a list

Parameters:possible_types (list, any) – Either a list of possible types or a single possible type
Returns:List of possible types. If the input possible_types was given as a single possible types, a list with a single item is returned.
Return type:list
get_wrapped_object()[source]

Get the wrapped callable which instead of computing values right away returns a lazy wrapper.

class django_auxilium.utils.functools.lazy.LazyWrapper(f, args, kwargs)[source]

Bases: django.utils.functional.Promise

Wrapper class for lazy objects as returned by the LazyDecorator

The job of this wrapper is to not execute the inner function to get the result until absolutely necessary. For example simply storing a variable will not trigger inner function to be executed until something is done with the variable such as printing it, etc.

Parameters:
  • f (def) – Function which this lazy object wraps
  • args (tuple) – Tuple of arguments which were passed to the function for execution
  • kwargs (dict) – Dict of keyword arguments which were passed to the function for execution
class django_auxilium.utils.functools.lazy.LazyWrapperMeta[source]

Bases: type

Metaclass for LazyWrapper

The reason we need metaclass is because we need to customize some magic methods for LazyWrapper for particular types while creating class which is what metaclasses are for!

This metaclass expects all subclasses of LazyWrapper to supply possible_types attribute which this metaclass will use to customize the created class.

django_auxilium.utils.functools.lazy.SPECIAL_METHODS = ['__abs__', '__add__', '__and__', '__call__', '__cmp__', '__coerce__', '__contains__', '__delitem__', '__delslice__', '__div__', '__divmod__', '__eq__', '__float__', '__floordiv__', '__ge__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__hex__', '__iadd__', '__iand__', '__idiv__', '__idivmod__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__long__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rfloorfiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setitem__', '__setslice__', '__str__', '__sub__', '__truediv__', '__unicode__', '__xor__', 'next']

The list of special methods is compiled from http://code.activestate.com/recipes/496741-object-proxying/

django_auxilium.utils.functools.lazy.flazy(f, possible_types)[source]

Wrapper function for the LazyDecorator

It has the same API as Django’s lazy function so the same code can be reused with this decorator if necessary.

Examples

>>> def f():
...     print('inside f')
...     return 5
>>> g = flazy(f, int)
>>> h = g()
>>> print(str(h))
inside f
5
django_auxilium.utils.functools.lazy.lazy

alias of django_auxilium.utils.functools.lazy.LazyDecorator