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.sourceobject for information retrieval. - I recommend you to make @properties for retrieving the information for cleaner code.
- you can access the
- Override
YamlAssetLoader[T]/JSONAssetLoader[T]with yourYamlAsset/JSONAssetderivative- define the
asset_folder_name()andasset_class()methods.
- define the
- 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
PythonAssetwith your base Python class.- define the classmethod
base_asset_class_nameto return the class name of your main derivative. - define your properties and methods that you want to override in your asseted classes.
- define the classmethod
- Override
PythonAssetLoader[T]with yourPythonAssetderivative and define theasset_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
PythonAssetderivative or not. - The derivatives are automatically instantiated and added into your Loader's
loaded_assets.
- These modules will only be loaded regardless of wheter they contain a derivative of your
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
LoadedAssetand theAssetLoaderin a similar manner to the existing derivatives.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1995bbc5d3683c48196048f60647b4141b14c06c5b658843ce276436deca8b6c
|
|
| MD5 |
55b8a6cf6c69fefca09dc0c69ffd8434
|
|
| BLAKE2b-256 |
219c26d73ffe558d1d211811dd3ed00f282db87e369510cac2a2212d7b451c7b
|
Provenance
The following attestation bundles were made for centralized_data-0.0.4.tar.gz:
Publisher:
python-publish.yml on Snowcaloid/py-centralized-data
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
centralized_data-0.0.4.tar.gz -
Subject digest:
1995bbc5d3683c48196048f60647b4141b14c06c5b658843ce276436deca8b6c - Sigstore transparency entry: 175993239
- Sigstore integration time:
-
Permalink:
Snowcaloid/py-centralized-data@6ca70147b1ac5f2d1ca1dac8ea5238850dbc4d21 -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/Snowcaloid
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6ca70147b1ac5f2d1ca1dac8ea5238850dbc4d21 -
Trigger Event:
release
-
Statement type:
File details
Details for the file centralized_data-0.0.4-py3-none-any.whl.
File metadata
- Download URL: centralized_data-0.0.4-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5a6f85490da6524beaa401ebc8fc22a87490152b99c488ad8dd60c4f6ff273d
|
|
| MD5 |
6dd58ecad75865af80c78301f09fe583
|
|
| BLAKE2b-256 |
8ba6d218ffd154b9ce87832f68dc7c76101191ea83cdf0c1d98e8c00705a2c52
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
centralized_data-0.0.4-py3-none-any.whl -
Subject digest:
e5a6f85490da6524beaa401ebc8fc22a87490152b99c488ad8dd60c4f6ff273d - Sigstore transparency entry: 175993240
- Sigstore integration time:
-
Permalink:
Snowcaloid/py-centralized-data@6ca70147b1ac5f2d1ca1dac8ea5238850dbc4d21 -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/Snowcaloid
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6ca70147b1ac5f2d1ca1dac8ea5238850dbc4d21 -
Trigger Event:
release
-
Statement type: