def run_test(self): # set messages elapsed_time_text = str( humanfriendly.Timer(self.backup.specs.get("mtime")).elapsed_time) self.success_message = "File last modification time correct ( " + elapsed_time_text + " >= " + str( self.params) + " )." self.error_message = "File last modification time INCORRECT ( " + elapsed_time_text + " < " + str( self.params) + " )." # set result self.result = humanfriendly.Timer( self.backup.specs.get("mtime")).elapsed_time >= self.params
def run_test(self): elapsed_time_text = str( humanfriendly.Timer( self.backup.specs.get("newest_entry_mtime")).elapsed_time) self.success_message = "Newest entry (" + self.newest_entry + ") last modification time correct ( " + elapsed_time_text + " <= " + str( self.params["max_age"]) + " )." self.error_message = "Newest entry (" + self.newest_entry + ") last modification time INCORRECT ( " + elapsed_time_text + " > " + str( self.params["max_age"]) + " )." # set result self.result = humanfriendly.Timer( self.backup.specs.get( "newest_entry_mtime")).elapsed_time <= self.params
def test_timer(self): """Test :func:`humanfriendly.Timer`.""" for seconds, text in ((1, '1 second'), (2, '2 seconds'), (60, '1 minute'), (60 * 2, '2 minutes'), (60 * 60, '1 hour'), (60 * 60 * 2, '2 hours'), (60 * 60 * 24, '1 day'), (60 * 60 * 24 * 2, '2 days'), (60 * 60 * 24 * 7, '1 week'), (60 * 60 * 24 * 7 * 2, '2 weeks')): t = humanfriendly.Timer(time.time() - seconds) self.assertEqual(humanfriendly.round_number(t.elapsed_time, keep_width=True), '%i.00' % seconds) self.assertEqual(str(t), text) # Test rounding to seconds. t = humanfriendly.Timer(time.time() - 2.2) self.assertEqual(t.rounded, '2 seconds') # Test automatic timer. automatic_timer = humanfriendly.Timer() time.sleep(1) self.assertEqual(normalize_timestamp(humanfriendly.round_number( automatic_timer.elapsed_time, keep_width=True, )), '1.00') # Test resumable timer. resumable_timer = humanfriendly.Timer(resumable=True) for i in range(2): with resumable_timer: time.sleep(1) self.assertEqual(normalize_timestamp(humanfriendly.round_number( resumable_timer.elapsed_time, keep_width=True, )), '2.00') # Make sure Timer.__enter__() returns the timer object. with humanfriendly.Timer(resumable=True) as timer: assert timer is not None
def dl_all(start, end): page = start while True: print("Downloading page {}...".format(page)) try: raw_quotes = dl_page(page) except (requests.ConnectionError, RequestError): print("Network error. Stopping.") break for quote in raw_quotes: q = parse_quote(quote) status = add_quote(q) print("Done, go to the next page.") page += 1 if page == end: print("Finished!") exit(0) with humanfriendly.Spinner( label="Wait before moving to the next page...", timer=humanfriendly.Timer()) as s: for i in range(25): time.sleep(0.2) s.step() return
def timed(*args, **kw): hf_timer = hf.Timer() result = method(*args, **kw) print("<Timeit> {!r} ({!r}, {!r}) {}".format(method.__name__, args, kw, hf_timer.rounded)) return result