Python學習筆記10

 

1.函數式編程

 

理論就來自lambda演算,雖然沒有學過lisp,一直被其大名震撼。 html

特性: jquery

函數是以一等公民 express

能夠做爲參數 編程

能夠做爲返回值 緩存

具備閉包特性 多線程

 

1.1參數傳遞方式

  • 通常參數傳遞:值傳遞,引用傳遞
  • 命名參數傳遞,使用"參數名=值"的格式,Python內成爲關鍵字參數(keyword argument)
  • 默認參數設置
  • 可變參數,使用*開頭,被解析成爲一個元組
  • 可變參數,使用**開頭,被解析成爲一個字典,必須使用關鍵字參數的方式
  • 在調用的時候如何加上*,則會被解成元組或字典
  1. def func(*args):
  2.     print type(args)
  3.     print args
  4.  
  5. func(1,2.3,'true')
  6.  
  7. def funcDict(**args):
  8.     print type(args)
  9.     print args
  10.     print args['name']
  11.  
  12. funcDict(name='pzdn',age=20)

 

1.2迭代器Iterator

相似C#的枚舉器Enumerator 閉包

  • 典型標識:next(),it.next()
  1. lst =range(2)
  2. it = iter(lst)
  3.  
  4. try:
  5.     while True:
  6.         print next(it) # it.next()
  7. except StopIteration:
  8.     pass

 

1.3生成器

生成器就是一種迭代器 框架

  • 使用yield關鍵字實現迭代返回
  • 和C#的yield是同樣的
  • 調用next方法實現迭代
  1. def fibonacci():
  2.     a =b =1
  3.     yield a
  4.     yield b
  5.     while True:
  6.         a,b = b, a+b
  7.         yield b
  8.  
  9. for num in fibonacci():
  10.     if num > 100: break
  11.     print num

1.4 enumerate

enumerate相似jquery的$.each 函數式編程

for idx, ele in enumerate(lst): 函數

   print idx, ele

 

1.5lambda

屬於匿名函數。

  • lambda args: expression。第一個是關鍵字,第二個是逗號分隔的參數,冒號以後是表達式塊

 

1.6map

  • map,按照func計算以後返回

 

print map(lambda x:x**3,range(1,6))

print map(lambda x:x+x,'abcde')

print map(lambda x,y:x+y,range(8),range(8))

 

1.7filter

  • 相似Linq的Where擴展方法,選擇爲true的進行計算

print filter(lambda x:x%2 != 0 and x%3 != 0,range(2,20))

1.8reduce

官方解釋:

  1. Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.
  2. def reduce(function, iterable, initializer=None):
  3.   it = iter(iterable)
  4.   if initializer is None:
  5.     try:
  6.       initializer = next(it)
  7.     except StopIteration:
  8.       raise TypeError('reduce() of empty sequence with no initial value')
  9.   accum_value = initializer
  10.   for x in iterable:
  11.     accum_value = function(accum_value, x)
  12.   return accum_value

 

  1. def statistics(dic,k):
  2.   if not k in dic:
  3.     dic[k] = 1
  4.   else:
  5.     dic[k] +=1
  6.   return dic
  7.  
  8. lst = [1,1,2,3,2,3,3,5,6,7,7,6,5,5,5]
  9. print reduce(statistics,lst,{})
  10. #提供第三個參數,第一次,初始字典爲空,做爲statistics的第一個參數,而後遍歷lst,做爲第二個參數,而後將返回的字典集合做爲下一次的第一個參數

 

  • 累加地計算,才能"reduce"減小

print reduce(lambda x,y:x+y,range(1,101)) #5050

print reduce(lambda x,y:x+y,range(1,101),20) #5070

 

1.9閉包

  • 相似js.一個做用就是訪問做用於的私有變量

    另外,"閉包是綁定了外部做用域的變量(但不是全局變量)的函數。即便程序運行至離開外部函數,若是閉包仍然可見,則被綁定的變量仍然有效;每次運行至外部函數,都會從新建立閉包,綁定的變量是不一樣的,不須要擔憂在舊的閉包中綁定的變量會被新的值覆蓋。"——摘自

 

