diff --git a/schedule/__init__.py b/schedule/__init__.py index 2db9b0ad..df912de3 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -334,6 +334,17 @@ def seconds(self): self.unit = "seconds" return self + @property + def millisecond(self): + if self.interval != 1: + raise IntervalError("Use milliseconds instead of millisecond") + return self.milliseconds + + @property + def milliseconds(self): + self.unit = "milliseconds" + return self + @property def minute(self): if self.interval != 1: @@ -703,9 +714,9 @@ def _schedule_next_run(self) -> None: """ Compute the instant when this job should run next. """ - if self.unit not in ("seconds", "minutes", "hours", "days", "weeks"): + if self.unit not in ("milliseconds", "seconds", "minutes", "hours", "days", "weeks"): raise ScheduleValueError( - "Invalid unit (valid units are `seconds`, `minutes`, `hours`, " + "Invalid unit (valid units are `millisecond`, `seconds`, `minutes`, `hours`, " "`days`, and `weeks`)" ) diff --git a/setup.py b/setup.py index a56d374a..1f5df096 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup -SCHEDULE_VERSION = "1.2.0" +SCHEDULE_VERSION = "1.2.1" SCHEDULE_DOWNLOAD_URL = "https://github.com/dbader/schedule/tarball/" + SCHEDULE_VERSION diff --git a/test_schedule.py b/test_schedule.py index 7d4617ec..684451b6 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -35,13 +35,14 @@ class mock_datetime: Monkey-patch datetime for predictable results """ - def __init__(self, year, month, day, hour, minute, second=0): + def __init__(self, year, month, day, hour, minute, second=0, microsecond=0): self.year = year self.month = month self.day = day self.hour = hour self.minute = minute self.second = second + self.microsecond = microsecond self.original_datetime = None def __enter__(self): @@ -59,13 +60,14 @@ def now(cls): self.hour, self.minute, self.second, + self.microsecond, ) self.original_datetime = datetime.datetime datetime.datetime = MockDate return MockDate( - self.year, self.month, self.day, self.hour, self.minute, self.second + self.year, self.month, self.day, self.hour, self.minute, self.second, self.microsecond, ) def __exit__(self, *args, **kwargs): @@ -77,6 +79,7 @@ def setUp(self): schedule.clear() def test_time_units(self): + assert every().milliseconds.unit == "milliseconds" assert every().seconds.unit == "seconds" assert every().minutes.unit == "minutes" assert every().hours.unit == "hours" @@ -211,6 +214,7 @@ def test_next_run_with_tag(self): assert schedule.next_run("tag4") is None def test_singular_time_units_match_plural_units(self): + assert every().millisecond.unit == every().milliseconds.unit assert every().second.unit == every().seconds.unit assert every().minute.unit == every().minutes.unit assert every().hour.unit == every().hours.unit @@ -438,9 +442,15 @@ def test_at_time_minute(self): self.assertRaises(TypeError, every().minute.at, 2) def test_next_run_time(self): - with mock_datetime(2010, 1, 6, 12, 15): + with mock_datetime(2010, 1, 6, 12, 15, 22, 45000): mock_job = make_mock_job() assert schedule.next_run() is None + assert every().millisecond.do(mock_job).next_run.microsecond == 46000 + assert every(100).milliseconds.do(mock_job).next_run.microsecond == 145000 + assert every().second.do(mock_job).next_run.second == 23 + assert every(3).seconds.do(mock_job).next_run.second == 25 + assert every().second.do(mock_job).next_run.second == 23 + assert every(3).seconds.do(mock_job).next_run.second == 25 assert every().minute.do(mock_job).next_run.minute == 16 assert every(5).minutes.do(mock_job).next_run.minute == 20 assert every().hour.do(mock_job).next_run.hour == 13