How to use a OO class inside cadCAD state?

Hi people, I’m expressing the Commons Stack as a class Commons in Python (python notebook here) to abstract away some of the complexity.

The problem is that cadCAD seems to expect the state to serve as the Commons , but it’s actually all wrapped away behind my Commons class. The result is I have to make my state update function secretly update more variables than it officially should (see the end of def update_commons(). I really want the variables funding_pool, collateral_pool, token_supply to be in the state so it’s easy to convert into a pandas Dataframe for plotting, but they’re actually inside the commons object.

def update_commons(params, step, sL, s, _input):
    commons = s["commons"]
    participants_expenditure = _input["update_network_spending"]
    for expenditure in participants_expenditure:
        if expenditure > 0:
            commons.burn(expenditure)
            s["funding_pool"] = commons._funding_pool
            s["collateral_pool"] = commons._collateral_pool
            s["token_supply"] = commons._token_supply
    return "commons", commons

I’m not sure if this is okay/violates any assumptions cadCAD internally makes? Is there a different way to do this?

1 Like

Thanks @randomshinichi. The assignments made to s["funding_pool"] s["collateral_pool"] and s["token_supply"] violate cadCAD’s internal flow. All state updates should be made in specific state update functions for each state variable. To avoid unintended consequences, I’d recommend you delete these three lines of code

s["funding_pool"] = commons._funding_pool
s["collateral_pool"] = commons._collateral_pool
s["token_supply"] = commons._token_supply

and add a new partial state update block to your simulation, with 3 state update functions, one for each of those 3 state variables.

Its important to note that this extra layer of syntax that makes it harder to use the OO paradigm comes from the fact that under the hood cadCAD is essentially data pipelines: sequences of transformers. This is a much more functional architecture as it is focused on how data flows through the code rather than how the code itself is organized. Much like in ML use cases which also rely on sequences of transformers, we’re working steadily to improve UX at the API level with feedback from users. so @randomshinichi, please continue to engage with the team on how we can make the APIs more intuitive for people coming from a more OO paradigm (within the context of the underlying data pipeline based architecture).