Prettify expressions
This commit is contained in:
		| @@ -11,7 +11,7 @@ __author__ = "Davide Testa" | |||||||
| __email__ = "davide@davte.it" | __email__ = "davide@davte.it" | ||||||
| __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] | __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] | ||||||
| __license__ = "GNU General Public License v3.0" | __license__ = "GNU General Public License v3.0" | ||||||
| __version__ = "2.5.18" | __version__ = "2.5.19" | ||||||
| __maintainer__ = "Davide Testa" | __maintainer__ = "Davide Testa" | ||||||
| __contact__ = "t.me/davte" | __contact__ = "t.me/davte" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ import datetime | |||||||
| import json | import json | ||||||
| import logging | import logging | ||||||
| import operator | import operator | ||||||
|  | import re | ||||||
|  |  | ||||||
| from collections import OrderedDict | from collections import OrderedDict | ||||||
| from typing import List, Union | from typing import List, Union | ||||||
| @@ -182,6 +183,16 @@ def get_operators() -> dict: | |||||||
|  |  | ||||||
| calc_buttons = get_calc_buttons() | calc_buttons = get_calc_buttons() | ||||||
| operators = get_operators() | operators = get_operators() | ||||||
|  | operators_spacer = re.compile(r"(\d)\s*([+\-*%]|/{1,2})\s*(\d)") | ||||||
|  | spaced_operators = r"\1 \2 \3" | ||||||
|  | operators_space_remover = re.compile(r"(\d)\s*(\*\*)\s*(\d)") | ||||||
|  | un_spaced_operators = r"\1\2\3" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def prettify_expression(expression): | ||||||
|  |     expression = operators_spacer.sub(spaced_operators, expression) | ||||||
|  |     expression = operators_space_remover.sub(un_spaced_operators, expression) | ||||||
|  |     return expression | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_calculator_keyboard(additional_data: list = None): | def get_calculator_keyboard(additional_data: list = None): | ||||||
| @@ -367,6 +378,7 @@ async def calculate_session(bot: Bot, | |||||||
|             expression += calc_buttons[input_value]['value'] |             expression += calc_buttons[input_value]['value'] | ||||||
|         else: |         else: | ||||||
|             logging.error(f"Invalid input from calculator button: {input_value}") |             logging.error(f"Invalid input from calculator button: {input_value}") | ||||||
|  |     expression = prettify_expression(expression) | ||||||
|     if record: |     if record: | ||||||
|         bot.db['calculations'].update( |         bot.db['calculations'].update( | ||||||
|             dict( |             dict( | ||||||
| @@ -449,6 +461,7 @@ async def _calculate_command(bot: Bot, | |||||||
|                 id=record_id |                 id=record_id | ||||||
|             ) |             ) | ||||||
|             expression = f"{record['expression'] or ''}\n{text}" |             expression = f"{record['expression'] or ''}\n{text}" | ||||||
|  |         expression = prettify_expression(expression) | ||||||
|         bot.db['calculations'].update( |         bot.db['calculations'].update( | ||||||
|             dict( |             dict( | ||||||
|                 id=record_id, |                 id=record_id, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user