Converts the given data to the appropriate number if it is indeed a number, else returns the data as it is with a False flag. This function also serves as a check of whether the input is a number or not. Parameters ---------- token: The data that needs to be converted to th
(token: str | float)
| 38 | |
| 39 | |
| 40 | def parse_token(token: str | float) -> float | str: |
| 41 | """ |
| 42 | Converts the given data to the appropriate number if it is indeed a number, else |
| 43 | returns the data as it is with a False flag. This function also serves as a check |
| 44 | of whether the input is a number or not. |
| 45 | |
| 46 | Parameters |
| 47 | ---------- |
| 48 | token: The data that needs to be converted to the appropriate operator or number. |
| 49 | |
| 50 | Returns |
| 51 | ------- |
| 52 | float or str |
| 53 | Returns a float if `token` is a number or a str if `token` is an operator |
| 54 | """ |
| 55 | if token in OPERATORS: |
| 56 | return token |
| 57 | try: |
| 58 | return float(token) |
| 59 | except ValueError: |
| 60 | msg = f"{token} is neither a number nor a valid operator" |
| 61 | raise ValueError(msg) |
| 62 | |
| 63 | |
| 64 | def evaluate(post_fix: list[str], verbose: bool = False) -> float: |