pwtools.base.FlexibleGetters

class pwtools.base.FlexibleGetters[source]

Bases: object

The most basic base class – the mothership!

Implements a mechanism which allows to call getters in arbitrary order, even if they depend on each other. The mechanism also assured that the code in each getter is only executed once (by using checks with self.is_set_attr()).

For each attr, there must exist a getter. We define the convention:

self.foo  -> self.get_foo()
self.bar  -> self.get_bar()
self._baz -> self._get_baz() # note the underscores
...

self.attr_lst is an optional list of strings, each is the name of a data attribute, e.g. [‘foo’, ‘bar’, ‘_baz’, …]. Derived classes can override self.attr_lst by using self.set_attr_lst().

This model is extremely powerful and allows the construction of very general code (see parse.py). One drawback: Beware of cyclic dependencies (i.e. get_bar -> get_foo -> get_bar -> …, max. recursion limit error). Always test! This corner case danger is outweight by usefullness.

Examples

class MySuperParsingClass(FlexibleGetters):
    def __init__(self):
        self.set_attr_lst(['foo', 'bar', '_baz'])
        self.set_all()

    def set_all(self):
        "Sets self.foo, self.bar and self._baz by calling their
        getters"
        for attr in self.attr_lst:
            self.try_set_attr(attr)

    # Getters call each other
    def _get_baz(self):
        return self.calc_baz()

    def get_bar(self):
        if self.check_set_attr('_baz'):
            return self.calc_stuff(self._baz)**2.0
        else:
            return None

    def get_foo(self):
        required = ['bar', '_baz']
        if self.check_set_attr_lst(required):
            return do_stuff(self._baz, self.bar)
        else:
            return None

Setting self.attr_lst is optional. It is supposed to be used only in set_all(). The try_set_attr() - method works without it, too.

__init__()[source]

Methods

assert_attr(attr)

Raise AssertionError if self.<attr> is not set (is_set_attr() returns False.

assert_attr_lst(attr_lst)

assert_set_attr(attr)

Same as assert_attr(), but run try_set_attr() first.

assert_set_attr_lst(attr_lst)

check_set_attr(attr)

Run try_set_attr() and return the result of is_set_attr(), i.e. True or False.

check_set_attr_lst(attr_lst)

dump(dump_filename[, mkdir])

Write object to binary file using pickle.

get_return_attr(attr_name)

Call try_set_attr() are return self.<attr_name> if set.

init_attr_lst([attr_lst])

Set each self.<attr> in attr_lst to None.

is_set_attr(attr)

Check if self has the attribute self.<attr> and if it is _not_ None.

is_set_attr_lst(attr_lst)

load(dump_filename)

Load pickled object.

raw_return(attr_name)

Call try_set_attr(_<attr_name>_raw) and return it if set, else None.

raw_slice_get(attr_name, sl, axis)

Shortcut method:

set_all([attr_lst])

Call getter for each attr name in attr_lst.

set_attr_lst(attr_lst)

Set self.attr_lst and init each attr to None.

try_set_attr(attr)

If self.<attr> does not exist or is None, then invoke an appropirately named getter as if this command would be executed.

try_set_attr_lst(attr_lst)