本博客采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载本博客文章必须也遵循署名-非商业用途-保持一致的创作共用协议.
显示有限的接口到外部
当发布python第三方package时, 并不希望代码中所有的函数或者class可以被外部import, 在__init__.py
中添加__all__
属性,
该list中填写可以import的类或者函数名, 可以起到限制的import的作用, 防止外部import其他函数或者类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from base
import APIBase
from client
import Client
from decorator
import interface, export, stream
from server
import Server
from storage
import Storage
from util
import (LogFormatter, disable_logging_to_stderr,
enable_logging_to_kids, info)
__all__ = [
'APIBase',
'Client',
'LogFormatter',
'Server',
'Storage',
'disable_logging_to_stderr',
'enable_logging_to_kids',
'export',
'info',
'interface',
'stream']
|
with的魔力
with语句需要支持上下文管理协议的对象
, 上下文管理协议包含__enter__
和__exit__
两个方法. with语句建立运行时上下文需要通过这两个方法执行进入和退出
操作.
其中上下文表达式
是跟在with之后的表达式, 该表示大返回一个上下文管理对象
1
2
3
4
|
with
open(
"test.txt",
"r")
as my_file:
for
line
in my_file:
print
line
|
详细原理可以查看这篇文章, 浅谈 Python 的 with 语句
知道具体原理, 我们可以自定义支持上下文管理协议的类, 类中实现__enter__
和__exit__
方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
class MyWith(object):
def __init__(self):
print
"__init__ method"
def __enter__(self):
print
"__enter__ method"
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
print
"__exit__ method"
if exc_traceback
is
None:
print
"Exited without Exception"
return
True
else:
print
"Exited with Exception"
return
False
def test_with():
with MyWith()
as my_with:
print
"running my_with"
print
"------分割线-----"
with MyWith()
as my_with:
print
"running before Exception"
raise Exception
print
"running after Exception"
if __name__ ==
'__main__':
test_with()
|
执行结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
__init__
method
__enter__
method
running my_with
__exit__
method
Exited
without
Exception
------分割线-----
__init__
method
__enter__
method
running before
Exception
__exit__
method
Exited
with
Exception
Traceback (most recent call last):
File
"bin/python", line
34,
in <module>
exec(compile(__file__f.read(), __file__,
"exec"))
File
"test_with.py", line
33,
in <module>
test_with()
File
"test_with.py", line
28,
in test_with
raise
Exception
Exception
|
证明了会先执行__enter__
方法, 然后调用with内的逻辑, 最后执行__exit__
做退出处理, 并且, 即使出现异常也能正常退出
filter的用法
相对filter
而言, map和reduce使用的会更频繁一些, filter
正如其名字, 按照某种规则过滤
掉一些元素
1
2
3
4
5
6
7
8
9
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
lst = [
1,
2,
3,
4,
5,
6]
# 所有奇数都会返回True, 偶数会返回False被过滤掉
print filter(lambda x: x %
2 !=
0, lst)
#输出结果
[
1,
3,
5]
|
一行作判断
当条件满足时, 返回的为等号后面的变量, 否则返回else后语句
1
2
3
4
5
6
|
lst = [
1,
2,
3]
new_lst = lst[
0]
if lst
is
not
None
else
None
print new_lst
1
|
装饰器之单例
使用装饰器实现简单的单例模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def singleton(cls):
instances = dict()
def _singleton(*args, **kwargs):
if cls
not
in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return _singleton
@singleton
class Test(object):
pass
if __name__ ==
'__main__':
t1 = Test()
t2 = Test()
print t1, t2
|
staticmethod装饰器
类中两种常用的装饰, 首先区分一下他们
- 普通成员函数, 其中第一个隐式参数为
对象
-
classmethod装饰器
, 类方法(给人感觉非常类似于OC中的类方法), 其中第一个隐式参数为类
-
staticmethod装饰器
, 没有任何隐式参数. python中的静态方法类似与C++中的静态方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class A(object):
def foo(
self, x)
:
print
"executing foo(%s, %s)" % (
self, x)
@classmethod
def class_foo(cls, x)
:
print
"executing class_foo(%s, %s)" % (cls, x)
@staticmethod
def static_foo(x)
:
print
"executing static_foo(%s)" % x
def test_three_method()
:
obj =
A()
obj.foo(
"para")
obj.class_foo(
"para")
A.class_foo(
"para")
obj.static_foo(
"para")
A.static_foo(
"para")
if __name_
_ ==
'__main__'
:
test_three_method()
executing foo(<__main_
_.
A object at
0x100ba4e10>, para)
executing class_foo(<
class '__main__.A'>, para)
executing class_foo(<
class '__main__.A'>, para)
executing static_foo(para)
executing static_foo(para)
|
property装饰器
将property
与装饰器结合实现属性私有化(更简单安全的实现get和set方法
)
1
2
|
property(fget=
None, fset=
None, fdel=
None, doc=
None)
|
fget
是获取属性的值的函数,fset
是设置属性值的函数,fdel
是删除属性的函数,doc
是一个字符串(like a comment).从实现来看,这些参数都是可选的
property有三个方法getter()
, setter()
和delete()
来指定fget, fset和fdel。 这表示以下这行
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Student(object):
@property #相当于property.getter(score) 或者property(score)
def score(self):
return self._score
@score.setter #相当于score = property.setter(score)
def score(self, value):
if
not isinstance(value, int):
raise ValueError(
'score must be an integer!')
if value <
0
or value >
100:
raise ValueError(
'score must between 0 ~ 100!')
self._score = value
|
iter魔法
- 通过yield和
__iter__
的结合, 我们可以把一个对象变成可迭代的 - 通过
__str__
的重写, 可以直接通过想要的形式打印对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
class TestIter(object):
def __init__(self):
self.lst = [
1,
2,
3,
4,
5]
def read(self):
for ele
in xrange(len(self.lst)):
yield ele
def __iter__(self):
return self.read()
def __str__(self):
return
','.join(map(str, self.lst))
__repr__ = __str__
def test_iter():
obj = TestIter()
for num
in obj:
print num
print obj
if __name__ ==
'__main__':
test_iter()
|
神奇partial
partial使用上很像C++中仿函数(函数对象).
在stackoverflow给出了类似与partial的运行方式
1
2
3
4
5
6
7
|
def partial(func, *part_args):
def wrapper(*extra_args):
args = list(part_args)
args.extend(extra_args)
return func(*args)
return wrapper
|
利用用闭包的特性绑定预先绑定一些函数参数, 返回一个可调用的变量, 直到真正的调用执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from functools
import partial
def sum(a, b):
return a + b
def test_partial():
fun = partial(sum,
2)
print fun(
3)
if __name__ ==
'__main__':
test_partial()
5
|
神秘eval
eval我理解为一种内嵌的python解释器(这种解释可能会有偏差), 会解释字符串为对应的代码并执行, 并且将执行结果返回
看一下下面这个例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def test_first():
return
3
def test_second(num):
return num
action = {
"para":
5,
"test_first" : test_first,
"test_second": test_second
}
def test_eavl():
condition =
"para == 5 and test_second(test_first) > 5"
res = eval(condition, action)
print res
if __name__ ==
'_
|
exec
- exec在Python中会忽略返回值, 总是返回None, eval会返回执行代码或语句的返回值
-
exec
和eval
在执行代码时, 除了返回值其他行为都相同 - 在传入字符串时, 会使用
compile(source, '<string>', mode)
编译字节码. mode的取值为exec
和eval
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
def test_first():
print
"hello"
def test_second():
test_first()
print
"second"
def test_third():
print
"third"
action = {
"test_second": test_second,
"test_third": test_third
}
def test_exec():
exec
"test_second"
in action
if __name__ ==
'__main__':
test_exec()
|
getattr
getattr(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, ‘foobar’) is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
通过string类型的name, 返回对象的name属性(方法)对应的值, 如果属性不存在, 则返回默认值, 相当于object.name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
class TestGetAttr(object):
test =
"test attribute"
def say(self):
print
"test method"
def test_getattr():
my_test = TestGetAttr()
try:
print getattr(my_test,
"test")
except AttributeError:
print
"Attribute Error!"
try:
getattr(my_test,
"say")()
except AttributeError:
print
"Method Error!"
if __name__ ==
'__main__':
test_getattr()
test attribute
test method
|
命令行处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
def process_command_line(argv):
"""
Return a 2-tuple: (settings object, args list).
`argv` is a list of arguments, or `None` for ``sys.argv[1:]``.
"""
if argv
is
None:
argv = sys.argv[
1:]
parser = optparse.OptionParser(
formatter=optparse.TitledHelpFormatter(width=
78),
add_help_option=
None)
parser.add_option(
'-h',
'--help', action=
'help',
help=
'Show this help message and exit.')
settings, args = parser.parse_args(argv)
if args:
parser.error(
'program takes no command-line arguments; '
'"%s" ignored.' % (args,))
return settings, args
def main(argv=None):
settings, args = process_command_line(argv)
return
0
if __name__ ==
'__main__':
status = main()
sys.exit(status)
|
读写csv文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import csv
with open(
'data.csv',
'rb')
as f:
reader = csv.reader(f)
for row
in reader:
print row
import csv
with open(
'data.csv',
'wb')
as f:
writer = csv.writer(f)
writer.writerow([
'name',
'address',
'age'])
data = [
(
'xiaoming ',
'china',
'10'),
(
'Lily',
'USA',
'12')]
writer.writerows(data)
|
各种时间形式转换
只发一张网上的图, 然后差文档就好了, 这个是记不住的
![记不住](http://ww1.sinaimg.cn/large/ab508d3djw1exykn9rbe1j20kj0f977t.jpg)
字符串格式化
一个非常好用, 很多人又不知道的功能
1
2
3
|
>>> name = "andrew"
>>> "my name is {name}".format(name=name)
'my name is andrew'
|
参考链接