basic static-method python 中并没有像是 Java 里的 static 关键字,取代的是 @classmethod
和 @staticmethod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class A : name = 'A' @classmethod def a (cls ): print (cls.name) @staticmethod def b (): print ('b' ) A.a() A.b()
A
b
multi-condition if(a||b)当 a 为真时,还会对 b 求值吗?
不会
1 2 3 4 5 6 7 8 9 10 11 12 import timedef calculate (name, t ): print ("calculating: " , name, "seconds needs: " , t) time.sleep(t) return True if (calculate('A' , 2 ) or calculate('B' , 2 )): print ("Done" )
calculating: A seconds needs: 2
Done
modules base64 1 2 3 4 5 6 7 8 9 10 import base64username = 'Weidows' password = '123456' encode = base64.b64encode((username + ":" + password).encode()) headers = {"Authorization" : "Basic " + encode.decode()} print (headers)
{'Authorization': 'Basic V2VpZG93czoxMjM0NTY='}
多线程-进程 Python 多线程不能利用 CPU 多核优势,IO 密集型可用多线程,CPU 密集型适合用多进程 [2]
也就是说只有 IO 密集型任务适用多线程,其他情况: 多进程 > 不用 > 多线程
, API 区别如下:[3]
虽然但是,很多情况下涉及到资源共享, 多进程处理起来比较麻烦,还是多线程舒服.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 from concurrent.futures import ThreadPoolExecutorimport threadingimport multiprocessingimport timemulti_num = multiprocessing.cpu_count() * 2 def counter (num ): time.sleep(num) print (threading.currentThread().name, num, end='\t' ) if __name__ == '__main__' : with ThreadPoolExecutor(max_workers=multi_num) as thread_pool: start = time.time() thread_pool.map (counter, range (10 )) thread_pool.shutdown(wait=True ) print ("多线程: " , time.time() - start)
ThreadPoolExecutor-0_0 0 ThreadPoolExecutor-0_1 1 ThreadPoolExecutor-0_0 2 ThreadPoolExecutor-0_2 3 ThreadPoolExecutor-0_3 4 ThreadPoolExecutor-0_4 5 ThreadPoolExecutor-0_5 6 ThreadPoolExecutor-0_6 7 ThreadPoolExecutor-0_7 8 ThreadPoolExecutor-0_8 9 多线程: 9.009207010269165
opengl 彩色三角形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 from OpenGL.GL import *from OpenGL.GLUT import *def draw (): glBegin(GL_LINES) glColor4f(1.0 , 0.0 , 0.0 , 1.0 ) glVertex3f(-0.8 , 0.0 , 0.0 ) glVertex3f(0.8 , 0.0 , 0.0 ) glColor4f(0.0 , 1.0 , 0.0 , 1.0 ) glVertex3f(0.0 , -0.8 , 0.0 ) glVertex3f(0.0 , 0.8 , 0.0 ) glColor4f(0.0 , 0.0 , 1.0 , 1.0 ) glVertex3f(0.0 , 0.0 , -0.8 ) glVertex3f(0.0 , 0.0 , 0.8 ) glEnd() glBegin(GL_TRIANGLES) glColor4f(1.0 , 0.0 , 0.0 , 1.0 ) glVertex3f(-0.5 , -0.366 , -0.5 ) glColor4f(0.0 , 1.0 , 0.0 , 1.0 ) glVertex3f(0.5 , -0.366 , -0.5 ) glColor4f(0.0 , 0.0 , 1.0 , 1.0 ) glVertex3f(0.0 , 0.5 , -0.5 ) glEnd() glFlush() if __name__ == "__main__" : glutInit() glutCreateWindow('Quidam Of OpenGL' ) glutDisplayFunc(draw) glutMainLoop()
点线抛物线 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 from OpenGL.GL import *from OpenGL.GLU import *from OpenGL.GLUT import *from numpy import *import sysdef init (): glClearColor(1.0 , 1.0 , 1.0 , 1.0 ) gluOrtho2D(-5.0 , 5.0 , -5.0 , 5.0 ) def plotfunc (): glClear(GL_COLOR_BUFFER_BIT) glColor3f(1.0 , 0.2 , 0.6 ) glPointSize(3.0 ) glBegin(GL_POINTS) for x in arange(-5.0 , 5.0 , 0.1 ): y = x * x glVertex2f(x, y) glEnd() glFlush() def main (): glutInit(sys.argv) glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) glutInitWindowPosition(50 , 100 ) glutInitWindowSize(400 , 400 ) glutCreateWindow("Function Plotter" ) glutDisplayFunc(plotfunc) init() glutMainLoop() main()
转动时钟 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 from OpenGL.GL import *from OpenGL.GLU import *from OpenGL.GLUT import *import mathimport timeh = 0 m = 0 s = 0 def Draw (): PI = 3.1415926 R = 0.5 TR = R - 0.05 glClear(GL_COLOR_BUFFER_BIT) glLineWidth(5 ) glBegin(GL_LINE_LOOP) for i in range (100 ): glVertex2f(R * math.cos(2 * PI / 100 * i), R * math.sin(2 * PI / 100 * i)) glEnd() glLineWidth(2 ) for i in range (100 ): glBegin(GL_LINES) glVertex2f(TR * math.sin(2 * PI / 12 * i), TR * math.cos(2 * PI / 12 * i)) glVertex2f(R * math.sin(2 * PI / 12 * i), R * math.cos(2 * PI / 12 * i)) glEnd() glLineWidth(1 ) h_Length = 0.2 m_Length = 0.3 s_Length = 0.4 count = 60.0 s_Angle = s / count count *= 60 m_Angle = (m * 60 + s) / count count *= 12 h_Angle = (h * 60 * 60 + m * 60 + s) / count glLineWidth(1 ) glBegin(GL_LINES) glVertex2f(0.0 , 0.0 ) glVertex2f(s_Length * math.sin(2 * PI * s_Angle), s_Length * math.cos(2 * PI * s_Angle)) glEnd() glLineWidth(5 ) glBegin(GL_LINES) glVertex2f(0.0 , 0.0 ) glVertex2f(h_Length * math.sin(2 * PI * h_Angle), h_Length * math.cos(2 * PI * h_Angle)) glEnd() glLineWidth(3 ) glBegin(GL_LINES) glVertex2f(0.0 , 0.0 ) glVertex2f(m_Length * math.sin(2 * PI * m_Angle), m_Length * math.cos(2 * PI * m_Angle)) glEnd() glLineWidth(1 ) glBegin(GL_POLYGON) for i in range (100 ): glVertex2f(0.03 * math.cos(2 * PI / 100 * i), 0.03 * math.sin(2 * PI / 100 * i)) glEnd() glFlush() def Update (): global h, m, s t = time.localtime(time.time()) h = int (time.strftime('%H' , t)) m = int (time.strftime('%M' , t)) s = int (time.strftime('%S' , t)) glutPostRedisplay() glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA) glutInitWindowSize(400 , 400 ) glutCreateWindow("My clock" ) glutDisplayFunc(Draw) glutIdleFunc(Update) glutMainLoop()
Hello-World-pyglet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import pygletwindow = pyglet.window.Window() label = pyglet.text.Label('Hello, world' , font_name='Times New Roman' , font_size=36 , x=window.width // 2 , y=window.height // 2 , anchor_x='center' , anchor_y='center' ) @window.event def on_draw (): window.clear() label.draw() pyglet.app.run()
茶壶 动态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 from OpenGL.GL import *from OpenGL.GLU import *from OpenGL.GLUT import *def drawFunc (): glClear(GL_COLOR_BUFFER_BIT) glRotatef(0.1 , 5 , 5 , 0 ) glutWireTeapot(0.5 ) glFlush() glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA) glutInitWindowPosition(0 , 0 ) glutInitWindowSize(400 , 400 ) glutCreateWindow(b"first" ) glutDisplayFunc(drawFunc) glutIdleFunc(drawFunc) glutMainLoop()
静态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 from OpenGL.GL import *from OpenGL.GLU import *from OpenGL.GLUT import *def drawFunc (): glClear(GL_COLOR_BUFFER_BIT) glColor3f(1.0 , 0.0 , 0.0 ) glutWireTeapot(0.5 ) glFlush() glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA) glutInitWindowSize(400 , 400 ) glutCreateWindow(b"Teapot" ) glutDisplayFunc(drawFunc) glutMainLoop()
数据分析 numpy newaxis 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import numpy as npA = np.array([[2 , 0 , 2 , 1 ], [2 , 0 , 2 , 2 ]]) print (A)print ("A.shape: " , A.shape)B = A[np.newaxis, :] print (B)print ("B.shape: " , B.shape)C = A[:, np.newaxis] print (C)print ("C.shape: " , C.shape)D = A[:, np.newaxis, 3 ] print (D)print ("D.shape: " , D.shape)
[[2 0 2 1]
[2 0 2 2]]
A.shape: (2, 4)
[[[2 0 2 1]
[2 0 2 2]]]
B.shape: (1, 2, 4)
[[[2 0 2 1]]
[[2 0 2 2]]]
C.shape: (2, 1, 4)
[[1]
[2]]
D.shape: (2, 1)
借物表 [1] : How does numpy.newaxis work and when to use it?
[2] : Python threadpool与multiprocessing
[3] : Python并发实战,怎样使用多进程multiprocessing加速程序运行