def update_s(self, updateCommand: str) -> None: """ Update Stack's currencies from the given command. Args: updateCommand (str): Command to be parsed. """ currenciesUpdate = updateCommand.split(sep=",") if len(currenciesUpdate) is not 3: Logger("Might update the three currencies.") return for currencyUpdate in currenciesUpdate: update = currencyUpdate.split(sep=":") if len(update) is not 2: Logger("Wrong format. Command: update_s") elif (update[CURRENCY] in globals.currencies) is False: Logger( f"{update[CURRENCY]} is not a valid currency." f"Currencies are {globals.currencies}" ) elif self._utils.isFloat(update[VALUE]) is False: Logger("Wrong {} value.".format(update[CURRENCY])) else: self.updateCurrency( currency=update[CURRENCY], value=update[VALUE] )
def initPair(self, inputPair: str) -> None: """ Initialise the pair of currencies. Args: inputPair (str): Input string to be parsed and checked. """ def isValidCurrency(inputCurrency: str) -> bool: if inputCurrency in globals.currencies: return True return False # Parse the pair of currencies inputPair = inputPair.split(sep="_") # Check the pair of currencies if len(inputPair) is not 2: Logger("Might be a pair such as `currency_currency`.") elif isValidCurrency(inputPair[0]) is False\ or isValidCurrency(inputPair[1]) is False: Logger("Wrong currency.") elif inputPair[0] == inputPair[1]: Logger("Two times the same currency.") else: self._currency1 = inputPair[0] self._currency2 = inputPair[1]
def initRates(self, inputCandle: str) -> list: """ Parse the inputCandle string into a list of three `rates`. Args: inputCandle (str): the input command to be parsed. Returns: list: such as [Rate, Rate, Rate] """ # Split input string (cut by a semi-colon) inputsRate = inputCandle.split(sep=";") if len(inputsRate) is not 3: Logger("There might be three rates per candle.") self._state = globals.INVALID return None outputRates = [ Rate(inputsRate[0]), Rate(inputsRate[1]), Rate(inputsRate[2]) ] # Check for rates' validity for rate in outputRates: if rate._state is globals.INVALID: Logger("Candle is invalid") self._state = globals.INVALID return outputRates
def parseInputRate(self, inputRate: str): """ Parse the inputRate into class' attributes. Args: inputRate (str): The input to be parsed. Returns: bool: False if it cannot initialize the Rate. True otherwise. """ # Split input string (cut by a colon) items = inputRate.split(sep=",") if len(items) is not 7: Logger("Wrong input rate.") return None self.initPair(items[globals.CANDLE_FORMAT["PAIR"]]) self._date = self.initDate(items[globals.CANDLE_FORMAT["DATE"]]) self._high = self.initPrice(items[globals.CANDLE_FORMAT["HIGH"]]) self._low = self.initPrice(items[globals.CANDLE_FORMAT["LOW"]]) self._open = self.initPrice(items[globals.CANDLE_FORMAT["OPEN"]]) self._close = self.initPrice(items[globals.CANDLE_FORMAT["CLOSE"]]) self._volume = self.initPrice(items[globals.CANDLE_FORMAT["VOLUME"]]) # Change the Rate's state to VALID if all of its attributes are valid if all([self._currency1, self._currency2, self._date, self._high, self._low, self._open, self._open, self._close, self._volume]): self._state = globals.VALID
def initPrice(self, inputPrice: str) -> float: """ Initialize a price value of the current rate. Args: inputPrice (str): Input string to be checked. Returns: float: Return None if it cannot initialize the price. Return the cast value otherwise. """ if Utilities().isFloat(inputPrice) is False: Logger("Prices might be floats.") return None return float(inputPrice)
def getInput(self) -> str: """ Get input and catch potential errors. Returns: str: Input received from stdin. """ try: inputs = input() except EOFError: exit(84) except KeyboardInterrupt: Logger("\nexit.", logType="INFO") exit(0) else: return inputs
def initDate(self, inputDate: str) -> int: """ Initialize the Rate's date. Args: inputDate (str): Input string to be checked. Returns: int: Return None if it cannot initialize the date. Return the cast value otherwise. """ if Utilities().isInt(inputDate) is False: Logger("Rate's date might be an integer.") return None return int(inputDate)
def fetchCommand(self, command: list) -> None: """ Fetch the right command depending of the input and execute it. Args: command (list): the command to be checked and executed. """ if command[0] == "settings" and len(command) is 3: self.initSettings(command) elif command[0] == "update" and len(command) == 4: if f"{command[1]} {command[2]}" == "game next_candles": newCandle = Candle(command[3]) if newCandle._state == globals.VALID: self._candles.append(newCandle) self._t.update_lastClosePrices(newCandle) elif f"{command[1]} {command[2]}" == "game stacks": self._stack.update_s(command[3]) elif f"{command[0]} {command[1]}" == "action order": # Server is waiting for a move. (`sell`|`buy`|`pass`) self._t.strategy(candles=self._candles, stack=self._stack) else: Logger("Unrecognized command.")
def test_wrongLevelCase(caplog): Logger("Hello world!", logType='WRONGTYPE') with caplog.at_level(logging.ERROR): assert caplog.records[0].message == "Wrong logging type."
def test_ERRORLevelCase(caplog): Logger("Hello world!") with caplog.at_level(logging.ERROR): assert caplog.records[0].message == "Hello world!"