Best Python code snippet using autotest_python
my_poi.py
Source:my_poi.py
...66 bot.send_message(chat_id=message.chat.id, text=text, reply_markup=keyboard)67@bot.message_handler(commands=['reset'])68def reset_command(message):69 chat_id = message.chat.id70 db.execute_query_with_param(connection, db.delete_poi_query, (chat_id,))71 text = 'ÐÑбÑана команда /reset ÐÑе ÐаÑи меÑÑа ÑдаленÑ'72 bot.send_message(chat_id=message.chat.id, text=text)73@bot.message_handler(content_types=['location'])74def send_location_command(message):75 chat_id = message.chat.id76 if chat_id not in chat_ids or not chat_ids[chat_id]['is_input_poi']:77 bot.send_message(chat_id=message.chat.id, text='ÐнаÑале вÑбеÑиÑе ÐºÐ¾Ð¼Ð°Ð½Ð´Ñ Ð¸Ð· ÑпиÑка')78 return79 print(message.location)80 print(message.location.latitude)81 print(message.location.longitude)82 chat_ids[chat_id]['location_lat'] = message.location.latitude83 chat_ids[chat_id]['location_lon'] = message.location.longitude84 print('ÐокаÑÐ¸Ñ Ð¿ÑинÑÑа')85@bot.message_handler(content_types=['photo'])86def send_photo_command(message):87 chat_id = message.chat.id88 if chat_id not in chat_ids or not chat_ids[chat_id]['is_input_poi']:89 bot.send_message(chat_id=message.chat.id, text='ÐнаÑале вÑбеÑиÑе ÐºÐ¾Ð¼Ð°Ð½Ð´Ñ Ð¸Ð· ÑпиÑка')90 return91 file_info = bot.get_file(message.photo[-1].file_id)92 downloaded_file = bot.download_file(file_info.file_path)93 rphoto = db.resize_image(downloaded_file)94 chat_ids[chat_id]['photo'] = rphoto95 print('ФоÑо пÑинÑÑо')96@bot.message_handler()97def handle_message(message):98 # print(message.text)99 chat_id = message.chat.id100 if chat_id not in chat_ids or not chat_ids[chat_id]['is_input_poi']:101 bot.send_message(chat_id=message.chat.id, text='ÐÑбеÑиÑе ÐºÐ¾Ð¼Ð°Ð½Ð´Ñ Ð¸Ð· ÑпиÑка')102 elif chat_ids[chat_id]['input_poi_step'] == 'address':103 chat_ids[chat_id]['address'] = message.text104 # elif chat_ids[chat_id]['input_poi_step'] == 'photo':105 # photo = message.image106 # rphoto = db.resize_image(photo)107 # chat_ids[chat_id]['photo'] = rphoto108 elif chat_ids[chat_id]['input_poi_step'] == 'description':109 chat_ids[chat_id]['description'] = message.text110 # elif chat_ids[chat_id]['input_poi_step'] == 'location':111 # chat_ids[chat_id]['location_lat'] = message.location['latitude']112 # chat_ids[chat_id]['location_lon'] = message.location['longitude']113 # print(chat_ids)114@bot.callback_query_handler(func=lambda x: True)115def callback_worker(callback_query):116 input_poi_step = callback_query.data117 message = callback_query.message118 chat_id = message.chat.id119 if callback_query.data in ['1', '5', '10']:120 pois = db.execute_read_query(connection, db.select_poi_query, (chat_id,), limit=int(callback_query.data))121 if len(pois) == 0:122 bot.send_message(chat_id=message.chat.id, text='ÐиÑего неÑ')123 return124 # text = f'ÐоказÑÐ²Ð°Ñ {callback_query.data} меÑÑ'125 # bot.send_message(chat_id=message.chat.id, text=text)126 for poi in pois:127 text = f'СоÑ
Ñанено {poi[5]} в {poi[6]}'128 bot.send_message(chat_id=message.chat.id, text=text)129 bot.send_message(chat_id=message.chat.id, text=poi[0])130 if poi[1]:131 bot.send_message(chat_id=message.chat.id, text=poi[1])132 # print(poi[0], poi[1])133 photo = poi[2]134 if photo:135 bot.send_photo(chat_id, photo)136 if poi[3] and poi[3]:137 bot.send_location(chat_id, poi[3], poi[4])138 return139 chat_ids[chat_id]['input_poi_step'] = input_poi_step140 if input_poi_step == 'address':141 bot.send_message(chat_id=message.chat.id, text='ÐведиÑе адÑÐµÑ :')142 if input_poi_step == 'photo':143 bot.send_message(chat_id=message.chat.id, text='ÐобавÑÑе ÑоÑо :')144 if input_poi_step == 'description':145 bot.send_message(chat_id=message.chat.id, text='ÐведиÑе опиÑание :')146 if input_poi_step == 'location':147 bot.send_message(chat_id=message.chat.id, text='ÐобавÑÑе локаÑÐ¸Ñ :')148 if input_poi_step == 'save':149 if not chat_ids[chat_id]['address']:150 bot.send_message(chat_id=message.chat.id, text='ÐдÑÐµÑ Ð½Ðµ Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð¿ÑÑÑÑм')151 return152 save_poi(chat_id)153 bot.send_message(chat_id=message.chat.id, text='ÐеÑÑо ÑоÑ
Ñанено')154 if input_poi_step == 'notsave':155 notsave_poi(chat_id)156 bot.send_message(chat_id=message.chat.id, text='ÐеÑÑо не ÑоÑ
Ñанено')157def clear_input_state(chat_id):158 chat_ids[chat_id]['is_input_poi'] = False159 chat_ids[chat_id]['input_poi_step'] = ''160 chat_ids[chat_id]['address'] = ''161 chat_ids[chat_id]['photo'] = ''162 chat_ids[chat_id]['description'] = ''163 chat_ids[chat_id]['location_lat'] = ''164 chat_ids[chat_id]['location_lon'] = ''165def save_poi(chat_id):166 # print(chat_ids[chat_id])167 cur_date_time = datetime.datetime.now()168 cur_date = cur_date_time.date()169 cur_time = cur_date_time.time()170 date_creation = str(cur_date)171 time_creation = cur_time.strftime('%H:%M:%S')172 db.execute_query_with_param(connection, db.insert_poi_query,173 (chat_id, chat_ids[chat_id]['address'], chat_ids[chat_id]['description'], chat_ids[chat_id]['photo'], chat_ids[chat_id]['location_lat'], chat_ids[chat_id]['location_lon'], date_creation, time_creation))174 clear_input_state(chat_id)175def notsave_poi(chat_id):176 # print(chat_ids[chat_id])177 clear_input_state(chat_id)178def main():179 global connection180 connection = db.create_connection('poi.db')181 db.execute_query(connection, db.create_poi_table_query)182 print('Start')183 bot.polling()184if __name__ == '__main__':185 main()186'''...
postgresql_helper.py
Source:postgresql_helper.py
...134 cur.execute('ALTER TABLE IF EXISTS %s RENAME TO %s;', (src_table_name, dest_table_name))135 connection.commit()136 cur.close()137 connection.close()138def execute_query_with_param(query="", config_file_path=CONFIGURATION_FILE_PATH, param=''):139 engine = get_engine(config_file_path)140 connection = engine.connect()141 result = connection.execute(text(query), domain_list=param).scalar()...
db.py
Source:db.py
...47 connection.commit()48 # print("Query executed successfully")49 except Error as e:50 print(f"The error '{e}' occurred")51def execute_query_with_param(connection, query, values):52 cursor = connection.cursor()53 try:54 cursor.execute(query, values)55 connection.commit()56 # print("Query executed successfully")57 except Error as e:58 print(f"The error '{e}' occurred")59def execute_read_query(connection, query, values, limit=1):60 cursor = connection.cursor()61 result = None62 try:63 cursor.execute(query, values)64 result = cursor.fetchmany(limit)65 # result = cursor.fetchall()66 return result67 except Error as e:68 print(f"The error '{e}' occurred")69def read_image(filename):70 try:71 fin = open(filename, "rb")72 img = fin.read()73 # img = Image.open(filename)74 return img75 except IOError as e:76 # Ð ÑлÑÑае оÑибки, вÑводим ее ÑекÑÑ77 print("Error %d: %s" % (e.args[0], e.args[1]))78 sys.exit(1)79 finally:80 if fin:81 # ÐакÑÑваем подклÑÑение Ñ Ñайлом82 fin.close()83def resize_image(original_image):84 max_size = (320, 240)85 image = Image.open(io.BytesIO(original_image))86 image.thumbnail(max_size, Image.ANTIALIAS)87 # image.show()88 img_byte_arr = io.BytesIO()89 image.save(img_byte_arr, format='JPEG')90 img_byte_arr.seek(0)91 small_image = img_byte_arr.read()92# return small_image93 return sqlite3.Binary(small_image)94# connection = create_connection('poi.db')95# execute_query(connection, create_poi_table_query)96#97# chat_id = '12345678'98# chat_id = '454724133'99# address = 'ÐоÑква4'100# description = 'ХоÑоÑее меÑÑо4'101# image = read_image('anypics.ru-5050-480.jpg')102# small_image = resize_image(image)103# photo = sqlite3.Binary(small_image)104# location_lat = '55.753707'105# location_lon = '37.62003'106# cur_date_time = datetime.datetime.now()107# cur_date = cur_date_time.date()108# cur_time = cur_date_time.time()109# date_creation = str(cur_date)110# time_creation = cur_time.strftime('%H:%M:%S')111# execute_query_with_param(connection, insert_poi_query,112# (chat_id, address, description, photo, location_lat, location_lon, date_creation, time_creation))113# execute_query_with_param(connection, delete_poi_query, (chat_id,))114# pois = execute_read_query(connection, select_poi_query, (chat_id,/), limit=1)115# for poi in pois:116# print(poi[0], poi[1])117# poi = pois[0]118# print(poi[0], poi[1])119# photo = poi[2]120# fout = open('output1.jpg', 'wb')121# fout.write(photo)122# fout.close()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!