From a2d8ca366389f78210ccf5ab2788deb5a61f4979 Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Fri, 25 Dec 2015 23:28:07 +0200 Subject: [PATCH] some errors are prefixed with '[Error]: ' - strip that down use custom _lstrip_str() func instead of regex. a little performance improvment and (IMO) a bit more readable. --- telegram/error.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/telegram/error.py b/telegram/error.py index fe557771b..b24bffc8d 100644 --- a/telegram/error.py +++ b/telegram/error.py @@ -18,7 +18,22 @@ """This module contains a object that represents a Telegram Error""" -import re + +def _lstrip_str(in_s, lstr): + """ + Args: + in_s (str): in string + lstr (str): substr to strip from left side + + Returns: + str: + + """ + if in_s.startswith(lstr): + res = in_s[len(lstr):] + else: + res = in_s + return res class TelegramError(Exception): @@ -26,16 +41,20 @@ class TelegramError(Exception): def __init__(self, message): """ + Args: + message (str): + Returns: - str: + """ super(TelegramError, self).__init__() - api_error = re.match(r'^Error: (?P.*)', message) - if api_error: - self.message = api_error.group('message').capitalize() - else: - self.message = message + msg = _lstrip_str(message, 'Error: ') + msg = _lstrip_str(msg, '[Error]: ') + if msg != message: + # api_error - capitalize the msg... + msg = msg.capitalize() + self.message = msg def __str__(self): return '%s' % (self.message)