class WebsiteMonitor: def __init__(self, url, checkInterval): self.url = url self.checkInterval = checkInterval # calculating the number of checks in an hour to initialize the history maxNumberOfChecksSaved = int(round(3600.0 / checkInterval)) self.checksHistory = History(maxNumberOfChecksSaved) # the history may be used by other threads, thus we protect it with a lock self.historyLock = threading.Lock() # interval keeps track of the Interval instance that execute self.check every self.checkInterval self.interval = None # keeps track of when the monitor was launched self.startedMonitoringTime = time.time() def check(self): # HTTP HEAD request to the url of the monitored website # measurez success = True checkTime = time.time() try: r = requests.head(self.url, timeout=min(self.checkInterval, 5.0)) except requests.exceptions.RequestException: success = False responseTime = time.time() - checkTime with self.historyLock: self.checksHistory.add(( -1 if not (success) else r.status_code, checkTime, -1.0 if not (success) else responseTime, )) def startMonitoring(self): self.startedMonitoringTime = time.time() self.interval = Interval(self.checkInterval, self.check) def stopMonitoring(self): if self.interval != None: self.interval.cancel() self.interval = None ##### # THREAD SAFE GETTER WITH CONDITION ##### def getCheckHistoryForThePast(self, seconds): t = time.time() with self.historyLock: # check[1] = timestamp of the check return self.checksHistory.getValuesUntilCondition( lambda check: t - check[1] > seconds)
def main(): tab = Tab() history = History() tab.setUrl("http://tutorialspoint.com") history.add(tab.saveUrl()) tab.setUrl("https://www.python.org/doc/") history.add(tab.saveUrl()) tab.setUrl("http://www.trome.com.pe") print("Current web url: " + tab.getUrl()) print("Back button is clicked.. ") tab.restoreFromSavedUrl(history.get(1)) print("Saved url: " + tab.getUrl()) print("Back button again.. ") tab.restoreFromSavedUrl(history.get(0)) print("Saved url: " + tab.getUrl())
# creating necessary objects log = History() menu = Menu() reader = InputReader() sub = Integrals() myHelp = Help() while True: selection = menu.select() # if selection == 'q': # Quiting the main menu break elif selection == '1': # choosing single integral option result = sub.singleIntegral() log.add(result) elif selection == '2': # choosing many integrals results = sub.manyIntegrals() log.add(results) elif selection == '3': # choosing history log.show() raw_input("Press any key to close...") elif selection == '4': # choosing help myHelp.show() raw_input("Press any key to close...") if not(log.isEmpty()): # displays this session's history only if there is something to show log.show() if reader.yesNoInput("Save history to file?"): log.saveToFile()