def acquire_rates_data(self): """Request rates data from the site and processes them returns True on successful receiving and data processing """ prinf('%s params: %s', self.base_url, self.my_params) g_start() try: self.response_data = requests.get(self.base_url, params=self.my_params, timeout=self.timeout) except OSError: prinw('%s host not available', self.name) return False g_end('request responded') if not self.response_data: return False else: status_code = self.response_data.status_code prinf(status_code ) if status_code > 400 : prinw('%s currency converter site response not found. %s', self.nam, status_code) return False elif status_code == 200: prinf('%s response ok', self.name) self.update_rates_valid_data() self.in_ccode = self.response_data.json()[self.strs[jpn.key_in_ccode]] self.rates = self.response_data.json()[self.strs[jpn.key_output]] return True
def g_end(text, timer_name=None): """Ends a global timer measurement and prinfs it""" if timer_name is None: timer_name = 'default' if timer_name not in timers.keys(): prinw('%s timer_name not started yet.', timer_name) return -1 sec = time.time() - timers[timer_name] ms = round(sec * 1000, 2) prinf('%s ms %s', ms, text) return ms
def setup_redis(use_redis=True, use_redis_on_windows=False): """The platform detection and redis usage decision tree is hereby created as on tested Windows 7 64bit (MSOpenTech 3.2.100) there was one second time penalty on first contact with redis """ truly_use_redis = use_redis if platform.system() == 'Windows': truly_use_redis = use_redis_on_windows prinw( 'Not using redis - Windows platform has 1 sec latency on first contact!' ) return truly_use_redis
def touch_db_file(self): """Tries to create empty database file at the desired location if unsuccessfully it disables sql database usage - the exchange rates from cc-sites will not be stored """ if not self.db_file_exist(): # create empty database file open(self.db_file, 'a').close() if not self.db_file_exist(): prinw( """Offline database file is not present, even after creation attempt.""" """\nPlease create empty file in this path: <%s>""" """\nWorking without offline sql database can be slow - for every request there is internet connection needed""", str(Path(self.db_file))) self.use_sql = False return None
def draw_walls(self): Ww, Wh = Wsize = Window.size prinw(Wsize) self.root.info(Wsize) self.wall_id = 0 # thickness t = 125 txu = "warning" x0, y0 = 30, 30 # w0, h0 = Ww-2*x0, Wh-2*y0 w0, h0 = self.root.field_size sizes = [(w0, t), (t, h0 + 2 * t), (w0, t), (t, h0 + 2 * t)] poss = [[0, -t], [w0, -t], [0, h0], [-t, -t]] poss = [[pos[0] + x0, pos[1] + y0] for pos in poss] for pos, size in zip(poss, sizes): self.create_wall(pos, size, txu)
def get_db_valid_from_via_txt(self): """Returns offline sql database valid_from utc time from txt file [rates_info_txt_file]""" rates_info_txt_exist = Path(self.rates_info_txt_file).is_file() if rates_info_txt_exist: g_start() with open(self.rates_info_txt_file, 'r', encoding=self.encoding) as f: whole = f.readlines() if len(whole) < 2: prinw( 'rates_info_txt_file does not contain lines with db_valid_to_str, db_valid_from_str!' ) return None else: db_valid_to_str, db_valid_from_str = whole[0:2] db_valid_from = arrow.get(db_valid_from_str) g_end('loaded rates_info_txt_file') # ~ 8 ms return db_valid_from else: prinw('rates_info_txt_file does not exist') return None
def update_rates_data_if_needed(self): """Main decision tree for rates database update from internet""" if self.valid_database_exists: # we need only new data (fresher then database) only_sites_fresher_than_db = True else: # no database - just try to get data only_sites_fresher_than_db = False # get sites ordered retrospectively from the one with freshest last_updated utc time sorted_sites = self.get_retrospectively_sorted_sites( only_sites_fresher_than_db) if len(sorted_sites) == 0 and self.valid_database_exists: prinf('No fresher site than database - using sql database rates.') return # try to update offline database starting with the freshest any_success = self.try_update_database_from_sites(sorted_sites) if not any_success: prinw( 'Conversion rates data were not available in any cconversion site!' )
def update_database_from_site(self, site): """Requires exchange rates data from the site and processes them - requires all rates data from the currency conversion site - creates pandas representation of all the exchange rates - saves them to the sql database returns - tuple of: [update_success] = True on successful data processing, False otherwise [response_success] = True when successfully received data, False otherwise """ response_success = site.get_all_rates() if not response_success: return False, response_success rates_df, rates_info_lines = self.create_rates_table_in_pandas(site) update_success = True if self.use_sql: self.save_to_sql(rates_df, rates_info_lines) else: prinw('Not using offline sql database!') return update_success, response_success