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)

相关文章
相关标签/搜索