Best Python code snippet using pandera_python
strategies.py
Source:strategies.py
...177# Values taken from178# https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/numpy.html#from_dtype # noqa179MIN_DT_VALUE = -(2**63)180MAX_DT_VALUE = 2**63 - 1181def _is_datetime_tz(pandera_dtype: DataType) -> bool:182 native_type = getattr(pandera_dtype, "type", None)183 return isinstance(native_type, pd.DatetimeTZDtype)184def _datetime_strategy(185 dtype: Union[np.dtype, pd.DatetimeTZDtype], strategy186) -> SearchStrategy:187 if isinstance(dtype, pd.DatetimeTZDtype):188 def _to_datetime(value) -> pd.DatetimeTZDtype:189 if isinstance(value, pd.Timestamp):190 return value.tz_convert(tz=dtype.tz) # type: ignore[union-attr]191 return pd.Timestamp(value, unit=dtype.unit, tz=dtype.tz) # type: ignore[union-attr]192 return st.builds(_to_datetime, strategy)193 else:194 res = (195 st.just(dtype.str.split("[")[-1][:-1])196 if "[" in dtype.str197 else st.sampled_from(npst.TIME_RESOLUTIONS)198 )199 return st.builds(dtype.type, strategy, res)200def numpy_time_dtypes(201 dtype: Union[np.dtype, pd.DatetimeTZDtype], min_value=None, max_value=None202):203 """Create numpy strategy for datetime and timedelta data types.204 :param dtype: numpy datetime or timedelta datatype205 :param min_value: minimum value of the datatype to create206 :param max_value: maximum value of the datatype to create207 :returns: ``hypothesis`` strategy208 """209 def _to_unix(value: Any) -> int:210 if dtype.type is np.timedelta64:211 return pd.Timedelta(value).value212 return pd.Timestamp(value).value213 min_value = MIN_DT_VALUE if min_value is None else _to_unix(min_value)214 max_value = MAX_DT_VALUE if max_value is None else _to_unix(max_value)215 return _datetime_strategy(dtype, st.integers(min_value, max_value))216def numpy_complex_dtypes(217 dtype,218 min_value: complex = complex(0, 0),219 max_value: Optional[complex] = None,220 allow_infinity: bool = None,221 allow_nan: bool = None,222):223 """Create numpy strategy for complex numbers.224 :param dtype: numpy complex number datatype225 :param min_value: minimum value, must be complex number226 :param max_value: maximum value, must be complex number227 :returns: ``hypothesis`` strategy228 """229 max_real: Optional[float]230 max_imag: Optional[float]231 if max_value:232 max_real = max_value.real233 max_imag = max_value.imag234 else:235 max_real = max_imag = None236 if dtype.itemsize == 8:237 width = 32238 else:239 width = 64240 # switch min and max values for imaginary if min value > max value241 if max_imag is not None and min_value.imag > max_imag:242 min_imag = max_imag243 max_imag = min_value.imag244 else:245 min_imag = min_value.imag246 strategy = st.builds(247 complex,248 st.floats(249 min_value=min_value.real,250 max_value=max_real,251 width=width,252 allow_infinity=allow_infinity,253 allow_nan=allow_nan,254 ),255 st.floats(256 min_value=min_imag,257 max_value=max_imag,258 width=width,259 allow_infinity=allow_infinity,260 allow_nan=allow_nan,261 ),262 ).map(dtype.type)263 @st.composite264 def build_complex(draw):265 value = draw(strategy)266 hypothesis.assume(min_value <= value)267 if max_value is not None:268 hypothesis.assume(max_value >= value)269 return value270 return build_complex()271def to_numpy_dtype(pandera_dtype: DataType):272 """Convert a :class:`~pandera.dtypes.DataType` to numpy dtype compatible273 with hypothesis."""274 try:275 np_dtype = pandas_engine.Engine.numpy_dtype(pandera_dtype)276 except TypeError as err:277 if is_datetime(pandera_dtype):278 return np.dtype("datetime64[ns]")279 raise TypeError(280 f"Data generation for the '{pandera_dtype}' data type is "281 "currently unsupported."282 ) from err283 if np_dtype == np.dtype("object") or str(pandera_dtype) == "str":284 np_dtype = np.dtype(str)285 return np_dtype286def pandas_dtype_strategy(287 pandera_dtype: DataType,288 strategy: Optional[SearchStrategy] = None,289 **kwargs,290) -> SearchStrategy:291 # pylint: disable=line-too-long,no-else-raise292 """Strategy to generate data from a :class:`pandera.dtypes.DataType`.293 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.294 :param strategy: an optional hypothesis strategy. If specified, the295 pandas dtype strategy will be chained onto this strategy.296 :kwargs: key-word arguments passed into297 `hypothesis.extra.numpy.from_dtype <https://hypothesis.readthedocs.io/en/latest/numpy.html#hypothesis.extra.numpy.from_dtype>`_ .298 For datetime, timedelta, and complex number datatypes, these arguments299 are passed into :func:`~pandera.strategies.numpy_time_dtypes` and300 :func:`~pandera.strategies.numpy_complex_dtypes`.301 :returns: ``hypothesis`` strategy302 """303 def compat_kwargs(*args):304 return {k: v for k, v in kwargs.items() if k in args}305 # hypothesis doesn't support categoricals or objects, so we'll will need to306 # build a pandera-specific solution.307 if is_category(pandera_dtype):308 raise TypeError(309 "data generation for the Category dtype is currently "310 "unsupported. Consider using a string or int dtype and "311 "Check.isin(values) to ensure a finite set of values."312 )313 np_dtype = to_numpy_dtype(pandera_dtype)314 if strategy:315 if _is_datetime_tz(pandera_dtype):316 return _datetime_strategy(pandera_dtype.type, strategy) # type: ignore317 return strategy.map(np_dtype.type)318 elif is_datetime(pandera_dtype) or is_timedelta(pandera_dtype):319 return numpy_time_dtypes(320 pandera_dtype.type if _is_datetime_tz(pandera_dtype) else np_dtype, # type: ignore321 **compat_kwargs("min_value", "max_value"),322 )323 elif is_complex(pandera_dtype):324 return numpy_complex_dtypes(325 np_dtype,326 **compat_kwargs(327 "min_value", "max_value", "allow_infinity", "allow_nan"328 ),329 )330 return npst.from_dtype(331 np_dtype,332 **{ # type: ignore333 "allow_nan": False,334 "allow_infinity": False,335 **kwargs,336 },337 )338def eq_strategy(339 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],340 strategy: Optional[SearchStrategy] = None,341 *,342 value: Any,343) -> SearchStrategy:344 """Strategy to generate a single value.345 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.346 :param strategy: an optional hypothesis strategy. If specified, the347 pandas dtype strategy will be chained onto this strategy.348 :param value: value to generate.349 :returns: ``hypothesis`` strategy350 """351 # override strategy preceding this one and generate value of the same type352 # pylint: disable=unused-argument353 return pandas_dtype_strategy(pandera_dtype, st.just(value))354def ne_strategy(355 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],356 strategy: Optional[SearchStrategy] = None,357 *,358 value: Any,359) -> SearchStrategy:360 """Strategy to generate anything except for a particular value.361 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.362 :param strategy: an optional hypothesis strategy. If specified, the363 pandas dtype strategy will be chained onto this strategy.364 :param value: value to avoid.365 :returns: ``hypothesis`` strategy366 """367 if strategy is None:368 strategy = pandas_dtype_strategy(pandera_dtype)369 return strategy.filter(lambda x: x != value)370def gt_strategy(371 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],372 strategy: Optional[SearchStrategy] = None,373 *,374 min_value: Union[int, float],375) -> SearchStrategy:376 """Strategy to generate values greater than a minimum value.377 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.378 :param strategy: an optional hypothesis strategy. If specified, the379 pandas dtype strategy will be chained onto this strategy.380 :param min_value: generate values larger than this.381 :returns: ``hypothesis`` strategy382 """383 if strategy is None:384 strategy = pandas_dtype_strategy(385 pandera_dtype,386 min_value=min_value,387 exclude_min=True if is_float(pandera_dtype) else None,388 )389 return strategy.filter(lambda x: x > min_value)390def ge_strategy(391 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],392 strategy: Optional[SearchStrategy] = None,393 *,394 min_value: Union[int, float],395) -> SearchStrategy:396 """Strategy to generate values greater than or equal to a minimum value.397 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.398 :param strategy: an optional hypothesis strategy. If specified, the399 pandas dtype strategy will be chained onto this strategy.400 :param min_value: generate values greater than or equal to this.401 :returns: ``hypothesis`` strategy402 """403 if strategy is None:404 return pandas_dtype_strategy(405 pandera_dtype,406 min_value=min_value,407 exclude_min=False if is_float(pandera_dtype) else None,408 )409 return strategy.filter(lambda x: x >= min_value)410def lt_strategy(411 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],412 strategy: Optional[SearchStrategy] = None,413 *,414 max_value: Union[int, float],415) -> SearchStrategy:416 """Strategy to generate values less than a maximum value.417 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.418 :param strategy: an optional hypothesis strategy. If specified, the419 pandas dtype strategy will be chained onto this strategy.420 :param max_value: generate values less than this.421 :returns: ``hypothesis`` strategy422 """423 if strategy is None:424 strategy = pandas_dtype_strategy(425 pandera_dtype,426 max_value=max_value,427 exclude_max=True if is_float(pandera_dtype) else None,428 )429 return strategy.filter(lambda x: x < max_value)430def le_strategy(431 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],432 strategy: Optional[SearchStrategy] = None,433 *,434 max_value: Union[int, float],435) -> SearchStrategy:436 """Strategy to generate values less than or equal to a maximum value.437 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.438 :param strategy: an optional hypothesis strategy. If specified, the439 pandas dtype strategy will be chained onto this strategy.440 :param max_value: generate values less than or equal to this.441 :returns: ``hypothesis`` strategy442 """443 if strategy is None:444 return pandas_dtype_strategy(445 pandera_dtype,446 max_value=max_value,447 exclude_max=False if is_float(pandera_dtype) else None,448 )449 return strategy.filter(lambda x: x <= max_value)450def in_range_strategy(451 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],452 strategy: Optional[SearchStrategy] = None,453 *,454 min_value: Union[int, float],455 max_value: Union[int, float],456 include_min: bool = True,457 include_max: bool = True,458) -> SearchStrategy:459 """Strategy to generate values within a particular range.460 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.461 :param strategy: an optional hypothesis strategy. If specified, the462 pandas dtype strategy will be chained onto this strategy.463 :param min_value: generate values greater than this.464 :param max_value: generate values less than this.465 :param include_min: include min_value in generated data.466 :param include_max: include max_value in generated data.467 :returns: ``hypothesis`` strategy468 """469 if strategy is None:470 return pandas_dtype_strategy(471 pandera_dtype,472 min_value=min_value,473 max_value=max_value,474 exclude_min=not include_min,475 exclude_max=not include_max,476 )477 min_op = operator.ge if include_min else operator.gt478 max_op = operator.le if include_max else operator.lt479 return strategy.filter(480 lambda x: min_op(x, min_value) and max_op(x, max_value)481 )482def isin_strategy(483 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],484 strategy: Optional[SearchStrategy] = None,485 *,486 allowed_values: Sequence[Any],487) -> SearchStrategy:488 """Strategy to generate values within a finite set.489 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.490 :param strategy: an optional hypothesis strategy. If specified, the491 pandas dtype strategy will be chained onto this strategy.492 :param allowed_values: set of allowable values.493 :returns: ``hypothesis`` strategy494 """495 if strategy is None:496 return pandas_dtype_strategy(497 pandera_dtype, st.sampled_from(allowed_values)498 )499 return strategy.filter(lambda x: x in allowed_values)500def notin_strategy(501 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],502 strategy: Optional[SearchStrategy] = None,503 *,504 forbidden_values: Sequence[Any],505) -> SearchStrategy:506 """Strategy to generate values excluding a set of forbidden values507 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.508 :param strategy: an optional hypothesis strategy. If specified, the509 pandas dtype strategy will be chained onto this strategy.510 :param forbidden_values: set of forbidden values.511 :returns: ``hypothesis`` strategy512 """513 if strategy is None:514 strategy = pandas_dtype_strategy(pandera_dtype)515 return strategy.filter(lambda x: x not in forbidden_values)516def str_matches_strategy(517 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],518 strategy: Optional[SearchStrategy] = None,519 *,520 pattern: str,521) -> SearchStrategy:522 """Strategy to generate strings that patch a regex pattern.523 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.524 :param strategy: an optional hypothesis strategy. If specified, the525 pandas dtype strategy will be chained onto this strategy.526 :param pattern: regex pattern.527 :returns: ``hypothesis`` strategy528 """529 if strategy is None:530 return st.from_regex(pattern, fullmatch=True).map(531 to_numpy_dtype(pandera_dtype).type532 )533 def matches(x):534 return re.match(pattern, x)535 return strategy.filter(matches)536def str_contains_strategy(537 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],538 strategy: Optional[SearchStrategy] = None,539 *,540 pattern: str,541) -> SearchStrategy:542 """Strategy to generate strings that contain a particular pattern.543 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.544 :param strategy: an optional hypothesis strategy. If specified, the545 pandas dtype strategy will be chained onto this strategy.546 :param pattern: regex pattern.547 :returns: ``hypothesis`` strategy548 """549 if strategy is None:550 return st.from_regex(pattern, fullmatch=False).map(551 to_numpy_dtype(pandera_dtype).type552 )553 def contains(x):554 return re.search(pattern, x)555 return strategy.filter(contains)556def str_startswith_strategy(557 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],558 strategy: Optional[SearchStrategy] = None,559 *,560 string: str,561) -> SearchStrategy:562 """Strategy to generate strings that start with a specific string pattern.563 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.564 :param strategy: an optional hypothesis strategy. If specified, the565 pandas dtype strategy will be chained onto this strategy.566 :param string: string pattern.567 :returns: ``hypothesis`` strategy568 """569 if strategy is None:570 return st.from_regex(f"\\A{string}", fullmatch=False).map(571 to_numpy_dtype(pandera_dtype).type572 )573 return strategy.filter(lambda x: x.startswith(string))574def str_endswith_strategy(575 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],576 strategy: Optional[SearchStrategy] = None,577 *,578 string: str,579) -> SearchStrategy:580 """Strategy to generate strings that end with a specific string pattern.581 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.582 :param strategy: an optional hypothesis strategy. If specified, the583 pandas dtype strategy will be chained onto this strategy.584 :param string: string pattern.585 :returns: ``hypothesis`` strategy586 """587 if strategy is None:588 return st.from_regex(f"{string}\\Z", fullmatch=False).map(589 to_numpy_dtype(pandera_dtype).type590 )591 return strategy.filter(lambda x: x.endswith(string))592def str_length_strategy(593 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],594 strategy: Optional[SearchStrategy] = None,595 *,596 min_value: int,597 max_value: int,598) -> SearchStrategy:599 """Strategy to generate strings of a particular length600 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.601 :param strategy: an optional hypothesis strategy. If specified, the602 pandas dtype strategy will be chained onto this strategy.603 :param min_value: minimum string length.604 :param max_value: maximum string length.605 :returns: ``hypothesis`` strategy606 """607 if strategy is None:608 return st.text(min_size=min_value, max_size=max_value).map(609 to_numpy_dtype(pandera_dtype).type610 )611 return strategy.filter(lambda x: min_value <= len(x) <= max_value)612def _timestamp_to_datetime64_strategy(613 strategy: SearchStrategy,614) -> SearchStrategy:615 """Convert timestamp to numpy.datetime64616 Hypothesis only supports pure numpy dtypes but numpy.datetime64() truncates617 nanoseconds if given a pandas.Timestamp. We need to pass the unix epoch via618 the pandas.Timestamp.value attribute.619 """620 return st.builds(lambda x: np.datetime64(x.value, "ns"), strategy)621def field_element_strategy(622 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],623 strategy: Optional[SearchStrategy] = None,624 *,625 checks: Optional[Sequence] = None,626) -> SearchStrategy:627 """Strategy to generate elements of a column or index.628 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.629 :param strategy: an optional hypothesis strategy. If specified, the630 pandas dtype strategy will be chained onto this strategy.631 :param checks: sequence of :class:`~pandera.checks.Check` s to constrain632 the values of the data in the column/index.633 :returns: ``hypothesis`` strategy634 """635 if strategy:636 raise BaseStrategyOnlyError(637 "The series strategy is a base strategy. You cannot specify the "638 "strategy argument to chain it to a parent strategy."639 )640 checks = [] if checks is None else checks641 elements = None642 def undefined_check_strategy(elements, check):643 """Strategy for checks with undefined strategies."""644 warnings.warn(645 "Element-wise check doesn't have a defined strategy."646 "Falling back to filtering drawn values based on the check "647 "definition. This can considerably slow down data-generation."648 )649 return (650 pandas_dtype_strategy(pandera_dtype)651 if elements is None652 else elements653 ).filter(check._check_fn)654 for check in checks:655 if hasattr(check, "strategy"):656 elements = check.strategy(pandera_dtype, elements)657 elif check.element_wise:658 elements = undefined_check_strategy(elements, check)659 # NOTE: vectorized checks with undefined strategies should be handled660 # by the series/dataframe strategy.661 if elements is None:662 elements = pandas_dtype_strategy(pandera_dtype)663 # Hypothesis only supports pure numpy datetime64 (i.e. timezone naive).664 # We cast to datetime64 after applying the check strategy so that checks665 # can see timezone-aware values.666 if _is_datetime_tz(pandera_dtype):667 elements = _timestamp_to_datetime64_strategy(elements)668 return elements669def series_strategy(670 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],671 strategy: Optional[SearchStrategy] = None,672 *,673 checks: Optional[Sequence] = None,674 nullable: bool = False,675 unique: bool = False,676 name: Optional[str] = None,677 size: Optional[int] = None,678):679 """Strategy to generate a pandas Series.680 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance....
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!!