CC-BY-SA 4.0
大部分内容来自”a byte of python”,在此感谢作者。
函数除了可以按照顺序传递参数,还可以按照名称传递参数。
这可以有效解决我们在“默认参数”一节提到过的问题。
即函数大量参数有默认值,导致想在某个参数上传递一个不同的值,需要将前面的参数默认值全部传递一遍的窘境。
from __future__ import print_function
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c=24)
func(c=50, a=100)
a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
一般而言,比较容易理解的次序是:
根据上面的规则,我们可以得到一些结论:
有时我们需要接收不确定长度个参数。例如sum函数。
或者是出于某种理由,不想处理用户的所有输入。
这时我们可以用可变参数来接收或传递参数。
可变参数的基本写法是function_name(arguments, *argument_list, arguments_withname, **arguments_dict)。
def any_arguments(*p, **kw):
print(p)
print(kw)
any_arguments(1, 2, 3, 4, 5, a=1, b=2, c=3, d=4, e=5)
(1, 2, 3, 4, 5)
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
def sum(a, b, *p):
s = a + b
for i in p: s += i
return s
print(sum(1, 2))
print(sum(1, 2, 3))
3 6
def sum(l, inital=0):
s = inital
for i in l: s += i
return s
print(sum(range(101)))
5050
什么叫做“第一类”?
就是“一等公民”的意思。在使用上和其他值一样,没有任何区别和歧视。
第一类函数对象的意思就是,函数对象在使用上和其他对象一样。
你是否想象过,将函数作为普通对象使用?像普通对象一样,能够定义,作为参数传递,作为结果传出。
op_set = {
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: float(x)/y,
}
print(op_set['+'](1, 2))
3
python的作用域基本原则是,内层可以访问外层变量,外层不能访问内层。在两层的时候,只有局部和全局。
但是如果考虑在函数内可以定义函数,而且被定义出来的函数还可以作为结果值返回。
那么这个“在内层定义的函数”,除了访问自己的局部变量和全部变量外,是否可以访问定义他的函数的“局部变量”呢?
这种使得嵌套定义函数可访问上个作用域数据的定义,被称为“闭包”。
def addn(n):
def add(x):
return x+n
return add
add10 = addn(10)
print(add10(50))
60
正常来说,局部变量在函数结束时消亡。
然而如果在内层嵌套函数中需要访问的话,那么函数结束时局部变量也不能消亡,否则内层函数去访问什么呢?
带有闭包的函数,局部变量会被附带在刚刚生成函数上,随着这个函数对象的消亡而消亡。
因此,闭包是一种带环境数据的可执行对象。这是闭包和普通函数最大的区别。
闭包是带数据的方法,类是带方法的数据,两者非常类似。可以选用简单的那个。
在需要暴露内部数据,数据附带的方法比较复杂时,建议选用类。反之,可以选用闭包。
class addn(object):
def __init__(self, n):
self.n = n
def __call__(self, x):
return x + self.n
add10 = addn(10)
print(add10(50))
60
在加入闭包后,我们可以得到完整的变量查询顺序。
局部,闭包中,全局,内置。四个名称的字母简写为LEGB。
LEGB - Local Enclosing Global Builtins
my_variable = 1
def func():
my_variable = 2
print(my_variable)
func()
2
my_variable = 1
def func():
my_variable = 2
def func1():
my_variable = 3
print(my_variable)
return func1
func()()
3
装饰器方法允许你重写某个函数的执行过程,在实际函数执行前后执行一些自己的辅助代码。
从而允许你将部分功能附加在其他函数上。或者运用这种手法对函数的共同行为进行剥离。
这种重写过程,可以用闭包或类来实现。
from __future__ import print_function
class print_arg(object):
def __init__(self, f):
self.f = f
def __call__(self, *p, **kw):
print(p, kw)
return self.f(*p, **kw)
def add(a, b):
return a+b
add = print_arg(add)
print(add(10, 20))
(10, 20) {}
30
def print_arg(f):
def inner(*p, **kw):
print(p, kw)
return f(*p, **kw)
return inner
def add(a, b):
return a+b
add = print_arg(add)
print(add(10, 20))
(10, 20) {}
30
由于装饰器在python中经常使用,因此python定义了专门的语法来方便使用。
@print_arg
def add(a, b):
return a+b
print(add(10, 20))
(10, 20) {}
30
fib计算函数的运算很慢,因为fib在计算某个参数时,会重复执行很多次更低值的计算。
请设计一个装饰器,加速这个执行过程。并对比在加和不加这个装饰器下的效率。
def fib(n):
if n <= 1: return 1
return fib(n-1) + fib(n-2)
%time fib(35)
CPU times: user 3.4 s, sys: 16 ms, total: 3.42 s Wall time: 3.41 s
14930352
def memorized(f):
cache = {}
def inner(n):
if n not in cache:
cache[n] = f(n)
return cache[n]
return inner
@memorized
def fib(n):
if n <= 1: return 1
return fib(n-1) + fib(n-2)
%time fib(35)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns Wall time: 78 µs
14930352
请考虑上述装饰器是否能用于其他函数。
如果不能,请修改上述装饰器,使其能够用于其他函数。并说明其适用范围。
def memorized(f):
cache = {}
def inner(*p):
if p not in cache:
cache[p] = f(*p)
return cache[p]
return inner
@memorized
def fib(n):
if n <= 1: return 1
return fib(n-1) + fib(n-2)
%time fib(35)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns Wall time: 81.1 µs
14930352
在python中,我们时常需要生成一个非常大的序列数据并返回。例如帖子的列表,用户的ID,等等。
正常我们都是获得数据并生成一个list返回。但是很多时候,这个序列数据是近乎于无限的。
从性能角度来说,我希望使用一个计算一个,不需要为了全部列表而耗费资源。
从使用角度来说,我希望这个列表和原来的列表使用起来没有什么太大区别。两者最好做到可以互相替换。
幸好,python为我们提供了生成器这么一种功能。
def fib_seq(n):
l = []
a, b = 1, 1
for i in range(n):
a, b = a+b, a
l.append(a)
return l
print(fib_seq(20))
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711]
def fib_seq(n):
a, b = 1, 1
for i in range(n):
a, b = a+b, a
yield a
print(fib_seq(20))
print(list(fib_seq(20)))
<generator object fib_seq at 0x7f9a124368c0> [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711]
既然生成器机制允许我们一个个获得数据,那么我们定义一个“无限长”的list也是可以的。
只要不真的去使用所有数据,就不会产生死循环。
def fib_seq():
a = b = 1
while True:
a, b = a+b, a
yield b
def size_limited_seq(seq, n):
for i in seq:
if n <= 0: return
yield i
n -= 1
print(list(size_limited_seq(fib_seq(), 10)))
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def number_limited_seq(seq, n):
for i in seq:
if i >= n: break
yield i
print(list(number_limited_seq(fib_seq(), 100000)))
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025]
# 和前面定义的sum函数联用,sum函数支持生成器
print(sum(number_limited_seq(fib_seq(), 100000)))
196416
# python2下无需list,但是python3下map返回生成器
print(list(map(lambda x: x*x, range(20))))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
# python2下无需list,但是python3下filter返回生成器
print(list(filter(lambda x: x % 2 == 0, range(20))))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 在python2下,无需import。
# python3中,reduce已经被从全局命名空间中移除了,所以需要import。
from functools import reduce
reduce(lambda x, y: x*y, range(1, 20))
121645100408832000
三者都可以完成对list操作的特定功能,例如筛选出其中某些值,将其中的值转换为另一个值。但是三者在便利程度和使用细节上略有不同。
map/filter和列表推导式比较简短,适用于相对不是很复杂的情况。真正复杂的情况需要用for来展开。
map/filter和列表推导式基本假定是“运算不彼此干扰”。即运算某个元素时,不受到其他元素的影响。for没有这一假定,某元素运算时可以受到另一个元素影响。
一般来说列表推导式比map/filter更简洁一些,因为map/filter需要定义lambda。但是在已经拥有现成函数的前提下,map/filter更加方便一些。
print([x*x for x in range(20)])
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
l = []
for x in range(20):
l.append(x*x)
print(l)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
print([x for x in range(20) if x % 2 == 0])
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
l = []
for x in range(20):
if x % 2 == 0:
l.append(x)
print(l)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
写一个计算器,能够计算简单的加减乘除。(不考虑优先级)
op_set = {
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: float(x)/y,
}
def parser_exp(exp):
s = ''
for c in exp:
if c in op_set:
yield s
s = ''
yield c
else:
s += c
if s: yield s
print(list(parser_exp('1+2')))
['1', '+', '2']
def eval_exp(exp):
num = None
s = parser_exp(exp)
for e in s:
if e in op_set:
r = float(next(s))
num = op_set[e](num, r)
else:
num = float(e)
return num
print(eval_exp('2*3+1'))
print(eval_exp('1+2*3'))
7.0 9.0
在Python中,要分清“可变对象”和“不可变对象”。以及“对象引用”和“对象复制”。
对不可变对象而言,引用和复制并没有太大区别——反正弄来弄去都是同一个对象。
而对于可变对象,对于引用的修改会同时作用于原始对象。对复制的修改并不影响原始对象。
from __future__ import print_function
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist
del shoplist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
print('Copy by making a full slice')
mylist = shoplist[:]
del mylist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
Simple Assignment shoplist is ['mango', 'carrot', 'banana'] mylist is ['mango', 'carrot', 'banana'] Copy by making a full slice shoplist is ['mango', 'carrot', 'banana'] mylist is ['carrot', 'banana']
可变对象的拷贝是一件耗时而重要的工作。
那么,如果在可变对象内有另一个可变对象呢?例如在list里嵌套了list?
只对原始的list进行复制的行为,叫做浅拷贝。对多层对象进行拷贝的行为,叫做深拷贝。
from __future__ import print_function
import copy
orig_list = [1, 2, 3, [4, 5, 6]]
new_list = copy.deepcopy(orig_list)
print(new_list)
new_list[3].append(7)
new_list.append(8)
print(new_list)
print(orig_list)
print('-----------')
new_list = copy.copy(orig_list)
print(new_list)
new_list[3].append(7)
new_list.append(8)
print(new_list)
print(orig_list)
[1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6, 7], 8] [1, 2, 3, [4, 5, 6]] ----------- [1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6, 7], 8] [1, 2, 3, [4, 5, 6, 7]]
编写一个函数,将输入列表反序输出。
禁止用系统反向函数,例如reversed和list.reverse。
l = [1,2,3,4,5]
def my_reverse(l):
for i in range(1, len(l)+1):
yield l[len(l)-i]
print(list(my_reverse(l)))
[5, 4, 3, 2, 1]
修改上面的函数,将输入列表就地反序,不产生输出。
l = [1,2,3,4,5]
def my_reverse(l):
t = l[:]
del l[:]
for i in range(1, len(t)+1):
l.append(t[len(t)-i])
my_reverse(l)
print(l)
[5, 4, 3, 2, 1]
构造一颗自循环二叉树。
l = []
l.append(l)
l.append(l)
print(l)
[[...], [...]]
思考一下,如果只用tuple,是否能够完成自循环树的构造。
前面我们说过,对于list的处理常常可以归结为map/filter模式,而对于map/filter的复杂操作往往最好使用for来展开。
但是实际工作中,我们常常会发现,我们需要对list进行复杂处理,每层处理都需要用for。
而且每层处理都是可选的,总体处理过程需要根据条件进行拼装。
大家可能还记得“无限生成器”一节的size_limited_seq和number_limited_seq吧。
这种对list进行处理的模式称为链式范式——输入一个list,经过处理链条上一层层的转化,最后形成输出list。
每一个环节的输入和输出都是一个list——或其等效物生成器。因而每个环节都可以和其他环节拼装在一起。
类似的模式还在“计算器”一节出现过。
定义一些类,描述平行四边形,矩形,正方形。并按照你的理解实现继承关系。
不用实现方法和属性。
为刚刚的“平行四边形”,“矩形”,“正方形”类,添加以下属性和方法:
考虑一下,刚刚你实现的类中,“设定夹角”方法,对于“长方形”和“正方形”是一个合理的方法么?
思考一下,哪个原则错了?
增加一个用于计算面积的函数,考虑是否可以复用呢?
现在实现一个方法,判断两个对象是否相等。
编码方案基本分为两类:变长和定长。
字符集兼容性:GB18030下行兼容GBK,GBK下行兼容GB2312。GB18030兼容Unicode3.1中日韩表意文字区。
编码兼容性:UCS-2/UCS-4互相不兼容。UTF-8独立。GB18030的编码方式下行兼容GB2312/GBK。除了UCS-2/UCS-4以外,都兼容ascii。
b"hello world"
'hello world'
type(b"hello world")
str
u"hello world"
u'hello world'
type(u"hello world")
unicode
# encoding=utf-8
import io
f = io.open("abc.txt", "wt", encoding="utf-8")
f.write(u"想象一下这里是某些中文内容,也可能是日文或者韩文")
f.close()
# 如果在windows下,这个行为反而可能出现乱码。因为windows的默认编码为CP936,而不是UTF-8。
text = io.open("abc.txt", encoding="utf-8").read()
print(text)
想象一下这里是某些中文内容,也可能是日文或者韩文
!ls -l abc.txt
-rw-r--r-- 1 shell shell 72 9月 5 18:14 abc.txt
!cat abc.txt
想象一下这里是某些中文内容,也可能是日文或者韩文
# encoding=utf-8
import io
f = io.open("abc.txt", "wt", encoding="gbk")
f.write(u"想象一下这里是某些中文内容,也可能是日文或者韩文")
f.close()
text = io.open("abc.txt", encoding="gbk").read()
print(text)
text = open("abc.txt").read()
print(text)
text = io.open("abc.txt", encoding="utf-8").read()
print(text)
想象一下这里是某些中文内容,也可能是日文或者韩文 ����һ��������ijЩ�������ݣ�Ҳ���������Ļ��ߺ���
--------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) <ipython-input-42-d2ca517a40b2> in <module>() 12 print(text) 13 ---> 14 text = io.open("abc.txt", encoding="utf-8").read() 15 print(text) /usr/lib/python2.7/codecs.pyc in decode(self, input, final) 312 # decode input (taking the buffer into account) 313 data = self.buffer + input --> 314 (result, consumed) = self._buffer_decode(data, self.errors, final) 315 # keep undecoded input until the next call 316 self.buffer = data[consumed:] UnicodeDecodeError: 'utf8' codec can't decode byte 0xcf in position 0: invalid continuation byte
!ls -l abc.txt
-rw-r--r-- 1 shell shell 48 9月 5 18:14 abc.txt
!cat abc.txt
����һ��������ijЩ�������ݣ�Ҳ���������Ļ��ߺ���
写一个程序,将文件从指定编码转换成指定编码。
with open('abc.txt', 'rb') as fi:
data = fi.read()
data = data.decode('gbk').encode('utf-8')
with open('abc.txt', 'wb') as fo:
fo.write(data)
!cat abc.txt
想象一下这里是某些中文内容,也可能是日文或者韩文
!rm -f abc.txt
为上述程序增加猜测编码功能。
注:本题目需要自行查阅chardet库的文档。这是一个猜测字符串编码的库。
# encoding=utf-8
import chardet
text = u"想象一下这里是某些中文内容,也可能是日文或者韩文".encode('gbk')
print(text)
print(chardet.detect(text))
text = text.decode(chardet.detect(text)['encoding'])
print(text)
print(type(text))
����һ��������ijЩ�������ݣ�Ҳ���������Ļ��ߺ���
{'confidence': 0.99, 'encoding': 'GB2312'}
想象一下这里是某些中文内容,也可能是日文或者韩文
<type 'unicode'>
# encoding=utf-8
import chardet
text = u"想象一下这里是某些中文内容,也可能是日文或者韩文".encode('utf-8')
print(text)
print(chardet.detect(text))
text = text.decode(chardet.detect(text)['encoding'])
print(text)
print(type(text))
想象一下这里是某些中文内容,也可能是日文或者韩文
{'confidence': 0.99, 'encoding': 'utf-8'}
想象一下这里是某些中文内容,也可能是日文或者韩文
<type 'unicode'>
如果需要将一系列内容表述成文字,我们可以很方便的使用format来达成这一目标。
然而如果是表达完成的文字解析为内容(例如int)呢?format是否有一个反操作,能让我们方便的解析内容。
当然,最傻的办法就是手写文字解析器。时间成本巨大,而且可变性很差。
我们希望一种方法,能够使用和format里的语句“差不多”的东西完整这个功能。
幸运的是,我们可以用正则表达式完成类似的功能(当然,有很大区别)。
正则表达式是用于从文本中提取内容的表达式,你可以认为他是从字符串中提取字符串的字符串。
在搜索文件时,我们经常会用*和?表示适配一个或多个字符。
正则的思路和这个类似,但是更加复杂一些。他不但能表示匹配一个或多个字符,而且还能限定匹配哪个或者哪些字符,匹配多少。
He was carefully disguised but captured quickly by police.
\w+ly
'carefully', 'quickly'
可以用[...]的语法来匹配自定义内容,例如[a-z0-9]*,表示小写或数字重复任意次。
可以用^表示求反,除去这个字符不匹配。
在正则表达式中,可以用(...)的语法表示捕获部分内容。
例如([^/]*),表示匹配直到/之前的所有内容,并且捕获。
被捕获的内容可以后续使用(例如用于内容替换,像sed),或者在一次匹配中或的多个内容(例如在re库中使用groups,后面会讲)。
常规来说,正则会匹配“尽量长”的字符。例如我们用(h.*o),试图匹配hello, fox中的hello。但是实际结果是hello, fo。
可以在匹配规则后面加?,表示非贪婪匹配。
此时正则会尽力匹配“尽量短”的字符。例如(h.*?o)去匹配hello, fox,此时得到hello。
写出一个正则表达式,匹配手机号码。
写出一个正则表达式,匹配ipv4地址。
观看下面两个范例,解释逻辑:
匹配url: (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*)?
匹配email地址: [a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?
import sys
print(sys.version_info)
print(sys.version_info.major == 3)
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0) False
import os
import platform
import logging
print('normal output')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("Start of the program")
logging.info("Doing something")
logging.warning("Dying now")
DEBUG:root:Start of the program INFO:root:Doing something WARNING:root:Dying now
normal output
re是一个用来处理正则表达式的库。
import re
s = 'He was carefully disguised but captured quickly by police.'
r = re.compile('\w+ly')
r.findall(s)
['carefully', 'quickly']
match一定从字符串的头开始匹配,search搜索全部文本。search即使用了^,也可能匹配到一个新行。
因此,要匹配头部,或者整个完整字符串,需要用match。而要在文字中搜索,需要用search。
细节:从复杂性上看,理所当然的,match更快。
请写程序验证在“正则表达式”一节中所写的那些正则。
import re
r = re.compile('1\d{10}')
print(r.match('13512345678'))
print(r.match('021-10101010'))
<_sre.SRE_Match object at 0x7f9a0819a7e8> None
import re
r = re.compile(r'\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}')
print(r.match('192.168.0.1'))
print(r.match('1200.5.4.3'))
print(r.match('abc.def.ghi.jkl'))
print(r.match('192.168.0.999'))
<_sre.SRE_Match object at 0x7f9a0819a988> None None <_sre.SRE_Match object at 0x7f9a0819a988>
import re
# source: http://c.biancheng.net/cpp/html/1435.html
r = re.compile(r'(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*)?')
print(r.match('http://regxlib.com/Default.aspx'))
print(r.match('regxlib.com'))
<_sre.SRE_Match object at 0x7f9a123f53c0> None
import re
# source: http://bbs.csdn.net/topics/60032440
r = re.compile(r'[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?')
print(r.match('shell999@gmail.com'))
print(r.match('000@shell@1'))
<_sre.SRE_Match object at 0x7f9a123f5750> None
import pickle
shoplistfile = 'shoplist.data'
shoplist = ['apple', 'mango', 'carrot']
f = open(shoplistfile, 'wb')
pickle.dump(shoplist, f)
f.close()
del shoplist
f = open(shoplistfile, 'rb')
storedlist = pickle.load(f)
print(storedlist)
['apple', 'mango', 'carrot']
!ls -l shoplist.data
-rw-r--r-- 1 shell shell 46 9月 5 18:14 shoplist.data
!rm shoplist.data
不要写任何代码,求出攻占攻占巴士底狱发生在周几?
提示:攻占巴士底狱发生在西历1789年7月14日。
import datetime
print(datetime.date(1789, 7, 14).weekday())
1
帮助文档看这里。
算出2**15有多少位数。2**11515呢?2**115151515呢?
2**15
32768
len(str(2**15))
5
2**1515
1149326528034702581682260802282483154731794952366680598907315749676350303455258941969435226491616272765850432529872896570140083779714087641224676603265178037383658094111263859162703551253166913832114001816457662609862015656425761751871770440149072630404384442015791262449967076912697519498726922973530707535919651560412212344672313695699099755325593751836252686364599363077472034064061672117649734107141736148353976201394067293417855894280067914417745952768L
len(str(2**1515))
457
# log(2**15151515) == log2 * 15151515 == log10 * (log2/log10) * 15151515
# 2**15151515 == 10**((log2/log10) * 15151515)
import math
print((math.log(2)/math.log(10)) * 15)
print(int((math.log(2)/math.log(10)) * 15) + 1)
print((math.log(2)/math.log(10)) * 1515)
print(int((math.log(2)/math.log(10)) * 1515) + 1)
print((math.log(2)/math.log(10)) * 15151515)
print(int((math.log(2)/math.log(10)) * 15151515) + 1)
4.51544993496 5 456.060443431 457 4561060.49475 4561061
import math
f = lambda x: int((math.log(2)/math.log(10)) * x) + 1
print(f(15))
print(f(1515))
print(f(15151515))
5 457 4561061
做一个抽奖程序
s = u'赵钱孙李周吴郑王'
s = list(s)
import random
print(random.choice(s))
print(random.choice(s))
print(random.choice(s))
print(random.choice(s))
print(random.choice(s))
print(random.choice(s))
吴 赵 王 孙 赵 赵
s = u'赵钱孙李周吴郑王'
s = list(s)
import random
random.shuffle(s)
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())
钱 孙 赵 吴 郑 李
完成grep -v ^# file | wc -l的功能,注意不能用shell调用。
用python代码完成上述功能。
注意: windows的同学无法做grep和wc,所以这道题目只能用python代码完成这部分功能。
!cat /etc/rc.local
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exit 0
!grep -v ^# /etc/rc.local | wc -l
2
# grep -v ^# file | wc -l
import subprocess
p1 = subprocess.Popen(["grep", "-v", "^#", "/etc/rc.local"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["wc", "-l"], stdin=p1.stdout, stdout=subprocess.PIPE)
output = p2.communicate()[0]
int(output.strip())
2
import re
import io
r = re.compile('^#')
# 在python2下无需考虑编码。
# python3下由于re需要处理string而非bytes,因此需要对输入做转码。
with io.open('/etc/rc.local', 'r', encoding='utf-8') as fi:
print(len([line for line in fi if not r.match(line)]))
2
使用python -m pdb file.py来启动你的某个程序。
然后试试看跟踪运行过程,看看你的解释是否正确。
如果希望在程序中快速定位到某个位置,可以考虑set_trace调用。
执行某个代码,并且设定一些assert。如果全部正确,那么测试通过。如果出现问题,那么该子项测试失败。
unittest的好处在于,对每个小的代码段,实现一个小的自动化测试脚本,并且这些脚本可以根据框架规则整合到一起。
当代码段增多,每个代码的修改变复杂的时候,依然可以保证这些小的代码片段正确实现了当初预订的逻辑。
只要单元测试是通过的,那么每个小片段代码的逻辑就是大致上正确的。这可以有力的保证最终代码是正确的。
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
unittest.TextTestRunner().run(suite)
... ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK
<unittest.runner.TextTestResult run=3 errors=0 failures=0>
针对“正则表达式”一节中的表达式,建立正向和反向用例,检验其正确性。
import re
import unittest, unittest.loader
class TestMobile(unittest.TestCase):
r = re.compile('1\d{10}')
def test_match(self):
self.assertTrue(self.r.match('13512345678'))
def test_notmatch(self):
self.assertFalse(self.r.match('021-10101010'))
class TestInetAddr(unittest.TestCase):
r = re.compile(r'\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}')
def test_ipv4(self):
self.assertTrue(self.r.match('192.168.0.1'))
def test_notmatch(self):
self.assertFalse(self.r.match('1200.5.4.3'))
self.assertFalse(self.r.match('abc.def.ghi.jkl'))
def test_wrong(self):
# 这里会报错,这是正常的
self.assertFalse(self.r.match('192.168.0.999'))
class TestUrl(unittest.TestCase):
# source: http://c.biancheng.net/cpp/html/1435.html
r = re.compile(r'(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*)?')
def test_url(self):
self.assertTrue(self.r.match('http://regxlib.com/Default.aspx'))
def test_domain(self):
self.assertFalse(self.r.match('regxlib.com'))
class TestEmail(unittest.TestCase):
# source: http://bbs.csdn.net/topics/60032440
r = re.compile(r'[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?')
def test_email(self):
self.assertTrue(self.r.match('shell999@gmail.com'))
def test_notmatch(self):
self.assertFalse(self.r.match('000@shell@1'))
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestMobile))
suite.addTest(loader.loadTestsFromTestCase(TestInetAddr))
suite.addTest(loader.loadTestsFromTestCase(TestUrl))
suite.addTest(loader.loadTestsFromTestCase(TestEmail))
unittest.TextTestRunner().run(suite)
....F....
======================================================================
FAIL: test_wrong (__main__.TestInetAddr)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-74-ea312ac19d96>", line 25, in test_wrong
self.assertFalse(self.r.match('192.168.0.999'))
AssertionError: <_sre.SRE_Match object at 0x7f9a080c5850> is not false
----------------------------------------------------------------------
Ran 9 tests in 0.003s
FAILED (failures=1)
<unittest.runner.TextTestResult run=9 errors=0 failures=1>
记得我们上面做过的练习“计算器”么?下面我们要做一个带有优先级和括号处理能力的复杂版本计算器。
请先思考一下,这类计算器如何做。然后试着实现一下想法。(假定目前只支持双目算符,但是优先级可能超过两个级别)
复杂计算器的核心问题在于算符优先级。
最简单来说,我们面对的问题核心即是a op1 b op2 c的表达式计算问题。
如果op1优先级高,我们需要计算a op1 b。如果op2高,原则上我们应当计算b op2 c。
但是实际上,由于表达式构成一个很长的链条,因此即便op2优先级比较高,也无法确定可以先算op2,有可能后续还有一个优先级更高的op3。
因此,我们需要构建一个list,并记录一个计算位置。
如果这个位置的优先级比前面的更高,那么继续向后移动。如果优先级比前面更低或相等,那么先计算前面的数据,并且将被计算部分从list中移走,插入计算结果。
在表达式结束的时候,通过持续的计算和缩减前面的数据,可以使前面的数据剩下且只剩下一项。这项即是我们需要的结果。
括号的问题更容易解决一些。
我们可以在看到(的时候,将剩余表达式作为一个新表达式传递给自身(递归)来求值,并且将)看做是一种表达式终结。
这样,()中的内容会被表达式计算函数吸收并运算成结果,替代在原始位置。经过有限次递归后,这个问题总是可以解决的。
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2016-09-02
@author: Shell.Xu
@copyright: 2016, Shell.Xu <shell909090@gmail.com>
@license: BSD-3-clause
'''
op_set = {'(': None,
')': None,
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: float(x)/y,}
op_priority = {'+': 10, '-': 10,
'*': 20,
'/': 20,}
def parser_exp(exp):
s = ''
for c in exp:
if c in op_set:
if s: yield s
s = ''
yield c
else: s += c
if s: yield s
def find_last_op(l):
for e in reversed(l):
if e in op_set: return e
def force_stack(stack):
r = stack.pop(-1)
op = stack.pop(-1)
l = stack.pop(-1)
result = op_set[op](l, r)
print('{} {} {} => {}'.format(op, l, r, result))
stack.append(result)
def eval_exp(exp):
stack = []
for e in exp:
if e == ')': break
elif e == '(':
stack.append(eval_exp(exp))
continue
if e not in op_set:
stack.append(float(e))
continue
while True:
last = find_last_op(stack)
if last is None or op_priority[last] < op_priority[e]: break
force_stack(stack)
stack.append(e)
while len(stack) > 1:
force_stack(stack)
return stack[0]
def calc(exp):
s = parser_exp(exp)
return eval_exp(s)
print(calc('2*3+1'))
print(calc('1+2*3'))
print(calc('(1.1+2.5/10)*3/4+1'))
print(eval('(1.1+2.5/10)*3/4+1'))
* 2.0 3.0 => 6.0 + 6.0 1.0 => 7.0 7.0 * 2.0 3.0 => 6.0 + 1.0 6.0 => 7.0 7.0 / 2.5 10.0 => 0.25 + 1.1 0.25 => 1.35 * 1.35 3.0 => 4.05 / 4.05 4.0 => 1.0125 + 1.0125 1.0 => 2.0125 2.0125 2.0125
%%writefile calc.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2016-09-02
@author: Shell.Xu
@copyright: 2016, Shell.Xu <shell909090@gmail.com>
@license: BSD-3-clause
'''
op_set = {'(': None,
')': None,
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: float(x)/y,}
op_priority = {'+': 10, '-': 10,
'*': 20,
'/': 20,}
def parser_exp(exp):
s = ''
for c in exp:
if c in op_set:
if s: yield s
s = ''
yield c
else: s += c
if s: yield s
def find_last_op(l):
for e in reversed(l):
if e in op_set: return e
def force_stack(stack):
r = stack.pop(-1)
op = stack.pop(-1)
l = stack.pop(-1)
result = op_set[op](l, r)
print('{} {} {} => {}'.format(op, l, r, result))
stack.append(result)
def eval_exp(exp):
stack = []
for e in exp:
if e == ')': break
elif e == '(':
stack.append(eval_exp(exp))
continue
if e not in op_set:
stack.append(float(e))
continue
while True:
last = find_last_op(stack)
if last is None or op_priority[last] < op_priority[e]: break
force_stack(stack)
stack.append(e)
while len(stack) > 1:
force_stack(stack)
return stack[0]
def calc(exp):
s = parser_exp(exp)
return eval_exp(s)
print(calc('2*3+1'))
print(calc('1+2*3'))
print(calc('(1.1+2.5/10)*3/4+1'))
Writing calc.py
!pylint calc.py | head -n 10
************* Module calc
C: 11, 0: Wrong continued indentation (add 6 spaces).
')': None,
^ | (bad-continuation)
C: 12, 0: Wrong continued indentation (add 6 spaces).
'+': lambda x, y: x+y,
^ | (bad-continuation)
C: 13, 0: Wrong continued indentation (add 6 spaces).
'-': lambda x, y: x-y,
^ | (bad-continuation)
!pylint calc.py | tail -n 5
Global evaluation ----------------- Your code has been rated at 4.00/10 (previous run: 4.00/10, +0.00)
!flake8 calc.py
calc.py:11:5: E128 continuation line under-indented for visual indent calc.py:12:5: E128 continuation line under-indented for visual indent calc.py:13:5: E128 continuation line under-indented for visual indent calc.py:14:5: E128 continuation line under-indented for visual indent calc.py:15:5: E128 continuation line under-indented for visual indent calc.py:15:33: E231 missing whitespace after ',' calc.py:18:5: E128 continuation line under-indented for visual indent calc.py:19:5: E128 continuation line under-indented for visual indent calc.py:19:12: E231 missing whitespace after ',' calc.py:21:1: E302 expected 2 blank lines, found 1 calc.py:25:17: E701 multiple statements on one line (colon) calc.py:28:13: E701 multiple statements on one line (colon) calc.py:29:9: E701 multiple statements on one line (colon) calc.py:31:1: E302 expected 2 blank lines, found 1 calc.py:33:23: E701 multiple statements on one line (colon) calc.py:35:1: E302 expected 2 blank lines, found 1 calc.py:43:1: E302 expected 2 blank lines, found 1 calc.py:46:20: E701 multiple statements on one line (colon) calc.py:55:66: E701 multiple statements on one line (colon) calc.py:62:1: E302 expected 2 blank lines, found 1 calc.py:68:34: W292 no newline at end of file
!python3 -m mccabe calc.py
21:1: 'parser_exp' 5 31:1: 'find_last_op' 3 43:1: 'eval_exp' 8 62:1: 'calc' 1 35:1: 'force_stack' 1
%%writefile calc.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2016-09-02
@author: Shell.Xu
@copyright: 2016, Shell.Xu <shell909090@gmail.com>
@license: BSD-3-clause
'''
from __future__ import absolute_import, division,\
print_function, unicode_literals
op_set = {
'(': None,
')': None,
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: float(x)/y,
}
op_priority = {
'+': 10,
'-': 10,
'*': 20,
'/': 20,
}
def parser_exp(exp):
s = ''
for c in exp:
if c in op_set:
if s:
yield s
s = ''
yield c
else:
s += c
if s:
yield s
def find_last_op(l):
for e in reversed(l):
if e in op_set:
return e
def force_stack(stack):
r = stack.pop(-1)
op = stack.pop(-1)
l = stack.pop(-1)
result = op_set[op](l, r)
print('{} {} {} => {}'.format(op, l, r, result))
stack.append(result)
def eval_exp(exp):
stack = []
for e in exp:
if e == ')':
break
elif e == '(':
stack.append(eval_exp(exp))
continue
if e not in op_set:
stack.append(float(e))
continue
while True:
last = find_last_op(stack)
if last is None or op_priority[last] < op_priority[e]:
break
force_stack(stack)
stack.append(e)
while len(stack) > 1:
force_stack(stack)
return stack[0]
def calc(exp):
s = parser_exp(exp)
result = eval_exp(s)
assert not list(s)
return result
print(calc('2*3+1'))
print(calc('1+2*3'))
print(calc('(1.1+2.5/10)*3/4+1'))
Overwriting calc.py
!pylint calc.py | head -n 10
************* Module calc C: 91, 0: Final newline missing (missing-final-newline) C: 13, 0: Invalid constant name "op_set" (invalid-name) C: 23, 0: Invalid constant name "op_priority" (invalid-name) C: 32, 4: Invalid variable name "s" (invalid-name) C: 33, 8: Invalid variable name "c" (invalid-name) C: 37,12: Invalid variable name "s" (invalid-name) C: 40,12: Invalid variable name "s" (invalid-name) C: 45, 0: Invalid argument name "l" (invalid-name) C: 46, 8: Invalid variable name "e" (invalid-name)
!pylint calc.py | tail -n 5
Global evaluation ----------------- Your code has been rated at 7.36/10 (previous run: 7.36/10, +0.00)
!flake8 calc.py
calc.py:91:34: W292 no newline at end of file
!flake8 --max-complexity 10 calc.py
calc.py:91:34: W292 no newline at end of file
coverage
!python-coverage run calc.py
* 2.0 3.0 => 6.0 + 6.0 1.0 => 7.0 7.0 * 2.0 3.0 => 6.0 + 1.0 6.0 => 7.0 7.0 / 2.5 10.0 => 0.25 + 1.1 0.25 => 1.35 * 1.35 3.0 => 4.05 / 4.05 4.0 => 1.0125 + 1.0125 1.0 => 2.0125 2.0125
!python-coverage report -m
Name Stmts Miss Cover Missing --------------------------------------- calc.py 54 0 100%
!rm -f calc.py
在开发python的时候,有一款好的编辑器往往能很大的提升工作效率。针对python来说,编辑器可能提供的特性有:
Emacs的授权是GPLv3。更多细节,可以参考这里
假定你已经选择了使用emacs作为日常编辑器,懂得如何设定配置文件和安装插件。知道如何切换到python-mode。
Emacs默认内置了python.el,自动开启了以下功能:
filetype plugin on
let g:pydiction_location = '~/.vim/tools/pydiction/complete-dict'
Eclipse的授权为独特的EPL。Eclipse安装完成后,加装pydev组件
PyCharm是一款商业的Python IDE,专业版以上为商业授权。同时有社区版,社区版授权为Apache License。
3基本下行兼容2,2不兼容3。所以2/3兼容的基本思想就是遵循3的编写规范,同时兼顾2。
在Python文件的开始,增加以下内容(注意按照pep-8,应该放在什么位置):
from __future__ import absolute_import, division, print_function, unicode_literals
这会改变几个关键性的兼容性标志。主要是print变为函数,string变为unicode。
虽然future技巧修正了内嵌字符串,但是对于str和unicode对象而言,并没有什么变化。因此建议在需要的头部如此定义:
if sys.version_info.major == 3:
unicode = str
else:
bytes = str
Python2中定义str为裸数据,unicode为字符串,bytes没有定义。Python3中定义bytes为裸数据,str为unicode字符串,unicode没有定义。在如此定义后,可以在2/3中,统一使用bytes表示裸数据,用unicode表示字符串。
同时,在代码中,应该对所有试图表现裸数据语义的代码内嵌字符串对象,加b前缀。对所有试图表达字符串语义的代码内嵌字符串对象,加u前缀。
print('hello, world')
print(u'hello, world')
hello, world hello, world
原则上,应对所有内嵌字符串对象都添加b或u前缀,以明确代码中的每一处内嵌字符串的语义。但是实际上这会大大增加工作量,而且工程实践上并没有特别大的优势。因此往往有所折衷,只在需要澄清的地方增加前缀。其他地方可以含混带过,等需要时再修改。
但是要注意到,项目越大,往往越难于根据后果追查出是哪个含混带过造成的问题。这里需要小心取舍。
Python3下文件打开时要注意是打开为二进制,还是打开为文本。即打开时的b标志位,决定了读写时应使用裸数据还是字符串。如果使用文本方式打开,还需注意编码。
Python2下没有encoding参数,因此出于兼容考虑,建议使用codecs.open。这个调用在Python2/3下的语义是一致的。
根据当前用户目录下的~/.backup_setting.ini文件,从中读取所有需要备份的目录列表。
找出所有文件,过滤~和.bak结尾的文件,过滤特定时间之前的,过滤特定正则表达式,然后使用tar.gz打包。
再将打包结果命名为”年月日/时分秒.tar.gz”,放在指定目录下。
上述内容需要用python完成,不得使用子进程调用其他程序。
有能力的同学可以附加功能: