Package _Framework :: Module Util
[hide private]
[frames] | no frames]

Module Util

source code

Various utilities.

Classes [hide private]
  memoize
Decorator to use automatic memoization on a given function, such that results are chached and, if called a second time, the function will return the cached value.
  lazy_attribute
Decorator that will turn a method in a lazy attribute.
  BooleanContext
This class represents an boolean variable with RAII setting within a scope.
  NamedTupleMeta
  NamedTuple
Immutable object that acts like a dictionary whose members can also be set via attribute access.
  Bindable
Utility base class for general bindable function objects.
  CallCounter
Function object that counts the number of times it is called.
Functions [hide private]
 
clamp(val, minv, maxv) source code
 
linear(minv, maxv, val) source code
 
nop(*a, **k) source code
 
negate(value) source code
 
const(value) source code
 
in_range(value, lower_bound, upper_open_bound) source code
 
sign(value) source code
 
monkeypatch(target, name=None, override=False, doc=None)
Decorator that injects the decorated function into the 'target' class.
source code
 
monkeypatch_extend(target, name=None)
Decorator that injects the decorated function as an extension of a method of the 'target' class.
source code
 
instance_decorator(decorator)
Meta-decorator to define decorators that decorate a method in a concrete instance.
source code
 
forward_property(member)
Property that forwards access to a nested object.
source code
 
remove_if(predicate, lst)
Returns a new list with elements of the iterable 'lst' excepting those satisfying 'predicate'.
source code
 
flatten(list)
Flattens a list of lists into a new list.
source code
 
group(lst, n)
Returns a list of lists with elements from 'lst' grouped in blocks of 'n' elements.
source code
 
find_if(predicate, seq)
Returns the first element in sequence 'seq' satisfying 'predicate' or 'None' if no such element exists.
source code
 
index_if(predicate, seq)
Returns the index of the first element in sequence 'seq' satisfying predicate.
source code
 
union(a, b)
Returns a new dictionary with all the entries in dictionaries 'a' and 'b'.
source code
 
product(iter_a, iter_b)
Generator that generates all possible tuples combining elements from sequence 'iter_a' and 'iter_b'.
source code
 
next(iter)
Equivalent to iter.next()
source code
 
is_iterable(value)
Returns True if 'value' is iterable and False otherwise.
source code
 
recursive_map(fn, element, sequence_type=None)
Maps a tree-like data structure built by composing sequences of type iterable_type.
source code
 
first(seq) source code
 
second(seq) source code
 
third(seq) source code
 
compose(*funcs)
Returns the composition of all passed functions, similar to the mathematical dot.
source code
 
_partial(fn, *a, **k)
See functools.partial
source code
 
is_contextmanager(value) source code
 
infinite_context_manager(generator)
contextlib.contextmanager have the consumes the generator, so most of the time they can only be used one.
source code
 
trace_value(value, msg='Value: ')
Prints value and returns value.
source code
 
count_calls(fn=<function nop at 0x101bb55f0>) source code
Variables [hide private]
  mixin = <_Framework.Util.memoize object at 0x101bb8050>
  __package__ = '_Framework'
Function Details [hide private]

monkeypatch(target, name=None, override=False, doc=None)

source code 

Decorator that injects the decorated function into the 'target' class. If no name is given the decorated function name will be used for the injected method name. If the class already has a method with a given name it raises an error unless 'override' is set to True.

Example:

   class MyClass(object):
       pass

   @monkeypatch(MyClass)
   def patched_method(self):
       print "Lalala"

   MyClass().patched_method()

Output:

   Lalala

monkeypatch_extend(target, name=None)

source code 

Decorator that injects the decorated function as an extension of a method of the 'target' class. If no 'name' is passed, the decorated function name will be the name of the method.

Example:

  class MyClass(object):
      def some_method(self):
          print "Original"

   @monkeypatch_extend(MyClass)
   def some_method(self):
       print "Patch"

   MyClass().some_method()

Output:

   Original
   Patch

Known issues: if you are extending a method of class Deriv, when the method is only defined in its super-class Base (i.e. not overriden by Deriv but is inherited from Base), can break the ability of the method to properly cooperate (i.e. propagate calls to super in a diamond-shaped hierarchy [1]). If monkeypatch_extend in a metaclass, this can be worked around by injecting a cooperative definition of the method in Deriv's dictionary. An example of this can be seen in SubjectSlot.SubjectMeta

[1] A definition of cooperative method http://sinusoid.es/jpblib/coop.html

instance_decorator(decorator)

source code 

Meta-decorator to define decorators that decorate a method in a concrete instance. The decorator method will be passed the object instance as first argument and the unbound decorated method as second argument. The decorator method will be called lazily the first time the method is accessed.

For an example see @signal_slot in SubjectSlot module.

forward_property(member)

source code 

Property that forwards access to a nested object. You can use it as a decorator, where the function will be used only to extract the name of the property. It is useful when exposing some property of a subobject...

Example:

   class NestedClass(object):
       parameter = 0

   class SomeClass(object):
       def __init__(self, *a, **k):
           super(SomeClass, self).__init__(*a, **k)
           self._nested_object = NestedClass()

       @forward_property('_nested_object')
       def parameter(): pass

   print SomeClass().parameter

Output:

   0

flatten(list)

source code 

Flattens a list of lists into a new list. It does not do that recursively, only one level.

index_if(predicate, seq)

source code 

Returns the index of the first element in sequence 'seq' satisfying predicate. If no such element exists returns the length of the sequence.

union(a, b)

source code 

Returns a new dictionary with all the entries in dictionaries 'a' and 'b'. In case of conflict the entry from 'b' is taken.

recursive_map(fn, element, sequence_type=None)

source code 

Maps a tree-like data structure built by composing sequences of type iterable_type. if no iterable_type is given, it is assumed to be the type of the root element.

Example:

   print recurse_map(lambda t: t + (0,),
                     [[(0,), (1,)], [(3,), (4,)]])

Output:

   [[(0,0), (1,0)], [(3,0), (4,0)]]

compose(*funcs)

source code 

Returns the composition of all passed functions, similar to the mathematical dot.

Example:

   f = lambda x: x + 2
   g = lambda x: x * 2
   h = compose(f, g)
   print h(3)

Output:

  8 # (3 * 2) + 2

infinite_context_manager(generator)

source code 

contextlib.contextmanager have the consumes the generator, so most of the time they can only be used one. This variant will always re-instantiate the generator, such that the context manager can be reused.

trace_value(value, msg='Value: ')

source code 

Prints value and returns value. Useful when debugging the results of sub-expressions.