def GenerateRates(data: Sequence[Types.DictableRate]) -> Sequence[Rate]: """ Generates Rate objects from a Sequence of rate dictionaries Parameters: data (Sequence) : Sequence of rate dictionaries Returns: rates (Sequence) : Sequence of Rate objects """ if not isinstance(data, Sequence): mLogger.critical( f"GenerateRatesException: data is not type sequence but type '{type(data)}'" ) raise TypeError( "Argument to function GenerateRates must be a of a sequence type not of type '%s'" % type(data)) generatedRates: MutableSequence[Rate] = [] item: Mapping[str, Union[str, float]] for item in data: rate: Rate = Rate(str(item["name"]), str(item["currency_code"]), float(item["from_euro"]), float(item["to_euro"])) generatedRates.append(rate) generatedRates.append(Rate("Euro", "EUR", 1.0, 1.0)) return generatedRates
def __init__(self) -> None: self.__rates: Sequence[Rate] = [] self.__dictableRates: Sequence[Types.DictableRate] = [] self.__timestamp: float = 0 self.__Init() if len(self.__dictableRates) <= 0 or len( self.__rates) <= 0 or self.__timestamp == 0: mLogger.critical( f"PyRatesInitException: {self.__timestamp} {self.__rates} {self.__dictableRates}" ) raise Exception( "PyRatesInitError: Failed to initialize. Check '%s/%s' for further inspection." % (Constants.logPath, Constants.logFileName))
def __init__(self, master: Tk) -> None: if not isinstance(master, Tk): mLogger.critical( f"GUIException: constructor called with '{master}' of type '{type(master)}'" ) raise Exception("Run PyRatesGUI via the static Run() method.") self.__viewState: bool = False self.__master: Tk = master self.__pyrates: PyRates = PyRates() self.__canvas: Canvas = Canvas(self.__master, bg=GUI.backgroundColor, height=GUI.height, width=GUI.width) self.__canvas.pack() self.__RenderAll()
def Convert(self, toRate: Rate, amount: float = 1) -> float: """ Converts a given amount of self into toRate Parameters: toRate (Rate): Rate object target for currency conversion Returns: conversion (float) : The amount of self converted into toRate """ if isinstance(toRate, Rate): return (self.toEuro / toRate.toEuro) * amount mLogger.critical( f"ConvertException: toRate is not type Rate but type '{type(toRate)}'" ) raise TypeError( "toRate argument must be of type 'Rate' and not type '%s'" % type(toRate))
def ScrapeRates() -> Tuple[Sequence[Types.DictableRate], float]: """ Webscrapes x-rates for updated rates Return a Tuple containing the data and a timestamp Returns: data (Tuple) : Tuple containing data and timestamp """ rates: Sequence[Types.DictableRate] = [] timestamp: float = 0 request: Types.URLResponse = URLManager(url=Constants.url, header=Constants.header) if isinstance(request, bytes): response: HtmlElement = html.fromstring(request) rawNames: MutableSequence[str] = FindXPath(response, Constants.xpathName) rawFromEuros: MutableSequence[str] = FindXPath( response, Constants.xpathFrom) rawToEuros: MutableSequence[str] = FindXPath( response, Constants.xpathTo) names: Sequence[List[str]] = GetNames(rawNames) abbreviations: Sequence[List[str]] = [ [i] for i in Constants.currencies.keys() ] fromEuros: Sequence[List[str]] = [ FindFloats(str(i)) for i in rawFromEuros ] toEuros: Sequence[List[str]] = [ FindFloats(str(i)) for i in rawToEuros ] mRates: Sequence[Sequence[str]] = [ name + abbreviation + fromEuro + toEuro for name, abbreviation, fromEuro, toEuro in zip( names, abbreviations, fromEuros, toEuros) ] rates, timestamp = MakeRatesDictable(mRates) if len(rates) <= 0: mLogger.critical( f"ScrapeRatesExpection: response: {str(request)}, handled: {rates}" ) return rates, timestamp
def URLManager(url: str, header: Mapping[str, str], mSleep: int = 8, rJSON: bool = False) -> Types.URLResponse: """ Manages HTTP/HTTPS connections Returns: result (URLResponse) : Returns response content on success """ e: RequestException try: req: Response = get(url=url, params=header) except RequestException as e: mLogger.critical(e) raise RequestException(e) status: int = req.status_code if status == 200: if rJSON: return req.json() return req.content mLogger.critical(f"URLManager: non-200 response: {req}") raise Exception("PyRatesURLError: Failed communication with server. Check '%s/%s' for further inspection." % (Constants.logPath, Constants.logFileName))