Hello all
I want to make the following code easier by removing the decorator. How can I do it?
import telebot
bot = telebot.TeleBot('`**************************') # Replace with your bot token
questions = ["What is the answer to question 1","What is the answer to question 2","What is the answer to question 3","What is the answer to question 4"]
answers= ['A','B','C','D']
i,current_question = 0, None
@bot.message_handler(commands=['start'])
I tried the following code, but it did not work:
def handle_messages(message):
if message.text.lower().strip() == '/start':
bot.reply_to(message, "Welcome to the bot!")
else:
pass
bot.message_handler(func=handle_messages)
I’m not sure I understand the question, but I’ll try to give some helper info.
I’m not sure why you don’t want to use the decorator but take this example (using a decorator from the stdlib.) The same logic applies to ones in libraries.
from functools import cache
from datetime import datetime
from time import sleep
@cache
def cached_now_via_decorator_syntax():
""" Cached using @cache decorator syntax """
return datetime.now()
#: Cached using cache(<func>) syntax
cached_now_without_decorator_syntax = cache(datetime.now)
if __name__ == '__main__':
for i in range(3):
print(cached_now_via_decorator_syntax())
sleep(.1)
print (" ^^^ Are the same since it was cached via 'decorator syntax' ^^^")
for i in range(3):
print(cached_now_without_decorator_syntax())
sleep(.1)
print (" ^^^ Are the same since it was cached via via the decorator without the 'decorator syntax' ^^^")
print (".. the two ways are equivalent. @cache is just syntactic sugar for cache(<func>).")
for i in range(3):
print(datetime.now())
sleep(.1)
print (" ^^^ Are different because they are not cached ^^^")
In other words, I’d say use the decorator syntax, but if you really don’t want to, you don’t have to. A decorator with parameters works the same way but may look a bit odd:
from functools import lru_cache
from datetime import datetime
from time import sleep
@lru_cache(maxsize=4)
def cached_now_via_decorator_syntax():
""" Cached using @cache decorator syntax """
return datetime.now()
#: Cached using cache(<func>) syntax
cached_now_without_decorator_syntax = lru_cache(maxsize=4)(datetime.now)
if __name__ == '__main__':
for i in range(3):
print(cached_now_via_decorator_syntax())
sleep(.1)
print (" ^^^ Are the same since it was cached via 'decorator syntax' ^^^")
for i in range(3):
print(cached_now_without_decorator_syntax())
sleep(.1)
print (" ^^^ Are the same since it was cached via via the decorator without the 'decorator syntax' ^^^")
print (".. the two ways are equivalent. @lru_cache(maxsize=4) is just syntactic sugar for lru_cache(maxsize=4)(<func>).")
for i in range(3):
print(datetime.now())
sleep(.1)
print (" ^^^ Are different because they are not cached ^^^")
Running one of these gives me:
C:\Users\csm10495\Desktop>python test.py
2023-12-01 18:57:33.545686
2023-12-01 18:57:33.545686
2023-12-01 18:57:33.545686
^^^ Are the same since it was cached via 'decorator syntax' ^^^
2023-12-01 18:57:33.846963
2023-12-01 18:57:33.846963
2023-12-01 18:57:33.846963
^^^ Are the same since it was cached via via the decorator without the 'decorator syntax' ^^^
.. the two ways are equivalent. @cache is just syntactic sugar for cache(<func>).
2023-12-01 18:57:34.148657
2023-12-01 18:57:34.249316
2023-12-01 18:57:34.349884
^^^ Are different because they are not cached ^^^