2.多線程

 

2.1簡單使用

 

threading.currentThread()

threading.enumerate()

thread.start_new_thread()

  1. import thread,threading
  2. import time
  3.  
  4. def print_time(threadName, delay):
  5.     count =0
  6.     while count < 5:
  7.         time.sleep(delay)
  8.         count +=1
  9.         print "%s %s" % (threadName,time.ctime(time.time()))
  10.         print threading.currentThread().getName()
  11.  
  12. try:
  13.     thread.start_new_thread(print_time,("T1",4))
  14.     thread.start_new_thread(print_time,("T2",2))
  15.  
  16. except:
  17.      print "Error: unable to start thread"
  18.  
  19. print threading.enumerate()
  20. while 1:
  21.     pass

 

Thread類

thread.exit()

thread.run()

thread.start()

 

  1. exitFlag =0
  2. class myThread(threading.Thread):
  3.     def __init__(self,threadID,name,counter):
  4.         threading.Thread.__init__(self)
  5.         self.threadID = threadID
  6.         self.name = name
  7.         self.counter = counter
  8.     def run(self):
  9.         print "Starting " + self.name
  10.         print_time(self.name,self.counter,5)
  11.         print "Exiting " + self.name
  12.  
  13. def print_time(threadName, delay, counter):
  14.     while counter:
  15.         if exitFlag:
  16.             thread.exit()
  17.         time.sleep(delay)
  18.         print "%s: %s" % (threadName, time.ctime(time.time()))
  19.         counter -= 1
  20.  
  21. thread1 = myThread(1, "Thread-1", 1)
  22. thread2 = myThread(2, "Thread-2", 2)
  23.  
  24. thread1.start()
  25. thread2.start()
  26.  

 

for t in threads:

t.join()

print "Exiting Main Thread"

 

2.2線程同步

 

threading.Lock().acquire()

threading.Lock().release()

 

3.Jinja模板

http://jinja.pocoo.org/

http://erhuabushuo.is-programmer.com/posts/33926.html

強大的模板處理引擎

  • 語句塊使用:{% 語句 %}
  • 取值使用:{{ 值 }}
  • 控制流程:

{% if title %}

{{}}

{% else %}

{{}}

{% endif %}

 

  • 循環流程:

{% for post in posts%}

{{}}

{% endfor %}

 

  • 模板繼承

    {% block content %}{% endblock %}

     

    {% extends "base.html" %}

    {% block content %}

    {% endblock %}

 

import jinja2

 

template = jinja2.Template('Hello, {{name}}')

print template.render(name="pzdn")

 

 

4.簡單爬蟲框架

 

urllib:

參考:http://www.cnblogs.com/sysu-blackbear/p/3629420.html

  • urllib.urlopen(url[,data[,proxies]])

    打開一個url,返回一個文件對象。而後能夠進行相似文件對象的操做

  • urllib.urlretrieve(url[,filename[,reporthook[,data]]])

    將url定位到的html文件下載到你本地的硬盤中。若是不指定filename,則會存爲臨時文件。

    urlretrieve()返回一個二元組(filename,mine_hdrs)

  • urllib.urlcleanup()

    清除緩存

  • urllib.quote(url)和urllib.quote_plus(url)

    url編碼

  • urllib.unquote(url)和urllib.unquote_plus(url)

    url解碼

  • urllib.urlencode(query)

    對查詢參數編碼

import urllib

import re

 

def downloadPage(url):

h = urllib.urlopen(url)

return h.read()

 

def downloadImg(content):

pattern = r'src="(.+?\.jpg)" pic_ext'

m = re.compile(pattern)

urls = re.findall(m, content)

 

for i, url in enumerate(urls):

urllib.urlretrieve(url, "%s.jpg" % (i, ))

 

content = downloadPage("http://tieba.baidu.com/p/2460150866")

downloadImg(content)

相關文章
相關標籤/搜索