Easier sharing data between objects
pip3 install scoped-singleton
from dataclasses import dataclass
from scoped_singleton import thr
@thread_scoped_singleton
@dataclass
class Account:
uuid: str
use it with cached_property to stop repeated requests
from dataclasses import dataclass
from cached_property import cached_property
from scoped_singleton import thread_scoped_singleton
@thread_scoped_singleton
@dataclass
class Account:
uuid: str
@cached_property
def credit_limit(self):
return rpc(self.uuid)["credit_limit"]
@dataclass
class Transaction:
id: int
account_uuid: str
@property
def account(self):
return Account(uuid=self.account_uuid)
txn1 = Transaction(id=1, account_uuid="123")
txn2 = Transaction(id=2, account_uuid="123")
txn3 = Transaction(id=3, account_uuid="124")
assert txn1.account is txn2.account
assert txn1.account is not txn3.account