5t语录

总结定义,经验和结论的语句的目的在于1)当不记得目前的技术点时可以由此推衍。2)形成一定的思维定势。

主要来源:收录武沛奇先生课上经典语句。

1、生成器

    5t:只要函数中有yield,这个函数就是一个生成器函数。

    5t:a = Foo(),a就叫做一个生成器。

    5t:编写程序一般,有几次yield,就执行几次__next__()

  5t:下一次的next直接从yield处执行。zi:同样从再下一次yield处出来。

    zj:yield记录函数执行位置,yield a ,__next__返回当次a

    

 1 #! /usr/bin/env python
 2 
 3 def xrange(n):
 4     print 'i'
 5     i = 0
 6     print i
 7     while True:
 8         
 9         if i < n:
10             
11             yield i
12             i = i+1
13 a = xrange(10)
14 print dir(a)
15 t = 0
16 while t<10:
17     v = a.next()
18     print v
19     t = t+1
20 
21 >>> 
22 ====================== RESTART: C:/Python27/shengch.py ======================
23 ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'gi_code', 'gi_frame', 'gi_running', 'next', 'send', 'throw']
24 i
25 0
26 0
27 1
28 2
29 3
30 4
31 5
32 6
33 7
34 8
35 9
36 >>> 
View Code

      关于yield和执行语句的问题

      数值在yield 后当次即可返回

      想执行语句 一般放在yield前

 1 #! /usr/bin/env python
 2 
 3 import threading
 4 import Queue
 5 import time
 6 
 7 class ThreadPool:
 8     
 9     def __init__(self,num = 20):
10         self.que = Queue.Queue(num)
11         i = 0
12         while True:       
13             if i < num:
14                 self.que.put(threading.Thread)
15                 yield i
16                 i = i+1
17                 #self.que.put(threading.Thread)
18        
19 
20     def __new__(self):
21         
22 
23 
24 
25         
26     def get_thd(self):
27         return self.que.get()
28 
29     
30     def put_thd(self):
31         self.que.put(threading.Thread)
32 
33         
34     def thd_count(self):
35         return self.que.qsize()
36 
37     
38     def full_thd(self):
39         return self.que.full()
40 
41 
42 def Foo(pool,parameter):
43     time.sleep(1)
44     print parameter*100
45     pool.put_thd()
46 
47 thdp = ThreadPool()
48 
49 
50 for i in xrange(100):
51     
52     rt = thdp.get_thd()
53 
54     gun_thd=rt(target = Foo,args = (thdp,i,))
55     gun_thd.start()
56     thdp_count = thdp.thd_count()
57     print thdp_count
58 thdp_count = thdp.thd_count()
59 print thdp_count
View Code

 2、python中__new__

     5t:python 中 一切皆对象。

           类也是对象

          类是由谁创建的呢?默认情况下是由type创建。

    变量=type(‘类名’,[基类],{类成员}) --  tpye也是一个类,说明在创建新的类的时候,也执行了type中的构造方法。

          解释器从上到下解释当遇到 class def__init__时,自动执行type中的构造方法

                                   当遇到f = Foo()时,自动执行,type的__call__方法,object.new方法创建obj,然后执行Foo里的init方法。

                                   object.new 是调用C的程序。而暴露出来的就是 都由object.new创建对象。

         作用:定制类。 zj:在执行类或是对象时就能额外加内容,相当于装饰器。

 摘录自:  

录播地址

Mysqllib数据库     讲解new    63分钟

http://edu.51cto.com/px/video/571

 

3、线程和进程

线程和进程原理想不明白的时候想想武老实的甄嬛传。

5t:cpu是皇上,内存是皇城,主进程是皇后的宫殿(也是第一个宫殿),主线程是皇后,子进程是娘娘的宫殿,子线程在每个宫殿里都能有。

      同一个宫的线程共享进程里的资源。

      要完成执行应用程序的任务,只有皇后娘娘和贵人。自定义线程,通过主线程才能创造子线程。 即只有皇后才能立娘娘和贵人。

      线程是执行程序的最小单元。

5T:为什么分出那么多宫,为了提高效率。

5T:I/O密集型适合用多线程,计算密集型适合多进程。本质是I/O不用cpu,CPU只进入通向每一个进程。

5t:GIL 为了锁一个进程中的多个线程。

 4、5t:如果一旦搞不懂流程,就从解释器开始说。   1)代码从上到下执行,由编译器来解释语句。2)默认就由主线程来从上到下执行这些语句。3)再次创建的都是子线程。

t = threading.Thread(target = f2 ,args= (1,)) ------------线程实例创建
t.start()                        ----------------------------------实例启动状态    猜想 主线程执行到start 然后又执行到下一个start直到最后一个,然后将这些状态都报给IO最终处理者 或是计算最终处理者  各种cpu,(或者是负责这个进程的CPU),统一分配好后,开始执行所有线程。
t = threading.Thread(target = f2 ,args= (1,))
t.start()
t = threading.Thread(target = f2 ,args= (1,))
t.start()

是否还会等线程程序中的time.sleep()?会。被放入线程的 程序相当于火药(各种弹药穿甲的,爆炸的等等),而线程也可以理解成是装了弹药的子弹。而队列就是机枪的链条梭子(先进先出)。

4、队列

5T:1)queue  内存级别的队列。2)优点为提前存储线程。3)长度,get,get_nowait

5、进程

5T:window下不支持创建进程

6、json

5T:python中的json处理,型似字典和列表的字符串的时候,这个字典和列表中的元素,如果是字符串,必须使用双引号。最外层保证是一个字符串即可,最外层两侧使用单引号和双引号都可以。

 1 #-*- coding:utf-8 -*-
 2 import json
 3 di = """{"first":"123","sec":"abc"}"""
 4 ll = '["a","b",12]'
 5 
 6 ret = json.loads(di)
 7 print ret
 8 print type(ret)
 9 
10 r = json.loads(ll)
11 print r
12 print type(r)
View Code

内部元素为单引号时出现错误。

 1 #-*- coding:utf-8 -*-
 2 import json
 3 di = """{"first":"123","sec":'abc'}"""
 4 ll = '["a","b",12]'
 5 
 6 ret = json.loads(di)
 7 print ret
 8 print type(ret)
 9 
10 r = json.loads(ll)
11 print r
12 print type(r)
13 
14 
15 ====================== RESTART: C:/Python27/trainer.py ======================
16 
17 Traceback (most recent call last):
18   File "C:/Python27/trainer.py", line 6, in <module>
19     ret = json.loads(di)
20   File "C:\Python27\lib\json\__init__.py", line 339, in loads
21     return _default_decoder.decode(s)
22   File "C:\Python27\lib\json\decoder.py", line 364, in decode
23     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
24   File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
25     raise ValueError("No JSON object could be decoded")
26 ValueError: No JSON object could be decoded
27 >>> 
View Code

 5T:json.loads 将字符串转换为python基本数据类型。

  json.dumps将python 数据类型转换为 字符串。     客户端与服务器之间的传输。

   内存数据转换用 loads,dumps

  硬盘中数据用 load,dump

  与pickle区别是  可能写入到文件中数据类型为一个字符串但是 形式还是原来的数据类型,字典或是列表。这一特性可能会有用处。

 

          

posted on 2016-07-24 10:28  lexn  阅读(244)  评论(0)    收藏  举报

导航