Skip to main content

Package for cantralized-data, such as singletons, bindable singletons, global collections, dynamically loaded objects in Python.

Project description

Singleton objects similar to Angular Services

Installation

pip install centralized_data

Usage 1 - binding to properties

The singleton object is first initialized whenever it is bound to a property or when the first constructor is called.

from typing import override
from centralized_data import Bindable

class MyBindable(Bindable):
    @override
    def constructor(self) -> None:
        self.value = 3

    def method(self) -> str:
        return "Success"

class MyUsingClass:
    @MyBindable.bind
    def my_binding(self) -> MyBindable: ... # This code is overridden by the binding function. Just use ... or pass.

    def some_method(self):
        print(self.my_binding.value)
        print(self.my_binding.method())

Usage 2 - Singleton

The singleton object is first initialized whenever the first constructor is called.

from bindable import Bindable

class MyBindable(Bindable):
    @override
    def constructor(self) -> None:
        self.value = 3

    def method(self) -> str:
        return "Success"

def do_something() -> None:
    my_bindable = MyBindable()
    print(my_bindable.value)
    print(my_bindable.method())
    print(MyBindable().value) # always returns the same object.
    print(MyBindable().method()) # always returns the same object.

Usage 3 - Global Collections

The singleton objects are initialized when first accessed.

from bindable import GlobalCollection

class Customer(GlobalCollection[int]):
    @override
    def constructor(self, key: int) -> None:
        super().constructor(key)
        self.load_from_database()

    def load_from_database(self) -> None:
        record = database.sql('select name, address from customers')
        self.name = record['name']
        self.address = record['address']

Customer(1)
Customer(2).name = 'john'
Customer(2).name # already exists and is therefore not going to call the constructor again
Customer.clear()

Asset Loader

  • You have multiple Python classes deriving from another python class that extend it, but aren't directly referenced by your code.
/project
|- task.py
|- task_list.py
/assets
  /tasks
  |- write_hello_world_every_minute.py # is descendant of a class from task.py
  |- daily_plan_for_world_dominance.py # is descendant of a class from task.py
  |- monthly_defeat_the_demon_king.py # is descendant of a class from task.py
  • You have multiple yaml/json-Files that describe a workflow or a class in your code
/project
|- table.py
/assets
  /db_tables
  |- customer.yaml
  |- article.yaml
  |- shop.yaml

Usage 1 - use the existing templates for Yaml/JSON/Py Files

Yaml/JSON

  • Override YamlAsset/JSONAsset.
    • you can access the self.source object for information retrieval.
    • I recommend you to make @properties for retrieving the information for cleaner code.
  • Override YamlAssetLoader[T]/JSONAssetLoader[T] with your YamlAsset/JSONAsset derivative
    • define the asset_folder_name() and asset_class() methods.
  • Create your yaml/json files inside the folder */assets/<asset_folder_name()>/
class MyTable(YamlAsset):
    @property
    def some_field(self):
        return self.source.get("some_field")

class MyTables(YamlAssetLoader[MyTable]):
    @override
    def asset_class(self) -> Type[MyTable]: return MyTable
    @override
    def asset_folder_name(self) -> str: return 'db_tables'

Python

  • Override PythonAsset with your base Python class.
    • define the classmethod base_asset_class_name to return the class name of your main derivative.
    • define your properties and methods that you want to override in your asseted classes.
  • Override PythonAssetLoader[T] with your PythonAsset derivative and define the asset_folder_name() method.
  • Create your python files inside the folder */assets/<asset_folder_name()>/
    • These modules will only be loaded regardless of wheter they contain a derivative of your PythonAsset derivative or not.
    • The derivatives are automatically instantiated and added into your Loader's loaded_assets.
class Task(PythonAsset):
    @abstractmethod
    def type(self) -> Enum: ...
    @abstractmethod
    async def execute(self) -> any:

class Tasks(PythonAssetLoader[Task]):
    @override
    def asset_folder_name(self) -> str: return 'tasks'

    def get_task(self, type: Enum) -> Task:
        return next([task for task in self.loaded_assets if task.type() == type])

assets/tasks/daily_plan_for_world_dominance.py
...

class PlanForWorldDominance(Task):
    @override
    def type(self) -> Enum: return ...
    @override
    async def execute(self) -> any: print('veni vidi vici')

Usage 2 - implement your own loader

  • Simply override the LoadedAsset and the AssetLoader in a similar manner to the existing derivatives.

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

centralized_data-0.0.4.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

centralized_data-0.0.4-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file centralized_data-0.0.4.tar.gz.

File metadata

  • Download URL: centralized_data-0.0.4.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for centralized_data-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1995bbc5d3683c48196048f60647b4141b14c06c5b658843ce276436deca8b6c
MD5 55b8a6cf6c69fefca09dc0c69ffd8434
BLAKE2b-256 219c26d73ffe558d1d211811dd3ed00f282db87e369510cac2a2212d7b451c7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for centralized_data-0.0.4.tar.gz:

Publisher: python-publish.yml on Snowcaloid/py-centralized-data

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file centralized_data-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for centralized_data-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e5a6f85490da6524beaa401ebc8fc22a87490152b99c488ad8dd60c4f6ff273d
MD5 6dd58ecad75865af80c78301f09fe583
BLAKE2b-256 8ba6d218ffd154b9ce87832f68dc7c76101191ea83cdf0c1d98e8c00705a2c52

See more details on using hashes here.

Provenance

The following attestation bundles were made for centralized_data-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on Snowcaloid/py-centralized-data

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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