?
1.函数式编程
?
理论就来自lambda演算,虽然没有学过lisp,一直被其大名震撼。
特性:
函数是以一等公民
可以作为参数
可以作为返回值
具有闭包特性
?
1.1参数传递方式
- 一般参数传递:值传递,引用传递
- 命名参数传递,使用"参数名=值"的格式,Python内成为关键字参数(keyword argument)
- 默认参数设置
- 可变参数,使用*开头,被解析成为一个元组
- 可变参数,使用**开头,被解析成为一个字典,必须使用关键字参数的方式
- 在调用的时候如何加上*,则会被解成元组或字典
- def func(*args):
- ????print type(args)
- ????print args
- ?
- func(1,2.3,‘true‘)
- ?
- def funcDict(**args):
- ????print type(args)
- ????print args
- ????print args[‘name‘]
- ?
- funcDict(name=‘pzdn‘,age=20)
?
1.2迭代器Iterator
类似C#的枚举器Enumerator
- 典型标识:next(),it.next()
- lst =range(2)
- it = iter(lst)
- ?
- try:
- ????while True:
- ????????print next(it) # it.next()
- except StopIteration:
- ????pass
?
1.3生成器
生成器就是一种迭代器
- 使用yield关键字实现迭代返回
- 和C#的yield是一样的
- 调用next方法实现迭代
- def fibonacci():
- ????a =b =1
- ????yield a
- ????yield b
- ????while True:
- ????????a,b = b, a+b
- ????????yield b
- ?
- for num in fibonacci():
- ????if num > 100: break
- ????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
官方解释:
- 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.
- def reduce(function, iterable, initializer=None):
- ??it = iter(iterable)
- ??if initializer is None:
- ????try:
- ??????initializer = next(it)
- ????except StopIteration:
- ??????raise TypeError(‘reduce() of empty sequence with no initial value‘)
- ??accum_value = initializer
- ??for x in iterable:
- ????accum_value = function(accum_value, x)
- ??return accum_value
?
- def statistics(dic,k):
- ??if not k in dic:
- ????dic[k] = 1
- ??else:
- ????dic[k] +=1
- ??return dic
- ?
- lst = [1,1,2,3,2,3,3,5,6,7,7,6,5,5,5]
- print reduce(statistics,lst,{})
- #提供第三个参数,第一次,初始字典为空,作为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()
- import thread,threading
- import time
- ?
- def print_time(threadName, delay):
- ????count =0
- ????while count < 5:
- ????????time.sleep(delay)
- ????????count +=1
- ????????print "%s %s" % (threadName,time.ctime(time.time()))
- ????????print threading.currentThread().getName()
- ?
- try:
- ????thread.start_new_thread(print_time,("T1",4))
- ????thread.start_new_thread(print_time,("T2",2))
- ?
- except:
- ?????print "Error: unable to start thread"
- ?
- print threading.enumerate()
- while 1:
- ????pass
?
Thread类
thread.exit()
thread.run()
thread.start()
?
- exitFlag =0
- class myThread(threading.Thread):
- ????def __init__(self,threadID,name,counter):
- ????????threading.Thread.__init__(self)
- ????????self.threadID = threadID
- ????????self.name = name
- ????????self.counter = counter
- ????def run(self):
- ????????print "Starting " + self.name
- ????????print_time(self.name,self.counter,5)
- ????????print "Exiting " + self.name
- ?
- def print_time(threadName, delay, counter):
- ????while counter:
- ????????if exitFlag:
- ????????????thread.exit()
- ????????time.sleep(delay)
- ????????print "%s: %s" % (threadName, time.ctime(time.time()))
- ????????counter -= 1
- ?
- thread1 = myThread(1, "Thread-1", 1)
- thread2 = myThread(2, "Thread-2", 2)
- ?
- thread1.start()
- thread2.start()
- ?
?
for t in threads:
t.join()
print
"Exiting Main Thread"
?
2.2线程同步
?
threading.Lock().acquire()
threading.Lock().release()
?
3.Jinja模板
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 ? 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 urllib.urlretrieve(url, "%s.jpg" % (i, )) ? content = downloadPage("http://tieba.baidu.com/p/2460150866") downloadImg(content) |
?