Small library for observation mechanism
Project description
ntt-json-model
Library with model object which is observable and can be serialize and deserialize from json file
Example
With primitive
data
from ntt_json_model import *
class TestModel(ModelBase):
def __init__(self,
nValue: int = 3,
strName: str = "Hello",
fTemp: float = 3.1,
lstScores: List[int] = []) -> None:
super().__init__()
IntegerProperty(self, nValue, "_nValue")
FloatProperty(self, fTemp, "_fTemp")
StrProperty(self, strName, "_strName")
ListProperty(self, lstScores, "_lstScores")
@property
def Value(self) -> int:
return self._nValue.GetValue()
@Value.setter
def Value(self, nValue: int) -> None:
self._nValue.SetValue(nValue)
@property
def Temp(self) -> str:
return self._fTemp.GetValue()
@Temp.setter
def Temp(self, fTemp: float) -> None:
self._fTemp.SetValue(fTemp)
@property
def Name(self) -> str:
return self._strName.GetValue()
@Name.setter
def Name(self, strName: str) -> None:
self._strName.SetValue(strName)
@property
def Scores(self) -> List[int]:
return self._lstScores.GetValue()
ModelBase.mSubModels[TestModel.__name__] = TestModel
def PrintIfChanged() -> None:
print("Model has changed")
model = TestModel()
model.Connect(PrintIfChanged)
model.Temp = 3 # ---> "Model has changed"
print(model.ToDict())
# Output:
# {
# "__class__": "TestModel",
# "_nValue": 3,
# "_fTemp": 3.0,
# "_strName": "Hello",
# "_lstScores": []
# }
model.FromDict({
"__class__": "TestModel",
"_nValue": 3,
"_fTemp": 3.0,
"_strName": "Hello",
"_lstScores": [4, 3]
})
Model
Data
class TestModelClass(ModelBase):
def __init__(self, nScore: int = 4, *args, **kwargs) -> None:
super().__init__()
IntegerProperty(self, nScore, "_nScore")
ModelProperty(self, TestModel(*args, **kwargs), "_mTestModel")
@property
def Score(self) -> int:
return self._nScore.GetValue()
@Score.setter
def Score(self, nNewScore: int) -> None:
self._nScore.SetValue(nNewScore)
@property
def TestModel(self) -> TestModel:
return self._mTestModel.GetValue()
ModelBase.mSubModels[TestModelClass.__name__] = TestModelClass
Model List
Data
class TestModelListClass(ModelBase):
def __init__(self, fScore: float = 8.5) -> None:
super().__init__()
FloatProperty(self, fScore, "_fScore")
ModelListProperty(self, [], "_mModels")
@property
def Score(self) -> float:
return self._fScore.GetValue()
@Score.setter
def Score(self, fNewScore: float) -> None:
self._fScore.SetValue(fNewScore)
@property
def Models(self) -> List[TestModelClass]:
return self._mModels.GetValue()
ModelBase.mSubModels[TestModelListClass.__name__] = TestModelListClass
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
ntt-json-model-1.0.0.tar.gz
(8.3 kB
view details)
Built Distribution
File details
Details for the file ntt-json-model-1.0.0.tar.gz
.
File metadata
- Download URL: ntt-json-model-1.0.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b56d8f9cdd2718bc3e4497804d262d5ab775f7f1c69426b0e4205bdff26fea7 |
|
MD5 | 0bda445f5a67e316a0e6b54915d605b8 |
|
BLAKE2b-256 | 820b6d2e87ad3679a809004ed9233fa8f6c10b64e52505a3ad10fa218effc66b |
File details
Details for the file ntt_json_model-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: ntt_json_model-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c727579f2bd9ca65f5f81eca3cff671d1ade7557d98fbb0e63e2deea4cbc409 |
|
MD5 | 7fa7f9de8a529e8c3212587517ba37b2 |
|
BLAKE2b-256 | 10c7a4a42ae399df5e22896d209cbbb65ca296078c1b567a72a728591bfae5ae |