kalekundert / stepwise_mol_bio

MIT License
0 stars 0 forks source link

Be more clever about caching reactions #5

Closed kalekundert closed 3 years ago

kalekundert commented 3 years ago

Most of my protocols are implemented as classes with protocol and reaction properties. Both properties are calculated from scratch each time they are accessed. However, this limits the flexibility of the python API. It's also a little confusing, because although the object returned by the reaction property is mutable, any changes will not be reflected in the protocol.

It'd be nice to be able to (i) calculate the reaction as usual, (ii) change any aspect of the reaction, (iii) make a protocol with the modified reaction. Really, this just means making the reaction behave more like a normal attribute (e.g. calculated in the constructor or something). To do this, I'd need one method to calculate the reaction, and another to get the reaction (and calculate it if necessary). Basically a typical caching scheme:

class MyProtocol:

    def __init__(self):
        self._rxn = None

    def find_rxn(self):
        rxn = stepwise.MasterMix()
        self.set_rxn(rxn)

    def get_rxn(self):
        if self._rxn is None:
            self._find_rxn()
        return self._rxn

    def set_rxn(self, rxn):
        self._rxn = rxn

This is a lot of boiler plate, though. Is there any way to cut down on it? And would I want to recalculate on any attribute change?

kalekundert commented 3 years ago

This is solved by the new caching policies in autoprop>=4.0