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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file scoped-singleton-1.1.7.tar.gz.

File metadata

  • Download URL: scoped-singleton-1.1.7.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for scoped-singleton-1.1.7.tar.gz
Algorithm Hash digest
SHA256 a613c5de036144edd7ab05d08e2caf6ec981e091737d2a51a591c2b8c6a2bcab
MD5 da24b0d625731b38db36f0fd57f8edd1
BLAKE2b-256 3da986794d295c0b145c751ff571958320e125f68f2fde967934674e2ff310cc

See more details on using hashes here.

File details

Details for the file scoped_singleton-1.1.7-py3-none-any.whl.

File metadata

  • Download URL: scoped_singleton-1.1.7-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for scoped_singleton-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 391a304c888b889a639da6f947f1d75214c75090baed6da4859d4246761090be
MD5 6c6d85ba1ee7b7cf7c4105bc58474fee
BLAKE2b-256 7822157a6ce194c5b72f63512efd6ef6292780a6c99b1c04f6500898eb396e64

See more details on using hashes here.

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