Skip to main content

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

Reason this release was yanked:

old

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.2.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.2-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: centralized_data-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 e512e77a6c039df58d0af07575d8af101b4d608707f857c418ef4f820bd6ce6e
MD5 1f2784f140670e643afc2f3b45393731
BLAKE2b-256 f4e34bdf8ba4e6029b551704dc7f75f8eccafeedc5d3742105cec11fff81e9a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for centralized_data-0.0.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for centralized_data-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c50e8aa5936f129b8f328fba9cd0e923c5e9a5eb516a1cf03c182e16b7016a6e
MD5 f8e2b1aa286caa7709add7b9b9e09710
BLAKE2b-256 59b71060d8ee0ccc99ee79fc3f56d9f361d58d5023e11293ba8d61b45d44ed88

See more details on using hashes here.

Provenance

The following attestation bundles were made for centralized_data-0.0.2-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