Best Python code snippet using lisa_python
subclasses.py
Source:subclasses.py
...9 def __init__(self, runbook: Any, *args: Any, **kwargs: Any) -> None:10 super().__init__()11 self.runbook = runbook12 @classmethod13 def create_with_runbook(14 cls, runbook: schema.TypedSchema, **kwargs: Any15 ) -> "BaseClassWithRunbookMixin":16 if cls.type_schema() != type(runbook):17 # reload if type is defined in subclass18 runbook = schema.load_by_type(cls.type_schema(), runbook)19 return cls(runbook=runbook, **kwargs)20 @classmethod21 def type_schema(cls) -> Type[schema.TypedSchema]:22 raise NotImplementedError()23T_BASECLASS = TypeVar("T_BASECLASS", bound=BaseClassMixin)24if TYPE_CHECKING:25 SubClassTypeDict = UserDict[str, type]26else:27 SubClassTypeDict = UserDict28class Factory(InitializableMixin, Generic[T_BASECLASS], SubClassTypeDict):29 def __init__(self, base_type: Type[T_BASECLASS]) -> None:30 super().__init__()31 self._base_type = base_type32 self._log = get_logger("subclasses", base_type.__name__)33 def _initialize(self, *args: Any, **kwargs: Any) -> None:34 # initialize types from subclasses.35 # each type should be unique in code, or there is warning message.36 for subclass_type in self._get_subclasses(self._base_type):37 subclass_type_name = subclass_type.type_name()38 exists_type = self.get(subclass_type_name)39 if exists_type:40 # so far, it happens on ut only.41 # When UT code import each other, it happens.42 # it's important to use first registered.43 self._log.error(44 f"registered [{subclass_type_name}] subclass again. "45 f"It should happen in UT only. "46 f"new: [{subclass_type}], exist: [{exists_type}]"47 )48 else:49 self[subclass_type.type_name()] = subclass_type50 self._log.debug(51 f"registered: " f"[{', '.join([name for name in self.keys()])}]"52 )53 def load_typed_runbook(self, raw_runbook: Any) -> T_BASECLASS:54 type_name = raw_runbook[constants.TYPE]55 sub_type = self._get_sub_type(type_name)56 instance: Any = schema.load_by_type(sub_type, raw_runbook)57 if hasattr(instance, "extended_schemas"):58 if instance.extended_schemas:59 raise LisaException(60 f"found unknown fields: {instance.extended_schemas}"61 )62 return cast(T_BASECLASS, instance)63 def create_by_type_name(self, type_name: str, **kwargs: Any) -> T_BASECLASS:64 sub_type = self._get_sub_type(type_name)65 return cast(T_BASECLASS, sub_type(**kwargs))66 def create_by_runbook(67 self, runbook: schema.TypedSchema, **kwargs: Any68 ) -> T_BASECLASS:69 sub_type = self._get_sub_type(runbook.type)70 sub_type_with_runbook = cast(Type[BaseClassWithRunbookMixin], sub_type)71 sub_object = sub_type_with_runbook.create_with_runbook(72 runbook=runbook, **kwargs73 )74 assert isinstance(75 sub_object, BaseClassWithRunbookMixin76 ), f"actual: {type(sub_object)}"77 return cast(T_BASECLASS, sub_object)78 def _get_subclasses(79 self, type: Type[BaseClassMixin]80 ) -> Iterable[Type[BaseClassMixin]]:81 # recursive loop subclasses of subclasses82 for subclass_type in type.__subclasses__():83 yield subclass_type84 yield from self._get_subclasses(subclass_type)85 def _get_sub_type(self, type_name: str) -> type:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!