Sending computed properties to stream

Hi everyone. I would like to send a computed property from python to a speckle 2.0 stream. However, when I use the object below, the value for required_amount is not available on the server. Is there any way to fix this without declaring it in the init?

class Object(Base):

    def __init__(self, name: str = None, minimum_amount: float = 0, pshc: int = None, hc_factor: float = None):

        super().__init__()
        self.name = name
        self._minimum_amount = minimum_amount
        self._hc_factor = hc_factor
        self._pshc = pshc

    @property
    def required_amount(self):
        return max(self._minimum_amount, self._hc_factor * self._pshc)
2 Likes

Hey @marijn!

Sorry for the late reply, your post must have fallen through the cracks… :man_bowing:t3:

As for your question, I don’t think this is doable, as the whole point of computed properties is that you don’t need to serialise them (as their value would be recomputed when needed

Pinging @gjedlicska in-case there’s some python black-magic that can be done to get the result you want.

Hey @marijn

As Alan said, properties in python are not supported for serialization, as the example below shows. We’re following the same in our base class currently.

import json
from dataclasses import dataclass, asdict

@dataclass
class Foo:
    bar: str

    @property
    def also_bar(self):
      return self.bar
  

if __name__ == "__main__":
    foo = Foo('asdf')

    print(json.dumps(asdict(foo)))

prints → {"bar": "asdf"}