Advertisement
Guest User

api.py

a guest
Mar 12th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. class DB:
  2.     def __init__(self, host, user, db, password):
  3.         self.host = host
  4.         self.user = user
  5.         self.password = password
  6.         self.db = db
  7.         self.charset = 'utf8mb4'
  8.         self.cursorclass = pymysql.cursors.DictCursor
  9.  
  10. class DataConn:
  11.     def __init__(self, db_obj):
  12.         self.host = db_obj.host
  13.         self.user = db_obj.user
  14.         self.password = db_obj.password
  15.         self.db = db_obj.db
  16.         self.charset = db_obj.charset
  17.         self.cursorclass = db_obj.cursorclass
  18.  
  19.     def __enter__(self):
  20.         self.conn = pymysql.connect(
  21.             host = self.host,
  22.             user = self.user,
  23.             password = self.password,
  24.             db = self.db,
  25.             charset = self.charset,
  26.             cursorclass = self.cursorclass
  27.         )
  28.         return self.conn
  29.  
  30.     def __exit__(self, exc_type, exc_val, exc_tb):
  31.         self.conn.close()
  32.         if exc_val:
  33.             raise
  34. db = DB(
  35.     host = config.host,
  36.     user = config.user,
  37.     password = config.password,
  38.     db = config.db
  39. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement