flask 数据库连接池
先预留出来

1 # class Foo(object): 2 # name='hello' 3 4 # def func(self,y): 5 # return y+1 6 # 通过type来创建类 7 Foo = type('Foo', (object,), {'name': 'hello', 'func': lambda self, y: y + 1}) 8 # 使用元类来创建类,一句话代码如上所示,type括号里面的第一个参数是所要创建的类名用字符串的格式, 9 # 紧接着的后一个参数是该类所需要继承的父类,用元祖的格式, 10 # 最后一个参数是一个大括号,里面的键值对是我们所创建的类的静态属性以及动态属性,也叫方法,PS:大括号里面的值可以为空,为空就是如下代码: 11 # class Base(object): 12 # pass 13 hq = Foo() 14 15 # print(hq.name) 16 17 """ 18 class Foo(object): 19 __metaclass__ = type # 在python2中我们的类要继承的类用__metaclass__写到类下面静态属性的位置, 20 21 name = 'peter' 22 23 def func(self, z): 24 return z + 2 25 26 27 hw = Foo() 28 29 30 class Foo1(object, metaclass=type): 31 name = 'john' 32 33 def func(self, a): 34 return a + 3 35 36 37 ss = Foo1() 38 """ 39 40 """ 41 class SheType(type): 42 def __init__(self, *args, **kwargs): 43 print('hello from outside') 44 super(SheType, self).__init__(*args, **kwargs) 45 print('hello from') 46 47 48 class Foo2(object, metaclass=SheType): 49 name = 'judy' 50 51 def func(self, q): 52 return q * 2 53 qq=Foo2() 54 print(qq.name) 55 """ 56 57 """ 58 class Fo1(object): 59 name = 'peter' 60 61 def __init__(self, name, age): 62 self.name = name 63 self.age = age 64 65 def func(self): 66 ... 67 """ 68 69 # print(Fo1.__dict__) # 这里得出的是键值对 70 # print(2,dir(Fo1)) # 这里得出的都是我们的key, 71 72 73 # ======================== 74 """ 75 # 创建类的方法 76 class SheType(type): 77 def __init__(self, *args, **kwargs): 78 print('hello from outside') 79 super(SheType, self).__init__(*args, **kwargs) 80 print('hello from') 81 82 83 class Foo2(object, metaclass=SheType): 84 85 def func(self, q): 86 return q * 2 87 88 89 class Bar(Foo2): 90 91 # 这里是type的派生类,我们按照继承顺序一直向上找到源头,type, 92 # 只要是能找到type的类,就是我们的type的派生类, 93 # 就会执行我们的type里面的init方法,除非被子类所覆盖 94 95 ... 96 97 qq=Foo2() 98 """ 99 100 """ 101 class SheType(type): 102 def __init__(self, *args, **kwargs): 103 print('hello from outside') 104 super(SheType, self).__init__(*args, **kwargs) 105 print('hello from') 106 107 108 Base = SheType('Base', (object,), {}) 109 110 111 class Foo2(object, metaclass=SheType): 112 def func(self, q): 113 return q * 2 114 115 """ 116 117 """ 118 # ===========已函数的方式继承type类======= 119 class SheType(type): 120 def __init__(self, *args, **kwargs): 121 print('hello from outside') 122 super(SheType, self).__init__(*args, **kwargs) 123 print('hello from') 124 125 126 def with_metaclass(arg): # 这里的函数名with_metaclass是我们的wtforms源码里面就写好的,就是这样用的,我们一般就使用默认的即可 127 return SheType('Base', (object,), {}) # Base = SheType('Base', (object,), {}) 128 129 130 class Foo2(with_metaclass(object)): # 我们这里把object传参过去,让该函数接收, 131 addr = 'peking' 132 133 def func(self, q): 134 return q * 2 135 """ 136 137 138 class MyType(type): 139 def __init__(self, *args, **kwargs): 140 super(MyType, self).__init__(*args, **kwargs) 141 142 143 class Foo(object, metaclass=MyType): 144 ...

1 import datetime 2 3 v = datetime.datetime.now() 4 h = datetime.timedelta(days=20) 5 new_d = v + h 6 # print(new_d) 7 8 from redis import Redis 9 10 conn = Redis(host='127.0.0.1') 11 v = conn.keys() 12 # print(v)
元类创建类方法涉及到继承:

class MyType(type): def __call__(self, *args, **kwargs): ... class Foo(object,metaclass=MyType): def __init__(self): # self.__age='89' ... def __new__(cls, *args, **kwargs): ... he = Foo() # w=he._Foo__age # print(w)

1 import pymysql 2 conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='123',db='book_list') 3 cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) 4 cursor.execute("select id,name from userinfo where pwd=%(pwd)s and name=%(name)s",{'pwd':'1234','name':'peter'}) 5 # 从数据库里取出来的所有数据都是字符串,我们都需要记上引号进行处理,我们的数据类型是用于存入的时候,但是我们取出来的话就都是字符串的类型, 6 obj=cursor.fetchone() 7 conn.commit() 8 cursor.close() 9 conn.close() 10 print(obj)

1 import time 2 import pymysql 3 import threading 4 from DBUtils.PooledDB import PooledDB,SharedDBConnection 5 POOL=PooledDB( 6 creator=pymysql, # 使用链接数据库的模块 7 maxconnections=6, # 连接池允许的最大连接数,0还有None表示不限制连接数 8 mincached=2, # 初始化时,连接池中至少创建的空闲的链接,0表示不创建, 9 maxcached=4, # 连接池中最多闲置的链接,0和None表示不限制, 10 maxshared=3, # 连接池中最多共享的连接数量,0和None表示共享所有,PS:基本不用这个参数, 11 # 因为pymysql和MySQLdb等模块的threadsafety都是1,所以无论设置这个值为多少,maxcached永远为0,也就是所有链接共享 12 blocking=True, # 连接池中如果没有可用的链接,是否阻塞等待, 13 maxusage=None, # 一个连接最多被重复使用的次数,None表示无限次 14 setsession=[], # 开始会话前执行的命令列表,如['set datestyle to,''', 'set time zone...'] 15 ping=0, #pingMySQL服务端,检查是否服务可用, # 如:0=None=never, 16 # 1=default=whenever it is requested, 17 # 2=when a cursor is created, 18 # 4=when a query is executed, 19 # 7=always 20 host='127.0.0.1', 21 port=3306, 22 user='root', 23 password='123', 24 database='book_list', 25 charset='utf8' 26 ) 27 28 def func(): 29 conn=POOL.connection() 30 cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) 31 cursor.execute('select name from userinfo') 32 ret=cursor.fetchall() 33 cursor.close() 34 conn.close() 35 return ret 36 res=func() 37 print(res)
class Foo(): """ 只要我们的类里面有这个iter方法就可以使用yield,它就是一个迭代器 """ def __iter__(self): yield 321 yield 23 yield 9 j=Foo() for i in j: print(i) # 迭代器利用for循环取值