tethys.core.regobjs.regobj_zero
¶
Module Contents¶
-
class
tethys.core.regobjs.regobj_zero.
ZeroRegistrableObject
[source]¶ Bases:
tethys.core.regobjs.regobj_base.RegistrableObjectBase
Base class for all entities that used like models for a database. It’s the Zero generation.
- A few examples:
class SomeClass(ZeroRegistrableObject): FIELDS_SCHEMA = { "some_field": {"type": "string"} } def __init__(self, some_field, another_field, **kwargs): self.some_field = some_field self.another_field = another_field # ... # ------------------------------------------------- obj1 = SomeClass("str", "no:save", _id="some_id") obj2 = SomeClass.load("some_id") assert id(obj1) == id(obj2) # ------------------------------------------------- obj1 = SomeClass("str", "no:save") obj1.save() obj2 = ZeroRegistrableObject.load(obj1.path) assert id(obj1) == id(obj2) # ------------------------------------------------- ZeroRegistrableObject.REPOSITORY = JsonFileRepository(create=True) obj1 = SomeClass(some_field="str", another_field="no:save", _id="some_id") obj1.save() assert obj1.some_field == "str" assert obj1.another_field == "no:save" # restart script obj2 = SomeClass.load("some_id") assert obj1.some_field == "str" assert obj1.another_field == "no:save" # raise AttributeError: 'SomeClass' object has no attribute 'another_field'
Create and return a new object. See help(type) for accurate signature.
-
classmethod
prepare
(cls, item: dict, keys: list = None) → dict[source]¶ Validate and normalize data in the attrs that define in the
FIELDS_SCHEMA
-
classmethod
load
(cls: Type[RegObjZT], obj_id: str, with_cache: bool = True, **kwargs) → RegObjZT[source]¶ Load the entity from the repository by ID. If the entity already in the memory then method will return cached version.
- Parameters
- Returns
ZeroRegistrableObject instance
- Return type
-
classmethod
list
(cls: Type[RegObjZT], list_filter: dict = None, **kwargs) → List[RegObjZT][source]¶ Load list of the entities. You can specify some filters, but the filter’s query language is experimental.
- Filter example:
{“some_field>=”: 2} - list entities where some_field >= 2
“{field_name}{operator}” - key template
- Basic filters:
operator
python op.
->
field in {value}
!>
field not in {value}
>=
field >= {value}
>
field > {value}
<=
field <= {value}
<
field < {value}
!=
field != {value}
==
field == {value}
- Parameters
list_filter (dict) – dictionary of the filters
- Returns
list of the ZeroRegistrableObject instances
- Return type
List(ZeroRegistrableObject)
-
refresh
(self: RegObjZT, ignore_errors: bool = True, **kwargs) → Optional[RegObjZT][source]¶ Reload entity from the repository (without cache)
- Parameters
ignore_errors (bool) – ignore errors like TethysRONotFound (default: True)
- Returns
return ZeroRegistrableObject instance or None (when error like NotFound)
- Return type
-
save
(self: RegObjZT, save_dependency: bool = True, save_dependency_depth: int = 6, save_dependency_search_depth: int = 0, **kwargs) → RegObjZT[source]¶ Save current state to the repository.
-
delete
(self: RegObjZT, **kwargs) → RegObjZT[source]¶ Delete the object from the repository.
- Returns
return self object
- Return type
-
lock
(self, lock_ttl: float = 60, wait_timeout: float = float('inf'), blocking: bool = True, **kwargs) → bool[source]¶ Lock the object in the repository.
- Parameters
- Returns
is locked by the current process?
-
lock_context
(self, lock_ttl: float = 60, wait_timeout: float = float('inf'), blocking: bool = True, **kwargs)[source]¶ Contextmanager for the Locking/Unlocking the object in the repository. It allows nested context
- Example:
start = time.time() def lock(): obj.lock() print(time.time() - start) # ~ 2s with obj.lock_context(lock_ttl=1): with obj.lock_context(lock_ttl=2): print(time.time() - start) # ~ 0s Thread(target=lock).start() time.sleep(5) # todo_something