Best Python code snippet using pyatom_python
03_mro.py
Source:03_mro.py
...5 def make_transaction(self):6 print('ТÑанзакÑÐ¸Ñ Ð±Ð¸Ð·Ð½ÐµÑ-логики') 7class BaseProtocol():8 pass9 def make_connection(self):10 print('Ðазовое TCP-подклÑÑение') 11class MagicProtocol():12 def make_connection(self):13 print('ÐагиÑеÑкое подклÑÑение (кÑÑÑеÑ)') 14class FtpProtocol(BaseProtocol):15 pass16 def make_connection(self):17 print('FTP подклÑÑение')18class SftpProtocol(FtpProtocol):19 pass20 def make_connection(self):21 print('SFTP подклÑÑение') 22class HttpProtocol(BaseProtocol):23 pass24 def make_connection(self):25 print('HTTP подклÑÑение')26class HttpsProtocol(HttpProtocol):27 pass28 def make_connection(self):29 print('HTTPS подклÑÑение')30# Создадим обÑий клаÑÑ, коÑоÑÑй ÑнаÑледован Ð¾Ñ Ð½ÐµÑколÑкиÑ
клаÑÑов-пÑоÑоколов31class BusinessExchange(BusinessLogic, FtpProtocol, HttpsProtocol, 32 MagicProtocol):33 pass34 def make_connection(self):35 print('ÐизнеÑ-Ñделка')36# ÐомменÑиÑÑÑ Ð² Ñазном поÑÑдке в каждом клаÑÑе меÑод make_connection,37# можно поÑмоÑÑеÑÑ, как менÑеÑÑÑ Ð¿Ð¾Ð²ÐµÐ´ÐµÐ½Ð¸Ðµ клаÑÑа BusinessExchange:38business_exch = BusinessExchange()39business_exch.make_connection()40business_exch.make_transaction()41# ЧÑÐ¾Ð±Ñ ÑзнаÑÑ Ð¿Ð¾ÑÑдок ÑазÑеÑÐµÐ½Ð¸Ñ Ð¼ÐµÑодов, коÑоÑÑй в данном ÑлÑÑае пÑинÑл Python,42# можно поÑмоÑÑеÑÑ Ð·Ð½Ð°Ñение аÑÑибÑÑа __mro__ клаÑÑа:43print(BusinessExchange.__mro__)44# РклаÑÑаÑ
"нового ÑÑилÑ" (в Python 3 вÑе клаÑÑÑ - нового ÑÑилÑ) ÑеализÑеÑÑÑ45# алгоÑиÑм C3-линеаÑизаÑии Ð´Ð»Ñ Ð²ÑÑÑÑÐ°Ð¸Ð²Ð°Ð½Ð¸Ñ "ÑепоÑки" клаÑÑов-ÑодиÑелей:46# https://en.wikipedia.org/wiki/C3_linearization47# СÑÑеÑÑвÑеÑ, однако, ÑиÑÑаÑиÑ, когда невозможно вÑÑÑÑоиÑÑ ÑакÑÑ "ÑепоÑкÑ".48# ÐеÑазÑеÑÐ¸Ð¼Ð°Ñ ÑиÑÑаÑÐ¸Ñ Ð¼Ð½Ð¾Ð¶ÐµÑÑвенного наÑÐ»ÐµÐ´Ð¾Ð²Ð°Ð½Ð¸Ñ Ð² Python 3:49class XConnection(BaseProtocol):50 def make_connection(self):51 print('x-подклÑÑение')52class YConnection(BaseProtocol):53 def make_connection(self):54 print('y-подклÑÑение')55class ZConnection(XConnection, YConnection):56 def make_connection(self):57 print('Z-подклÑÑение')58class QConnection(XConnection, YConnection):59 def make_connection(self):60 print('Q-подклÑÑение')61class MixedConnection(ZConnection, QConnection):62 """docstring for MixedConnection"""63 def make_connection(self):64 print('MIX-подклÑÑение')65mixed_conn = MixedConnection()...
database.py
Source:database.py
1import os2from pymongo import MongoClient3from bot import get_bot_info4def make_connection():5 """Utility function to connect to database.6 Returns:7 instance of MongoClient.8 """9 client = MongoClient('mongodb+srv://eora_tester:' + os.environ.get('MONGO_PASS', 'password') + '@eora-test.idkwk.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')10 db = client.eora11 return db12def add_user(user):13 """Add user to database from register endpoint.14 Args:15 user: data of user.16 Returns:17 user_id.18 """19 db = make_connection()20 if db.users.find_one({'username': user['username']}):21 return None22 user_id = db.users.insert_one({**user, 'bots': []}).inserted_id23 return user_id24def get_user(username):25 """Get user by username.26 Args:27 username (str)28 Returns:29 user instance from mongo.30 """31 db = make_connection()32 user = db.users.find_one({'username': username}, {'_id': False})33 return user34def add_bot(username, token):35 """Add token to database. 36 Args:37 user: user object (TODO).38 token (str): token from request.39 Returns:40 telegram bot_id if added successfully.41 """42 db = make_connection()43 bot_info = get_bot_info(token)44 db.bots.insert_one({**bot_info, 'token': token})45 db.users.update_one({'username': username}, {'$push': {'bots': bot_info['bot_id']}})46 return bot_info['bot_id']47def load_bots(user):48 """Get list of bots of specific user.49 Args:50 user: user object (TODO).51 52 Returns:53 list of bots info.54 """55 db = make_connection()56 bot_ids = db.users.find_one({'username': user})['bots']57 bots = []58 for bot_id in bot_ids:59 print(db.bots.find_one({'bot_id': bot_id}, {'_id': False}))60 bots.append(db.bots.find_one({'bot_id': bot_id}, {'_id': False}))61 return bots62def remove_bot(username, bot_id):63 """Remove bot from user list.64 Args:65 username (str): username of targeted user.66 bot_id (int): id of bot to be removed.67 Returns:68 bot_id if removed successfully, None otherwise.69 """70 db = make_connection()71 user = db.users.find_one({'username': username})72 if bot_id in user['bots']:73 db.users.update_one({'username': username}, {'$pull': {'bots': bot_id}})74 db.bots.remove({'bot_id': bot_id})75 return bot_id76 return None77def get_bots_tokens():78 """Supplementary function for startup to get all bots.79 Returns:80 list of bots tokens.81 """82 db = make_connection()83 bots_tokens = []84 bots_in_db = db.bots.find({}, {'token': True})85 for el in bots_in_db:86 bots_tokens.append(el['token'])...
mydbfile.py
Source:mydbfile.py
1import pymysql as p2def make_connection():3 con=p.connect(host="localhost",user="root",password="",database="blog")4 return con567def insert_author(myt):8 con=make_connection()9 cur=con.cursor()10 query="insert into Author_table (A_UNAME,A_PASSWORD,A_CITY) values (%s,%s,%s)"11 cur.execute(query,myt)12 con.commit()13 con.close()1415def insert_user(myt):16 con=make_connection()17 cur=con.cursor()18 query="insert into User_table (U_UNAME,U_PASSWORD,U_CITY) values (%s,%s,%s)"19 cur.execute(query,myt)20 con.commit()21 con.close()2223def checkauthor(t):24 con=make_connection()25 cur=con.cursor()26 q="select * from Author_table where a_uname=%s and a_password=%s"27 cur.execute(q,t)28 data=cur.fetchall()29 return data3031def checkuser(t):32 con=make_connection()33 cur=con.cursor()34 q="select * from User_table where u_uname=%s and u_password=%s"35 cur.execute(q,t)36 data=cur.fetchall()37 return data383940def savepostfun(t):41 con=make_connection()42 cur=con.cursor()43 q="insert into Author_Post (P_UNAME,P_Title,P_post) values(%s,%s,%s)"44 cur.execute(q,t)45 con.commit()46 con.close()4748def view_spec_post(t):49 con=make_connection()50 cur=con.cursor()51 q="select * from Author_Post where P_Uname=%s"52 cur.execute(q,t)53 data=cur.fetchall()54 con.commit()55 con.close()56 return data5758def fetch_all_post():59 con=make_connection()60 cur=con.cursor()61 q="select * from Author_Post"62 cur.execute(q)63 data=cur.fetchall()64 con.commit()65 con.close()66 return data
...
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!!