本文整理汇总了Python中hypothesis.strategies.dates方法的典型用法代码示例。如果您正苦于以下问题:Python strategies.dates方法的具体用法?Python strategies.dates怎么用?Python strategies.dates使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hypothesis.strategies
的用法示例。
在下文中一共展示了strategies.dates方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: primitives
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import dates [as 别名]
def primitives():
return (
st.integers() |
st.floats(allow_nan=False) |
st.text() |
st.binary() |
st.datetimes(timezones=timezones() | st.none()) |
st.dates() |
st.times(timezones=timezones() | st.none()) |
st.timedeltas() |
st.booleans() |
st.none()
)
示例2: rfc3339
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import dates [as 别名]
def rfc3339(name: str) -> st.SearchStrategy[str]:
"""Get a strategy for date or time strings in the given RFC3339 format.
See https://tools.ietf.org/html/rfc3339#section-5.6
"""
# Hmm, https://github.com/HypothesisWorks/hypothesis/issues/170
# would make this a lot easier...
assert name in RFC3339_FORMATS
def zfill(width: int) -> Callable[[int], str]:
return lambda v: str(v).zfill(width)
simple = {
"date-fullyear": st.integers(0, 9999).map(zfill(4)),
"date-month": st.integers(1, 12).map(zfill(2)),
"date-mday": st.integers(1, 28).map(zfill(2)), # incomplete but valid
"time-hour": st.integers(0, 23).map(zfill(2)),
"time-minute": st.integers(0, 59).map(zfill(2)),
"time-second": st.integers(0, 59).map(zfill(2)), # ignore negative leap seconds
"time-secfrac": st.from_regex(r"\.[0-9]+"),
}
if name in simple:
return simple[name]
if name == "time-numoffset":
return st.tuples(
st.sampled_from(["+", "-"]), rfc3339("time-hour"), rfc3339("time-minute")
).map("%s%s:%s".__mod__)
if name == "time-offset":
return st.one_of(st.just("Z"), rfc3339("time-numoffset"))
if name == "partial-time":
return st.times().map(str)
if name == "date" or name == "full-date":
return st.dates().map(str)
if name == "time" or name == "full-time":
return st.tuples(rfc3339("partial-time"), rfc3339("time-offset")).map("".join)
assert name == "date-time"
return st.tuples(rfc3339("full-date"), rfc3339("full-time")).map("T".join)
示例3: get_numpy_array_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import dates [as 别名]
def get_numpy_array_strategy(
shape=10, exclude_dtypes=None, unique=False, sort=False, allow_nan=True
):
# the text example generation has quite some overhead when called the first time.
# we don't want this in our test sample generation since the HealthCheck of hypothesis
# might be triggered.
with catch_warnings():
simplefilter("ignore", NonInteractiveExampleWarning)
hyp_st.text().example()
dtype_strategy = get_scalar_dtype_strategy(exclude_dtypes)
array_strategy = hyp_np.arrays(dtype=dtype_strategy, shape=shape, unique=unique)
if exclude_dtypes is None or "date" not in exclude_dtypes:
date_start = hyp_st.lists(
hyp_st.dates(
min_value=datetime.date(1970, 1, 1), max_value=datetime.date(2100, 1, 1)
),
min_size=shape,
max_size=shape,
unique=unique,
)
date_start = date_start.map(np.array)
one_of_strategies = [array_strategy] + [date_start]
array_strategy = hyp_st.one_of(one_of_strategies)
def _restrict_datetime_ranges(arr):
if np.issubdtype(arr.dtype, np.datetime64):
return all(
(arr < np.datetime64("2200-01-01"))
& (arr > np.datetime64("1970-01-01"))
)
return True
if exclude_dtypes is None or "datetime" not in exclude_dtypes:
array_strategy = array_strategy.filter(_restrict_datetime_ranges)
if not allow_nan:
def _check_for_nan(arr):
if np.issubdtype(arr.dtype, np.floating):
return not any(np.isnan(arr))
return True
array_strategy = array_strategy.filter(_check_for_nan)
if unique and allow_nan:
def _maximum_single_nan(arr):
if np.issubdtype(arr.dtype, np.floating):
return sum(np.isnan(arr)) <= 1
return True
array_strategy = array_strategy.filter(_maximum_single_nan)
if sort:
array_strategy = array_strategy.map(np.sort)
return array_strategy