From 4d1613a43265ea039b5b009335343920fe188063 Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Sat, 24 Apr 2021 13:26:40 +0200 Subject: [PATCH 1/2] Add chunks function. Implementing a generator to split Lists into chunks. --- freqtrade/misc.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 6508363d6..2e255901e 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -6,7 +6,7 @@ import logging import re from datetime import datetime from pathlib import Path -from typing import Any +from typing import Any, Iterator, List from typing.io import IO import rapidjson @@ -202,3 +202,14 @@ def render_template_with_fallback(templatefile: str, templatefallbackfile: str, return render_template(templatefile, arguments) except TemplateNotFound: return render_template(templatefallbackfile, arguments) + + +def chunks(lst: List[Any], n: int) -> Iterator[List[Any]]: + """ + Split lst into chunks of the size n. + :param lst: list to split into chunks + :param n: number of max elements per chunk + :return: None + """ + for chunk in range(0, len(lst), n): + yield (lst[chunk:chunk + n]) From 3be7bc509c603bc1f40619d61d12c34def88500c Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Sat, 24 Apr 2021 13:28:24 +0200 Subject: [PATCH 2/2] Telegram: send locks as chunks of 25. Producing easily readable messages, hopefully always below the message lenght limit --- freqtrade/rpc/telegram.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 3eeedcd12..97a01fc53 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -20,7 +20,7 @@ from telegram.utils.helpers import escape_markdown from freqtrade.__init__ import __version__ from freqtrade.constants import DUST_PER_COIN from freqtrade.exceptions import OperationalException -from freqtrade.misc import round_coin_value +from freqtrade.misc import chunks, round_coin_value from freqtrade.rpc import RPC, RPCException, RPCHandler, RPCMessageType @@ -750,17 +750,18 @@ class Telegram(RPCHandler): Handler for /locks. Returns the currently active locks """ - locks = self._rpc._rpc_locks() - message = tabulate([[ - lock['id'], - lock['pair'], - lock['lock_end_time'], - lock['reason']] for lock in locks['locks']], - headers=['ID', 'Pair', 'Until', 'Reason'], - tablefmt='simple') - message = f"
{escape(message)}
" - logger.debug(message) - self._send_msg(message, parse_mode=ParseMode.HTML) + rpc_locks = self._rpc._rpc_locks() + for locks in chunks(rpc_locks['locks'], 25): + message = tabulate([[ + lock['id'], + lock['pair'], + lock['lock_end_time'], + lock['reason']] for lock in locks], + headers=['ID', 'Pair', 'Until', 'Reason'], + tablefmt='simple') + message = f"
{escape(message)}
" + logger.debug(message) + self._send_msg(message, parse_mode=ParseMode.HTML) @authorized_only def _delete_locks(self, update: Update, context: CallbackContext) -> None: