1.函数式编程
理论就来自lambda演算,虽然没有学过lisp,一直被其大名震撼。 html
特性: jquery
函数是以一等公民 express
能够做为参数 编程
能够做为返回值 缓存
具备闭包特性 多线程
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 闭包
-
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
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,做为第二个参数,而后将返回的字典集合做为下一次的第一个参数
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闭包
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://jinja.pocoo.org/
http://erhuabushuo.is-programmer.com/posts/33926.html
强大的模板处理引擎
- 语句块使用:{% 语句 %}
- 取值使用:{{ 值 }}
- 控制流程:
{% if title %} {{}} {% else %} {{}} {% endif %} |
{% for post in posts%} {{}} {% endfor %} |
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) |