Best Python code snippet using lisa_python
search_space.py
Source:search_space.py
...44 def check(self, capability: Any) -> ResultReason:45 raise NotImplementedError()46 def generate_min_capability(self, capability: Any) -> Any:47 self._validate_result(capability)48 return self._generate_min_capability(capability)49 def intersect(self, capability: Any) -> Any:50 self._validate_result(capability)51 return self._intersect(capability)52 def _call_requirement_method(self, method_name: str, capability: Any) -> Any:53 raise NotImplementedError(method_name)54 def _generate_min_capability(self, capability: Any) -> Any:55 return self._call_requirement_method(56 method_name=RequirementMethod.generate_min_capability,57 capability=capability,58 )59 def _intersect(self, capability: Any) -> Any:60 return self._call_requirement_method(61 method_name=RequirementMethod.intersect, capability=capability62 )63 def _validate_result(self, capability: Any) -> None:64 check_result = self.check(capability)65 if not check_result.result:66 raise NotMeetRequirementException(67 f"capability doesn't support requirement: {check_result.reasons}"68 )69T_SEARCH_SPACE = TypeVar("T_SEARCH_SPACE", bound=RequirementMixin)70@dataclass_json()71@dataclass72class IntRange(RequirementMixin):73 min: int = 074 max: int = field(default=sys.maxsize)75 max_inclusive: bool = True76 def __post_init__(self, *args: Any, **kwargs: Any) -> None:77 if self.min > self.max:78 raise LisaException(79 f"min: {self.min} shouldn't be greater than max: {self.max}"80 )81 elif self.min == self.max and self.max_inclusive is False:82 raise LisaException(83 "min shouldn't be equal to max, if max_includes is False."84 )85 def __repr__(self) -> str:86 max_value = self.max if self.max < sys.maxsize else ""87 max_inclusive = ""88 if max_value:89 max_inclusive = "(inc)" if self.max_inclusive else "(exc)"90 return f"[{self.min},{max_value}{max_inclusive}]"91 def __eq__(self, __o: object) -> bool:92 assert isinstance(__o, IntRange), f"actual type: {type(__o)}"93 return (94 self.min == __o.min95 and self.max == __o.max96 and self.max_inclusive == __o.max_inclusive97 )98 def check(self, capability: Any) -> ResultReason:99 result = ResultReason()100 if capability is None:101 result.add_reason("capability shouldn't be None")102 else:103 if isinstance(capability, IntRange):104 if capability.max < self.min:105 result.add_reason(106 f"capability max({capability.max}) is "107 f"smaller than requirement min({self.min})"108 )109 elif capability.max == self.min and not capability.max_inclusive:110 result.add_reason(111 f"capability max({capability.max}) equals "112 f"to requirement min({self.min}), but "113 f"capability is not max_inclusive"114 )115 elif capability.min > self.max:116 result.add_reason(117 f"capability min({capability.min}) is "118 f"bigger than requirement max({self.max})"119 )120 elif capability.min == self.max and not self.max_inclusive:121 result.add_reason(122 f"capability min({capability.min}) equals "123 f"to requirement max({self.max}), but "124 f"requirement is not max_inclusive"125 )126 elif isinstance(capability, int):127 if capability < self.min:128 result.add_reason(129 f"capability({capability}) is "130 f"smaller than requirement min({self.min})"131 )132 elif capability > self.max:133 result.add_reason(134 f"capability ({capability}) is "135 f"bigger than requirement max({self.max})"136 )137 elif capability == self.max and not self.max_inclusive:138 result.add_reason(139 f"capability({capability}) equals "140 f"to requirement max({self.max}), but "141 f"requirement is not max_inclusive"142 )143 else:144 assert isinstance(capability, list), f"actual: {type(capability)}"145 temp_result = _one_of_matched(self, capability)146 if not temp_result.result:147 result.add_reason(148 "no capability matches requirement, "149 f"requirement: {self}, capability: {capability}"150 )151 return result152 def _generate_min_capability(self, capability: Any) -> int:153 if isinstance(capability, int):154 result: int = capability155 elif isinstance(capability, IntRange):156 if self.min < capability.min:157 result = capability.min158 else:159 result = self.min160 else:161 assert isinstance(capability, list), f"actual: {type(capability)}"162 result = self.max if self.max_inclusive else self.max - 1163 for cap_item in capability:164 temp_result = self.check(cap_item)165 if temp_result.result:166 temp_min = self.generate_min_capability(cap_item)167 result = min(temp_min, result)168 return result169 def _intersect(self, capability: Any) -> Any:170 if isinstance(capability, int):171 return capability172 elif isinstance(capability, IntRange):173 result = IntRange(174 min=self.min, max=self.max, max_inclusive=self.max_inclusive175 )176 if self.min < capability.min:177 result.min = capability.min178 if self.max > capability.max:179 result.max = capability.max180 result.max_inclusive = capability.max_inclusive181 elif self.max == capability.max:182 result.max_inclusive = capability.max_inclusive and self.max_inclusive183 else:184 raise NotImplementedError(185 f"IntRange doesn't support other intersect on {type(capability)}."186 )187 return result188CountSpace = Union[int, List[IntRange], IntRange, None]189def decode_count_space(data: Any) -> Any:190 """191 CountSpace is complex to marshmallow, so it needs customized decode.192 Anyway, marshmallow can encode it correctly.193 """194 decoded_data: CountSpace = None195 if data is None or isinstance(data, int) or isinstance(data, IntRange):196 decoded_data = data197 elif isinstance(data, list):198 decoded_data = []199 for item in data:200 if isinstance(item, dict):201 decoded_data.append(IntRange.schema().load(item)) # type: ignore202 else:203 assert isinstance(item, IntRange), f"actual: {type(item)}"204 decoded_data.append(item)205 else:206 assert isinstance(data, dict), f"actual: {type(data)}"207 decoded_data = IntRange.schema().load(data) # type: ignore208 return decoded_data209def _one_of_matched(requirement: Any, capabilities: List[Any]) -> ResultReason:210 result = ResultReason()211 supported = False212 assert isinstance(requirement, RequirementMixin), f"actual: {type(requirement)}"213 for cap_item in capabilities:214 temp_result = requirement.check(cap_item)215 if temp_result.result:216 supported = True217 break218 if not supported:219 result.add_reason("no one meeting requirement")220 return result221@dataclass_json()222@dataclass223class SetSpace(RequirementMixin, Set[T]):224 is_allow_set: bool = False225 items: List[T] = field(default_factory=list)226 def __init__(227 self,228 is_allow_set: Optional[bool] = None,229 items: Optional[Iterable[T]] = None,230 ) -> None:231 self.items: List[T] = []232 if items:233 self.update(items)234 if is_allow_set is not None:235 self.is_allow_set = is_allow_set236 def __repr__(self) -> str:237 return (238 f"allowed:{self.is_allow_set},"239 f"items:[{','.join([str(x) for x in self])}]"240 )241 def __post_init__(self, *args: Any, **kwargs: Any) -> None:242 self.update(self.items)243 def check(self, capability: Any) -> ResultReason:244 result = ResultReason()245 if self.is_allow_set and len(self) > 0 and not capability:246 result.add_reason(247 "if requirements is allow set and len > 0, capability shouldn't be None"248 )249 assert isinstance(capability, SetSpace), f"actual: {type(capability)}"250 assert capability.is_allow_set, "capability must be allow set"251 # if self.options is not None:252 # cap_set = capability.options253 if result.result:254 if self.is_allow_set:255 if not capability.issuperset(self):256 result.add_reason(257 "capability cannot support some of requirements, "258 f"requirement: '{self}'"259 f"capability: '{capability}', "260 )261 else:262 inter_set: Set[Any] = self.intersection(capability)263 if len(inter_set) > 0:264 names: List[str] = []265 for item in inter_set:266 if isinstance(item, type):267 names.append(item.__name__)268 elif isinstance(item, object):269 names.append(item.__class__.__name__)270 else:271 names.append(item)272 result.add_reason(f"requirements excludes {names}")273 return result274 def add(self, element: T) -> None:275 super().add(element)276 self.items.append(element)277 def remove(self, element: T) -> None:278 super().remove(element)279 self.items.remove(element)280 def update(self, *s: Iterable[T]) -> None:281 super().update(*s)282 self.items.extend(*s)283 def _generate_min_capability(self, capability: Any) -> Optional[Set[T]]:284 result: Optional[SetSpace[T]] = None285 if self.is_allow_set and len(self) > 0:286 assert isinstance(capability, SetSpace), f"actual: {type(capability)}"287 result = SetSpace(is_allow_set=self.is_allow_set)288 if len(capability) > 0:289 for item in self:290 if item in capability:291 result.add(item)292 return result293 def _intersect(self, capability: Any) -> Any:294 return self._generate_min_capability(capability)295def decode_set_space(data: Any) -> Any:296 """297 not sure what's reason, __post_init__ won't be called automatically.298 So write this decoder to force it's called on deserializing299 """300 result = None301 if data:302 result = SetSpace.schema().load(data) # type: ignore303 return result304def decode_set_space_by_type(305 data: Any, base_type: Type[T]306) -> Optional[Union[SetSpace[T], T]]:307 if isinstance(data, dict):308 new_data = SetSpace[T](is_allow_set=True)...
nvme.py
Source:nvme.py
...98 search_space.check_countspace(self.disk_count, capability.disk_count),99 "disk_count",100 )101 return result102 def _generate_min_capability(self, capability: Any) -> Any:103 assert isinstance(capability, NvmeSettings), f"actual: {type(capability)}"104 min_value = NvmeSettings()105 if self.disk_count or capability.disk_count:106 min_value.disk_count = search_space.generate_min_capability_countspace(107 self.disk_count, capability.disk_count108 )...
hibernation.py
Source:hibernation.py
...15 def __hash__(self) -> int:16 return hash(self._get_key())17 def _get_key(self) -> str:18 return f"{self.type}/{self.is_enabled}"19 def _generate_min_capability(self, capability: Any) -> Any:20 return self21class Hibernation(Feature):22 @classmethod23 def on_before_deployment(cls, *args: Any, **kwargs: Any) -> None:24 settings = cast(HibernationSettings, kwargs.get("settings"))25 if settings.is_enabled:26 cls._enable_hibernation(*args, **kwargs)27 @classmethod28 def name(cls) -> str:29 return FEATURE_NAME_HIBERNATION30 @classmethod31 def settings_type(cls) -> Type[schema.FeatureSettings]:32 return HibernationSettings33 @classmethod...
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!!