[PEAK] Re: trellis.Set.discard

Sergey Schetinin maluke at gmail.com
Tue Oct 14 02:40:31 EDT 2008


Here's my take on merging variant:

class MergingValue(Value):
    __slots__ = 'inputs', 'scheduled', 'merge'

    def __init__(self, value=None, discrete=False, merge=None):
        self.inputs = {}
        self.scheduled = False
        if not callable(merge):
            raise TypeError() #@@
        self.merge = merge
        Value.__init__(self, value, discrete)

    @modifier
    def set_value(self, value):
        lock(self)
        cl = ctrl.current_listener
        self.inputs[cl] = value
        on_undo(self.inputs.__delitem__, cl)
        if not self.scheduled:
            change_attr(self, 'scheduled', True)
            on_commit(self.do_merge)

    def get_value(self):
        cl = ctrl.current_listener
        if cl in self.inputs:
            return self.inputs[cl]
        else:
            return Value.get_value(self)

    value = property(get_value, set_value)

    def do_merge(self):
        newval = self.merge(self._value, self.inputs)
        Value.set_value(self, newval)
        self.inputs = {}
        self.scheduled = False

A merge function could look like this:

def sum_merge(val, map):
    return sum(map.itervalues(), val)



I went for a simpler approach than @todo / future, and I didn't write
the decorators for it but still, I already like this better. It seems
to work as expected except for discrete case.

Setting this value doesn't count as a write, because the changes they
don't need to be tracked and will be merged later anyway, so no need
to ever retry.



More information about the PEAK mailing list