Skip to main content

Easier sharing data between objects

Project description

scoped-singleton

GitHub Build Status codecov PyPI

Easier sharing data between objects

Installation

pip3 install scoped-singleton

How to use it

from dataclasses import dataclass

from scoped_singleton import context_scoped_singleton


@context_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 context_scoped_singleton


@context_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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scoped-singleton-1.1.7.tar.gz (2.4 kB view hashes)

Uploaded Source

Built Distribution

scoped_singleton-1.1.7-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page