Skip to content

Commit eb4d58b

Browse files
author
Max
committed
little optimization on handler dictionary building: code duplication lessened
1 parent 57486b1 commit eb4d58b

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

telebot/__init__.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,12 @@ def _append_pre_next_step_handler(self):
654654
self.message_subscribers_next_step[k] = self.pre_message_subscribers_next_step[k]
655655
self.pre_message_subscribers_next_step = {}
656656

657+
def _build_handler_dict(self, handler, **filters):
658+
return {
659+
'function': handler,
660+
'filters': filters
661+
}
662+
657663
def message_handler(self, commands=None, regexp=None, func=None, content_types=['text']):
658664
"""
659665
Message handler decorator.
@@ -685,54 +691,34 @@ def default_command(message):
685691
"""
686692

687693
def decorator(handler):
688-
self.add_message_handler(handler, commands, regexp, func, content_types)
689-
return handler
694+
handler_dict = self._build_handler_dict(handler,
695+
commands=commands,
696+
regexp=regexp,
697+
func=func,
698+
content_types=content_types)
690699

691-
return decorator
692-
693-
def add_message_handler(self, handler, commands=None, regexp=None, func=None, content_types=None):
694-
if content_types is None:
695-
content_types = ['text']
700+
self.add_message_handler(handler_dict)
696701

697-
filters = {'content_types': content_types}
698-
if regexp:
699-
filters['regexp'] = regexp
700-
if func:
701-
filters['lambda'] = func
702-
if commands:
703-
filters['commands'] = commands
702+
return handler
704703

705-
handler_dict = {
706-
'function': handler,
707-
'filters': filters
708-
}
704+
return decorator
709705

706+
def add_message_handler(self, handler_dict):
710707
self.message_handlers.append(handler_dict)
711708

712709
def edited_message_handler(self, commands=None, regexp=None, func=None, content_types=['text']):
713710
def decorator(handler):
714-
self.add_edited_message_handler(handler, commands, regexp, func, content_types)
711+
handler_dict = self._build_handler_dict(handler,
712+
commands=commands,
713+
regexp=regexp,
714+
func=func,
715+
content_types=content_types)
716+
self.add_edited_message_handler(handler_dict)
715717
return handler
716718

717719
return decorator
718720

719-
def add_edited_message_handler(self, handler, commands=None, regexp=None, func=None, content_types=None):
720-
if content_types is None:
721-
content_types = ['text']
722-
723-
filters = {'content_types': content_types}
724-
if regexp:
725-
filters['regexp'] = regexp
726-
if func:
727-
filters['lambda'] = func
728-
if commands:
729-
filters['commands'] = commands
730-
731-
handler_dict = {
732-
'function': handler,
733-
'filters': filters
734-
}
735-
721+
def add_edited_message_handler(self, handler_dict):
736722
self.edited_message_handlers.append(handler_dict)
737723

738724
def inline_handler(self, func):

0 commit comments

Comments
 (0)