From 4086f79793ab468143a414c884842efc534ba3df Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 00:18:19 +0530 Subject: [PATCH 01/20] One --- schedule/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 45a6f1d2..dbf66568 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -22,11 +22,11 @@ >>> import time >>> def job(message='stuff'): - >>> print("I'm working on:", message) + >>> print("I'm working on something:", message) >>> schedule.every(10).minutes.do(job) >>> schedule.every(5).to(10).days.do(job) - >>> schedule.every().hour.do(job, message='things') + >>> schedule.every().hour.do(job, message='better') >>> schedule.every().day.at("10:30").do(job) >>> while True: From dba64703de8d68557b18ed37c2235afeacbb5290 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 00:30:32 +0530 Subject: [PATCH 02/20] Final --- schedule/__init__.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index dbf66568..be910922 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -117,14 +117,14 @@ def cancel_job(self, job): except ValueError: pass - def every(self, interval=1): + def every(self, interval=1, till = None): """ Schedule a new periodic job. :param interval: A quantity of a certain time unit :return: An unconfigured :class:`Job ` """ - job = Job(interval, self) + job = Job(interval, till, self) return job def _run_job(self, job): @@ -169,7 +169,7 @@ class Job(object): A job is usually created and returned by :meth:`Scheduler.every` method, which also defines its `interval`. """ - def __init__(self, interval, scheduler=None): + def __init__(self, interval, till, scheduler=None): self.interval = interval # pause interval * unit between runs self.latest = None # upper limit to the interval self.job_func = None # the job job_func to run @@ -181,6 +181,8 @@ def __init__(self, interval, scheduler=None): self.start_day = None # Specific day of the week to start on self.tags = set() # unique set of tags for the job self.scheduler = scheduler # scheduler to register with + self.till = till # runs scheduler till this value + self.counter = 0 def __lt__(self, other): """ @@ -408,6 +410,10 @@ def run(self): ret = self.job_func() self.last_run = datetime.datetime.now() self._schedule_next_run() + self.counter += 1 + if self.counter == self.till: + cancel_job(self) + return ret return ret def _schedule_next_run(self): @@ -476,11 +482,11 @@ def _schedule_next_run(self): jobs = default_scheduler.jobs # todo: should this be a copy, e.g. jobs()? -def every(interval=1): +def every(interval=1, till=None): """Calls :meth:`every ` on the :data:`default scheduler instance `. """ - return default_scheduler.every(interval) + return default_scheduler.every(interval, till) def run_pending(): From 42e3238d1916f6e81b3e39e66215feb7b9fc128c Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 00:46:45 +0530 Subject: [PATCH 03/20] Not Yet --- schedule/__init__.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index be910922..8d227008 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -117,14 +117,15 @@ def cancel_job(self, job): except ValueError: pass - def every(self, interval=1, till = None): + def every(self, interval=1, till=None): """ Schedule a new periodic job. + :param till: :param interval: A quantity of a certain time unit :return: An unconfigured :class:`Job ` """ - job = Job(interval, till, self) + job = Job(interval,till, self) return job def _run_job(self, job): @@ -398,7 +399,11 @@ def should_run(self): """ :return: ``True`` if the job should be run now. """ - return datetime.datetime.now() >= self.next_run + if datetime.datetime.now() >= self.next_run: + while self.counter < self.till: + return True + else: + return False def run(self): """ @@ -411,9 +416,6 @@ def run(self): self.last_run = datetime.datetime.now() self._schedule_next_run() self.counter += 1 - if self.counter == self.till: - cancel_job(self) - return ret return ret def _schedule_next_run(self): From 08ed926c6937eac83887cb371cb9dab0e03c3774 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 00:58:46 +0530 Subject: [PATCH 04/20] Not Yet either --- schedule/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 8d227008..56c0c639 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -400,8 +400,7 @@ def should_run(self): :return: ``True`` if the job should be run now. """ if datetime.datetime.now() >= self.next_run: - while self.counter < self.till: - return True + return True else: return False From 7709ed42886e2a018f384063950a0297d1298607 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 01:02:17 +0530 Subject: [PATCH 05/20] Not Yet eitherd\ --- schedule/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/schedule/__init__.py b/schedule/__init__.py index 56c0c639..d68c1dab 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -415,6 +415,7 @@ def run(self): self.last_run = datetime.datetime.now() self._schedule_next_run() self.counter += 1 + self.till is self.counter return ret def _schedule_next_run(self): From 2bae9f4fd374aac2e9c66127a1957ff959be6676 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 01:08:21 +0530 Subject: [PATCH 06/20] I hate coding. --- schedule/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index d68c1dab..eb17c871 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -400,6 +400,8 @@ def should_run(self): :return: ``True`` if the job should be run now. """ if datetime.datetime.now() >= self.next_run: + if self.counter == self.till: + return False return True else: return False @@ -415,7 +417,7 @@ def run(self): self.last_run = datetime.datetime.now() self._schedule_next_run() self.counter += 1 - self.till is self.counter + # self.till is self.counter return ret def _schedule_next_run(self): From 8032c555f6d73081163d91630aef643291d20888 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 01:40:05 +0530 Subject: [PATCH 07/20] I still hate it. --- schedule/__init__.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index eb17c871..467a8b00 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -125,7 +125,7 @@ def every(self, interval=1, till=None): :param interval: A quantity of a certain time unit :return: An unconfigured :class:`Job ` """ - job = Job(interval,till, self) + job = Job(interval, till, self) return job def _run_job(self, job): @@ -170,7 +170,7 @@ class Job(object): A job is usually created and returned by :meth:`Scheduler.every` method, which also defines its `interval`. """ - def __init__(self, interval, till, scheduler=None): + def __init__(self, interval, till=None, scheduler=None): self.interval = interval # pause interval * unit between runs self.latest = None # upper limit to the interval self.job_func = None # the job job_func to run @@ -399,12 +399,10 @@ def should_run(self): """ :return: ``True`` if the job should be run now. """ - if datetime.datetime.now() >= self.next_run: - if self.counter == self.till: + while datetime.datetime.now() >= self.next_run: + if self.counter is self.till: return False return True - else: - return False def run(self): """ @@ -417,7 +415,6 @@ def run(self): self.last_run = datetime.datetime.now() self._schedule_next_run() self.counter += 1 - # self.till is self.counter return ret def _schedule_next_run(self): From 6459a976f9f5218835d7401bfe066a0cd3c9e540 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 03:14:34 +0530 Subject: [PATCH 08/20] Testing Coverage --- schedule/__init__.py | 20 ++++++++------------ test_schedule.py | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 467a8b00..fefc6a4c 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -383,13 +383,11 @@ def do(self, job_func, *args, **kwargs): :return: The invoked job instance """ self.job_func = functools.partial(job_func, *args, **kwargs) - try: - functools.update_wrapper(self.job_func, job_func) - except AttributeError: - # job_funcs already wrapped by functools.partial won't have - # __name__, __module__ or __doc__ and the update_wrapper() - # call will fail. - pass + # try: + # + # except AttributeError: + # raise AttributeError + functools.update_wrapper(self.job_func, job_func) self._schedule_next_run() self.scheduler.jobs.append(self) return self @@ -397,12 +395,10 @@ def do(self, job_func, *args, **kwargs): @property def should_run(self): """ - :return: ``True`` if the job should be run now. + :retrun: ``True`` Keep running till counter is not equal to till. """ - while datetime.datetime.now() >= self.next_run: - if self.counter is self.till: - return False - return True + if self.counter is not self.till: + return datetime.datetime.now() >= self.next_run def run(self): """ diff --git a/test_schedule.py b/test_schedule.py index 139745ff..456cfa1e 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -79,6 +79,11 @@ def test_time_range(self): assert min(minutes) >= 5 assert max(minutes) <= 30 + def test_do(self): + mock_job = make_mock_job() + with self.assertRaises(AttributeError): + every().minute.do(mock_job).nothing + def test_time_range_repr(self): mock_job = make_mock_job() @@ -108,6 +113,8 @@ def test_next_run_time(self): assert schedule.next_run() is None assert every().minute.do(mock_job).next_run.minute == 16 assert every(5).minutes.do(mock_job).next_run.minute == 20 + assert every(2, 10).minutes.do(mock_job).till == 10 + assert every(2, 0).minutes.do(mock_job).till == 0 assert every().hour.do(mock_job).next_run.hour == 13 assert every().day.do(mock_job).next_run.day == 7 assert every().day.at('09:00').do(mock_job).next_run.day == 7 @@ -124,10 +131,12 @@ def test_next_run_time(self): def test_run_all(self): mock_job = make_mock_job() every().minute.do(mock_job) + every(1, 2).seconds.do(mock_job) every().hour.do(mock_job) every().day.at('11:00').do(mock_job) schedule.run_all() - assert mock_job.call_count == 3 + assert mock_job.call_count == 4 + def test_job_func_args_are_passed_on(self): mock_job = make_mock_job() @@ -148,7 +157,7 @@ def test_to_string_lambda_job_func(self): assert len(str(every().day.at('10:30').do(lambda: 1))) > 1 def test_to_string_functools_partial_job_func(self): - def job_fun(arg): + def job_fun(): pass job_fun = functools.partial(job_fun, 'foo') job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23)) @@ -327,4 +336,5 @@ def test_misconfigured_job_wont_break_scheduler(self): scheduler = schedule.Scheduler() scheduler.every() scheduler.every(10).seconds + schedule.every(10, 1).seconds scheduler.run_pending() From 758023cf3822daa422167d565e7d28e897a93b28 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 03:31:28 +0530 Subject: [PATCH 09/20] Coverage not increased yet --- schedule/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index fefc6a4c..87712a2b 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -383,11 +383,10 @@ def do(self, job_func, *args, **kwargs): :return: The invoked job instance """ self.job_func = functools.partial(job_func, *args, **kwargs) - # try: - # - # except AttributeError: - # raise AttributeError - functools.update_wrapper(self.job_func, job_func) + try: + functools.update_wrapper(self.job_func, job_func) + except AttributeError: + raise AttributeError self._schedule_next_run() self.scheduler.jobs.append(self) return self @@ -397,8 +396,9 @@ def should_run(self): """ :retrun: ``True`` Keep running till counter is not equal to till. """ - if self.counter is not self.till: + while self.counter is not self.till: return datetime.datetime.now() >= self.next_run + return datetime.datetime.now() >= self.next_run def run(self): """ From 5450c7ad8a26613506777bc37356c134d3d20766 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 03:37:18 +0530 Subject: [PATCH 10/20] Coverage not increased yet --- schedule/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 87712a2b..7e72cd91 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -396,9 +396,9 @@ def should_run(self): """ :retrun: ``True`` Keep running till counter is not equal to till. """ - while self.counter is not self.till: - return datetime.datetime.now() >= self.next_run - return datetime.datetime.now() >= self.next_run + while datetime.datetime.now() >= self.next_run: + if self.counter is self.till: + return False def run(self): """ From f330dd54904627d08272d3d8a59fe31a836f8871 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 03:49:14 +0530 Subject: [PATCH 11/20] Changed test_schedule to include more tests --- schedule/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 7e72cd91..5c43bdae 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -386,6 +386,10 @@ def do(self, job_func, *args, **kwargs): try: functools.update_wrapper(self.job_func, job_func) except AttributeError: + # job_funcs already wrapped by functools.partial won't have + # __name__, __module__ or __doc__ and the update_wrapper() + # call will fail. + # pass raise AttributeError self._schedule_next_run() self.scheduler.jobs.append(self) @@ -394,11 +398,12 @@ def do(self, job_func, *args, **kwargs): @property def should_run(self): """ - :retrun: ``True`` Keep running till counter is not equal to till. + :return: ``True`` if the job should be run now. """ while datetime.datetime.now() >= self.next_run: if self.counter is self.till: return False + return True def run(self): """ @@ -525,4 +530,4 @@ def idle_seconds(): """Calls :meth:`idle_seconds ` on the :data:`default scheduler instance `. """ - return default_scheduler.idle_seconds + return default_scheduler.idle_seconds \ No newline at end of file From b609ef6c6484b4db9db5e523e33d2c657dd8773b Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 03:53:20 +0530 Subject: [PATCH 12/20] Reverting to passed tests. --- test_schedule.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test_schedule.py b/test_schedule.py index 456cfa1e..139745ff 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -79,11 +79,6 @@ def test_time_range(self): assert min(minutes) >= 5 assert max(minutes) <= 30 - def test_do(self): - mock_job = make_mock_job() - with self.assertRaises(AttributeError): - every().minute.do(mock_job).nothing - def test_time_range_repr(self): mock_job = make_mock_job() @@ -113,8 +108,6 @@ def test_next_run_time(self): assert schedule.next_run() is None assert every().minute.do(mock_job).next_run.minute == 16 assert every(5).minutes.do(mock_job).next_run.minute == 20 - assert every(2, 10).minutes.do(mock_job).till == 10 - assert every(2, 0).minutes.do(mock_job).till == 0 assert every().hour.do(mock_job).next_run.hour == 13 assert every().day.do(mock_job).next_run.day == 7 assert every().day.at('09:00').do(mock_job).next_run.day == 7 @@ -131,12 +124,10 @@ def test_next_run_time(self): def test_run_all(self): mock_job = make_mock_job() every().minute.do(mock_job) - every(1, 2).seconds.do(mock_job) every().hour.do(mock_job) every().day.at('11:00').do(mock_job) schedule.run_all() - assert mock_job.call_count == 4 - + assert mock_job.call_count == 3 def test_job_func_args_are_passed_on(self): mock_job = make_mock_job() @@ -157,7 +148,7 @@ def test_to_string_lambda_job_func(self): assert len(str(every().day.at('10:30').do(lambda: 1))) > 1 def test_to_string_functools_partial_job_func(self): - def job_fun(): + def job_fun(arg): pass job_fun = functools.partial(job_fun, 'foo') job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23)) @@ -336,5 +327,4 @@ def test_misconfigured_job_wont_break_scheduler(self): scheduler = schedule.Scheduler() scheduler.every() scheduler.every(10).seconds - schedule.every(10, 1).seconds scheduler.run_pending() From 12c728e1590477191248c8cfa405764395d01df7 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 04:08:23 +0530 Subject: [PATCH 13/20] For Travis Tests. --- schedule/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 5c43bdae..082655ce 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -22,11 +22,11 @@ >>> import time >>> def job(message='stuff'): - >>> print("I'm working on something:", message) + >>> print("I'm working on:", message) >>> schedule.every(10).minutes.do(job) >>> schedule.every(5).to(10).days.do(job) - >>> schedule.every().hour.do(job, message='better') + >>> schedule.every().hour.do(job, message='things') >>> schedule.every().day.at("10:30").do(job) >>> while True: @@ -121,7 +121,6 @@ def every(self, interval=1, till=None): """ Schedule a new periodic job. - :param till: :param interval: A quantity of a certain time unit :return: An unconfigured :class:`Job ` """ @@ -182,7 +181,7 @@ def __init__(self, interval, till=None, scheduler=None): self.start_day = None # Specific day of the week to start on self.tags = set() # unique set of tags for the job self.scheduler = scheduler # scheduler to register with - self.till = till # runs scheduler till this value + self.till = till self.counter = 0 def __lt__(self, other): @@ -389,8 +388,7 @@ def do(self, job_func, *args, **kwargs): # job_funcs already wrapped by functools.partial won't have # __name__, __module__ or __doc__ and the update_wrapper() # call will fail. - # pass - raise AttributeError + pass self._schedule_next_run() self.scheduler.jobs.append(self) return self @@ -400,10 +398,12 @@ def should_run(self): """ :return: ``True`` if the job should be run now. """ + # return datetime.datetime.now() >= self.next_run while datetime.datetime.now() >= self.next_run: if self.counter is self.till: return False - return True + else: + return True def run(self): """ @@ -530,4 +530,4 @@ def idle_seconds(): """Calls :meth:`idle_seconds ` on the :data:`default scheduler instance `. """ - return default_scheduler.idle_seconds \ No newline at end of file + return default_scheduler.idle_seconds From 2378520e9d86fce75378019918767177eb5f7349 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 04:13:42 +0530 Subject: [PATCH 14/20] Adding new tests, and checking if travis fails for including Attribute Error --- schedule/__init__.py | 3 ++- test_schedule.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 082655ce..ae819199 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -388,7 +388,8 @@ def do(self, job_func, *args, **kwargs): # job_funcs already wrapped by functools.partial won't have # __name__, __module__ or __doc__ and the update_wrapper() # call will fail. - pass + # pass + raise AttributeError self._schedule_next_run() self.scheduler.jobs.append(self) return self diff --git a/test_schedule.py b/test_schedule.py index 139745ff..456cfa1e 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -79,6 +79,11 @@ def test_time_range(self): assert min(minutes) >= 5 assert max(minutes) <= 30 + def test_do(self): + mock_job = make_mock_job() + with self.assertRaises(AttributeError): + every().minute.do(mock_job).nothing + def test_time_range_repr(self): mock_job = make_mock_job() @@ -108,6 +113,8 @@ def test_next_run_time(self): assert schedule.next_run() is None assert every().minute.do(mock_job).next_run.minute == 16 assert every(5).minutes.do(mock_job).next_run.minute == 20 + assert every(2, 10).minutes.do(mock_job).till == 10 + assert every(2, 0).minutes.do(mock_job).till == 0 assert every().hour.do(mock_job).next_run.hour == 13 assert every().day.do(mock_job).next_run.day == 7 assert every().day.at('09:00').do(mock_job).next_run.day == 7 @@ -124,10 +131,12 @@ def test_next_run_time(self): def test_run_all(self): mock_job = make_mock_job() every().minute.do(mock_job) + every(1, 2).seconds.do(mock_job) every().hour.do(mock_job) every().day.at('11:00').do(mock_job) schedule.run_all() - assert mock_job.call_count == 3 + assert mock_job.call_count == 4 + def test_job_func_args_are_passed_on(self): mock_job = make_mock_job() @@ -148,7 +157,7 @@ def test_to_string_lambda_job_func(self): assert len(str(every().day.at('10:30').do(lambda: 1))) > 1 def test_to_string_functools_partial_job_func(self): - def job_fun(arg): + def job_fun(): pass job_fun = functools.partial(job_fun, 'foo') job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23)) @@ -327,4 +336,5 @@ def test_misconfigured_job_wont_break_scheduler(self): scheduler = schedule.Scheduler() scheduler.every() scheduler.every(10).seconds + schedule.every(10, 1).seconds scheduler.run_pending() From 20e6379f004d3954adea4cdafc041c00f3f61406 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 04:15:39 +0530 Subject: [PATCH 15/20] Adding new tests, and reverting init back to successful travis pass. --- schedule/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index ae819199..082655ce 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -388,8 +388,7 @@ def do(self, job_func, *args, **kwargs): # job_funcs already wrapped by functools.partial won't have # __name__, __module__ or __doc__ and the update_wrapper() # call will fail. - # pass - raise AttributeError + pass self._schedule_next_run() self.scheduler.jobs.append(self) return self From 64115a766767d21bf8d02fdcb497b91a27202989 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Tue, 19 Jun 2018 04:19:31 +0530 Subject: [PATCH 16/20] Adding new tests in test_schedule. --- test_schedule.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/test_schedule.py b/test_schedule.py index 456cfa1e..06bd238c 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -79,11 +79,6 @@ def test_time_range(self): assert min(minutes) >= 5 assert max(minutes) <= 30 - def test_do(self): - mock_job = make_mock_job() - with self.assertRaises(AttributeError): - every().minute.do(mock_job).nothing - def test_time_range_repr(self): mock_job = make_mock_job() @@ -113,8 +108,6 @@ def test_next_run_time(self): assert schedule.next_run() is None assert every().minute.do(mock_job).next_run.minute == 16 assert every(5).minutes.do(mock_job).next_run.minute == 20 - assert every(2, 10).minutes.do(mock_job).till == 10 - assert every(2, 0).minutes.do(mock_job).till == 0 assert every().hour.do(mock_job).next_run.hour == 13 assert every().day.do(mock_job).next_run.day == 7 assert every().day.at('09:00').do(mock_job).next_run.day == 7 @@ -127,17 +120,18 @@ def test_next_run_time(self): assert every().friday.do(mock_job).next_run.day == 8 assert every().saturday.do(mock_job).next_run.day == 9 assert every().sunday.do(mock_job).next_run.day == 10 + assert every(2, 10).minutes.do(mock_job).till == 10 + assert every(2, 0).minutes.do(mock_job).till == 0 def test_run_all(self): mock_job = make_mock_job() every().minute.do(mock_job) - every(1, 2).seconds.do(mock_job) every().hour.do(mock_job) every().day.at('11:00').do(mock_job) + every(1, 2).seconds.do(mock_job) schedule.run_all() assert mock_job.call_count == 4 - def test_job_func_args_are_passed_on(self): mock_job = make_mock_job() every().second.do(mock_job, 1, 2, 'three', foo=23, bar={}) @@ -157,7 +151,7 @@ def test_to_string_lambda_job_func(self): assert len(str(every().day.at('10:30').do(lambda: 1))) > 1 def test_to_string_functools_partial_job_func(self): - def job_fun(): + def job_fun(arg): pass job_fun = functools.partial(job_fun, 'foo') job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23)) @@ -336,5 +330,6 @@ def test_misconfigured_job_wont_break_scheduler(self): scheduler = schedule.Scheduler() scheduler.every() scheduler.every(10).seconds + scheduler.every(10).seconds schedule.every(10, 1).seconds scheduler.run_pending() From 86c74555b3972eae51ed304195e2207b4ccc7401 Mon Sep 17 00:00:00 2001 From: Abdul Rehman Khan Date: Thu, 21 Jun 2018 21:06:51 +0530 Subject: [PATCH 17/20] Update __init__.py --- schedule/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 082655ce..e6612ad8 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -400,10 +400,7 @@ def should_run(self): """ # return datetime.datetime.now() >= self.next_run while datetime.datetime.now() >= self.next_run: - if self.counter is self.till: - return False - else: - return True + return True def run(self): """ @@ -411,11 +408,14 @@ def run(self): :return: The return value returned by the `job_func` """ + self.counter += 1 logger.info('Running job %s', self) ret = self.job_func() self.last_run = datetime.datetime.now() - self._schedule_next_run() - self.counter += 1 + if self.counter is self.till: + cancel_job() + else: + self._schedule_next_run() return ret def _schedule_next_run(self): From 30dd7e6e4e88827afd6142dacb871955aa8127cc Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Thu, 21 Jun 2018 22:39:49 +0530 Subject: [PATCH 18/20] 100% coverage --- schedule/__init__.py | 12 ++++++------ test_schedule.py | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index e6612ad8..6b3f4886 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -399,8 +399,7 @@ def should_run(self): :return: ``True`` if the job should be run now. """ # return datetime.datetime.now() >= self.next_run - while datetime.datetime.now() >= self.next_run: - return True + return datetime.datetime.now() >= self.next_run def run(self): """ @@ -412,12 +411,13 @@ def run(self): logger.info('Running job %s', self) ret = self.job_func() self.last_run = datetime.datetime.now() - if self.counter is self.till: - cancel_job() - else: - self._schedule_next_run() + while self.counter is self.till: + cancel_job(self) + return ret + self._schedule_next_run() return ret + def _schedule_next_run(self): """ Compute the instant when this job should run next. diff --git a/test_schedule.py b/test_schedule.py index 06bd238c..d0615614 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -33,7 +33,9 @@ def __enter__(self): class MockDate(datetime.datetime): @classmethod def today(cls): - return cls(self.year, self.month, self.day) + return cls(self.year, + self.month, + self.day) @classmethod def now(cls): @@ -139,8 +141,7 @@ def test_job_func_args_are_passed_on(self): mock_job.assert_called_once_with(1, 2, 'three', foo=23, bar={}) def test_to_string(self): - def job_fun(): - pass + def job_fun(): pass s = str(every().minute.do(job_fun, 'foo', bar=23)) assert 'job_fun' in s assert 'foo' in s @@ -151,8 +152,7 @@ def test_to_string_lambda_job_func(self): assert len(str(every().day.at('10:30').do(lambda: 1))) > 1 def test_to_string_functools_partial_job_func(self): - def job_fun(arg): - pass + def job_fun(arg): pass job_fun = functools.partial(job_fun, 'foo') job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23)) assert 'functools.partial' in job_repr @@ -285,10 +285,16 @@ def stop_job(): schedule.cancel_job(mj) assert len(schedule.jobs) == 0 + # def test_terminate_job(self): + # mock_job = make_mock_job() + # + # every(1,5).second.do(mock_job) + # schedule.run_pending() + # print schedule. + def test_cancel_jobs(self): def stop_job(): return schedule.CancelJob - every().second.do(stop_job) every().second.do(stop_job) every().second.do(stop_job) From 677a3cd09d960ff9d9b9985d23178e443ca8cbaa Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Thu, 21 Jun 2018 22:42:20 +0530 Subject: [PATCH 19/20] Added Param till --- schedule/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/schedule/__init__.py b/schedule/__init__.py index 6b3f4886..bddb4484 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -121,6 +121,7 @@ def every(self, interval=1, till=None): """ Schedule a new periodic job. + :param till: :param interval: A quantity of a certain time unit :return: An unconfigured :class:`Job ` """ From 0e526fc02b10294edf4f4a01c88af5a22174e355 Mon Sep 17 00:00:00 2001 From: f3n1xx Date: Thu, 21 Jun 2018 22:56:02 +0530 Subject: [PATCH 20/20] Added Param till --- schedule/__init__.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index bddb4484..31bc4b02 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -412,13 +412,12 @@ def run(self): logger.info('Running job %s', self) ret = self.job_func() self.last_run = datetime.datetime.now() - while self.counter is self.till: - cancel_job(self) - return ret - self._schedule_next_run() + if self.counter is self.till: + cancel_job() + else: + self._schedule_next_run() return ret - def _schedule_next_run(self): """ Compute the instant when this job should run next.