decorators

Decorator and helper routines.

AUTHORS:

  • THOMAS MCTAVISH (2010-12-01): initial version, 0.1
neuronpy.util.decorators.set_overrides_and_defaults(obj, defaults, kwargs)[source]

For a class that has 'set_<var>()' accessor routines for its variables, this function should be called in __init__ to set any kwarg overrides and then set remaining default parameters by calling the appropriate 'set_<var>()' function. To use, simply place default values in a dict and pass it wih the kwargs.

EXAMPLE:

The following example shows general use.

from neuronpy.util.decorators import set_overrides_and_defaults

class MyClass(object):
    def __init__(self, **kwargs):
        defaults = {'var1':None,                         'var2':'spikeplot.png'}
        set_overrides_and_defaults(self, defaults, kwargs)
            
    def set_var1(self, val):
        # Do some tests to make sure val is appropriate, and then set
        self._var1 = val
        
    def set_var2(self, val):
        # Do some tests to make sure val is appropriate, and then set
        self._var2 = val
        
mc = MyClass()
print "mc._var1 =", mc._var1, "mc._var2=", mc._var2

This will give the following output:

mc._var1 = None mc._var2= spikeplot.png

We can override individual variables:

mc = MyClass(var2='case2')
print "mc._var1 =", mc._var1, "mc._var2=", mc._var2

and that will give:

mc._var1 = None mc._var2= case2

Finally, if the user tries to pass an invalid argument, an error is thrown.:

mc = MyClass(not_in_dict='My Invalid Keyword Arg')
print "mc._var1 =", mc._var1, "mc._var2=", mc._var2

This is the output.:

AttributeError: Unknown property not_in_dict

Note

This function removes the overriden items from the defaults dict. If the class needs to retain this dict, it should send a copy to this function.

Previous topic

Utilities

Next topic

dictdb

This Page