class builder: """ This is class responsible for building new items for database. """ def __init__(self, target: object = None) -> None: """ This create new builder, cleaning it by default. Parameters: target (object): Object to work on """ self.clear(target) @property def _target(self) -> object: """ Target to work on for child class. """ return self.__target @property def result(self) -> object: """ This return created object, or raise exception if it is not ready. Returns: (object): Ready created object """ if not self.__target.ready: raise TypeError("Can not read not ready result.") return self.__target def clear(self, target: object = None) -> None: """ This drop previous object from builded, and set new one. When target is not set, it create empty object to work on. Parameters: target (object | None): Target to work on or None """ if target is not None: self.__target = target return self.__target = self.__target_type() def __init_subclass__(cls, target_type: type, **kwargs) -> None: """ This create new class. It require target_type, which is class of new objects. It must have ready property, to check when new object is ready to load. Parameters: target_type(type): Type of target objects **kwargs: Rest of parameters """ super().__init_subclass__(**kwargs) if not type(target_type.ready) is property: raise Exception("Builder can build only database items.") cls.__target_type = target_type