Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a separate class for a custom function?

I have to find user_id and order_id in the message, but I want to make a separate class for this search. I just want to optimise my code.

I have highlighted a part of the code(#—–) that I would like to cram into a separate file, but since I am a beginner I do not understand how it could be implemented. Tell me, please or at least a similar example to figure it out for yourself.

This is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@dp.callback_query_handler(text="accept_ord", state=None)
    async def process_order_callback_4(query: types.CallbackQuery):
    answ_date_2=query.data

#------------
    #find the text from message
    text = str(query.message.text)
    #find id_order from the text of the message
    id = text.split('\n')[0].split("№ ")[1]
    #from str to int
    id_1 = int(re.search('\d+', id).group(0))
    #find id_user from table_2 in db
    cl_id = str(db.get_user_id_admin(id_1))
    #from str to int
    cl_id_1 = int(re.search('\d+', cl_id).group(0))
    #find id_user from table_1 in db
    user_id = str(db.get_user_telegram_id_admin(cl_id_1))
    user_id_clear=int(re.search('\d+', user_id).group(0))
    #------------

    await bot.send_message(user_id_clear,
                           text=f'The status of the order has been change №{id_1} изменен!\n\nPlease check main menu (/start)')
    if answ_date_2 == "accept_ord":
        status = 2
        db.set_new_status(id_1, status)

        await bot.send_message(query.from_user.id, text=f'The order №{id_1} has been assepted.', reply_markup=nav.back_admin_menu_order)

It looks kreepy, but it works, hah.

>Solution :

Avoid obsessing about classes; a class has a distinct theory that should not be abused. However, as you noted, your code appears to be a bit lengthy, and I would suggest the following:

@dp.callback_query_handler(text="accept_ord", state=None)
async def process_order_callback_4(query: types.CallbackQuery):
    answ_date_2=query.data

    #------------
    id_1, user_id_clear = await __mySearch(query)
    #------------
    
    await bot.send_message(user_id_clear,
                           text=f'The status of the order has been change №{id_1} изменен!\n\nPlease check main menu (/start)')
    if answ_date_2 == "accept_ord":
        status = 2
        db.set_new_status(id_1, status)
    
        await bot.send_message(query.from_user.id, text=f'The order №{id_1} has been assepted.', reply_markup=nav.back_admin_menu_order)


async def __mySearch(query):
    #find the text from message
    text = str(query.message.text)
    #find id_order from the text of the message
    id = text.split('\n')[0].split("№ ")[1]
    #from str to int
    id_1 = int(re.search('\d+', id).group(0))
    #find id_user from table_2 in db
    cl_id = str(db.get_user_id_admin(id_1))
    #from str to int
    cl_id_1 = int(re.search('\d+', cl_id).group(0))
    #find id_user from table_1 in db
    user_id = str(db.get_user_telegram_id_admin(cl_id_1))
    user_id_clear = int(re.search('\d+', user_id).group(0))
    return id_1, user_id_clear

Note: For some tasks, it is acceptable to construct a utils.py that does the task generically with the parameters provided and returns the desired result. It could be extracted into another file and called within yours such as import myFile (refer to this thread for more information)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading