19

几个Python“小伎俩”(续)

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MjM5ODkzMzMwMQ%3D%3D&%3Bmid=2650412491&%3Bidx=4&%3Bsn=6fc9076143b4d460c606d31196930af9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

qIbuumE.jpg!web

Python Cookbook的下半部分笔记~

Chap 7 函数

将元数据信息附加到函数参数上

  • 函数的参数注解可以提示程序员该函数应该如何使用,这是很有帮助的。比如,考虑下面这个带参数注解的函数:

def add(x:int, y:int) -> int:
    return x + y
  • python解释器并不会附加任何语法意义到这些参数注解上。但参数注解会给阅读代码的人提供提示,并且一些第三方工具和框架可能会为注解加上语法含义。这些注解也会出现在文档中:

help(add)
Help on function add in module __main__:
    add(x:int, y:int) -> int
  • 函数注解只会保存在函数的__annotations__属性中,如下:

>>> add.__annotations__
{'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}
  • 函数注解也可以用来实现函数重载。

让带有N个参数的可调用对象以较少的参数形式调用

  • 函数functools.partial()允许我们给一个或多个参数指定固定的值,以此来减少参数的数量。

In [14]: def spam(a, b, c, d):
    ...:     print(a, b, c, d)
In [15]: from functools import partial
In [17]: s1 = partial(spam, 1)
In [18]: s1(2, 3, 4)
(1, 2, 3, 4)
In [19]: s1(4, 5, 6)
(1, 4, 5, 6)
In [20]: s2 = partial(spam, d=42)
In [21]: s2(4, 5, 5)
(4, 5, 5, 42)
In [24]: s3 = partial(spam, 1, 2, d=42)
In [25]: s3(5)
(1, 2, 5, 42)
  • 这个东西的主要用途是和那些只接受单一参数的函数来一起工作。如sort()函数:

In [26]: points = [ (1, 2), (3, 4), (5, 6), (7, 8) ]
In [27]: import math
In [28]: def distance(p1, p2):
    ...:     x1, y1 = p1
    ...:     x2, y2 = p2
    ...:     return math.hypot(x2 - x1, y2 - y1)
In [29]: pt = (4, 3)
In [30]: points.sort(key=partial(distance, pt))
In [31]: points
Out[31]: [(3, 4), (1, 2), (5, 6), (7, 8)]

Chap 8 类与对象

修改实例的字符串表示

  • 可以通过定义__str__()和__repr__()方法来实现;

  • 特殊方法 repr()返回的是实例的代码表示。通常可以用它发挥的字符串文版本重新创建这个实例,即满足 obj == eval(repr(obj))

class Pair:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return 'Pair ({0.x!r}, {0.y!r})'.format(self)

    def __str__(self):
        return '({0.x!s}, {0.y!s})'.format(self)

p = Pair(3, 4)
p
Pair(3, 4)
print(p)
(3, 4)
  • 通常认为定义 repr() 和 str()是好的编程实践,因为这么做可以简化调试过程和实例的输出。

自定义字符串的输出格式

  • 目的是想通过format()函数和字符串方法来支持自定义的输出格式。可以通过在类内定义 format ()方法来实现

_formats = {
    'ymd' : '{d.year} - {d.month} - {d.day}',
    'mdy' : ...,
    'dmy' : ...
}
class Date:
    def __init__(self, year, month, day):
        ...
    def __format__(self, code):
        if code == '':
            code = 'ymd'
        fmt = _formats[code]
        return fmt.format(d=self)

将名称封装到类中

  • Python不想C++中有private那种东西,但是又想将私有数据封装到类的实例上,于是通过特定的命名规则来表达出对数据和方法的用途

  • 第一种是任何以单下划线开头的名字应该总是被认为只属于内部实现

  • 第二种是一双下划线打头,这种做法会导致出现名称重整(name mangling)。如如在类B中实现的__private_method 则会被重命名为_B__private_method。目的就是在于双下划线开头的属性不能通过过继承而覆盖。

创建可管理的属性

  • 要自定义对属性的访问,一种简单的方式是将其定义为property

class Person:
    def __init__(self, first_name):
        self._first_name = first_name

    # Getter function
    @property
    def first_name(self):
        return self._first_name

    # Setter function
    @first_name.setter
    def first_name(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected a string')
        self._first_name = value

    # Deleter function
    @first_name.deleter
    def first_name(self):
        raise AttributeError("Can't delete attribute")
  • property也可以用来定义需要计算的属性。这类属性并不会实际保存起来,而是根需要计算完成。如:

import math
class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def area(self):
        return math.pi * self.radius ** 2

    @property
    def perimeter(self):
        return 2 * math.pi * self.radius

c = Cirle(4.0)
c.radius
4
c.area
50.2654824
c.perimeter
25.132741228

Chap 9 元编程

元编程的主要目标是创建函数和类,并用他们来操纵代码。

给函数添加一个包装

  • 如果想给函数加上一个包装层以添加额外的处理,可以定义一个装饰器函数:

import time
from fuctools import wraps

def timethis(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(func.__name__, end-start)
        return result
    return wrapper
  • 装饰器就是一个函数,他可以接受一个函数作为输入并返回一个新的函数作为输出。

@timethis
def countdown(n):
...

#运行起来和下面代码的效果是一样的

def countdown(n):
    ....
countdown = timethis(countdown)
  • 编写装饰器时的一个重要部分就是拷贝装饰器的元数据,记得使用functools库中的@wraps装饰器

对装饰器进行解包装

  • 某种情况下我们想要撤销一个函数的装饰器,访问未经过包装的那个原始函数,可以通过访问__wrapped__属性来实现

@somedecorator
def add(x, y):
    return x + y

>>> orig_add = add.__wrapped__
>>> orig_add(3, 4)
7

Chap 10 模块和包

把模块按层次结构组织成包

  • 只要把代码按照所希望的方式在文件系统上进行组织,并确保每个目录中都定义了一个__init__.py文件即可

重新加载模块

  • 由于对模块的源代码做了修改,我们想要重新加载一个已经加载过的模块,可以使用imp.reload()来实现。

import spam
import imp
imp.reload(spam)

读取包中的数据文件

  • 我们的代码需要读取包中的一个数据文件,我们要尽可能的以可移植的方式来处理。

  • pkgutil.get_data()函数是一种高级的工具,无论包以什么样的形式安装到了哪里,都能用它来获取数据文件。

import pkgutil
data = pkgutil.get_data(__package__,'somedata.dat')

Chap 12 并发

启动和停止进程

  • threading 库可用来在单独的线程中执行任意的python 可调用对象。要实现这一要求,可以创建一个 Thread 实例并为它提供期望执行的可调用对象。

# Code to execute in an independent thread
import time
def countdown(n):
    while n > 0:
        print('T-minus', n)
        n -= 1
        time.sleep(5)

# Creat and lanch a thread
from threading import Thread
t = Thread(target=countdown, args=(10,))
t.start()
  • 可以用 t.is_alive() 来判断它是否还在运行。还可以使用 t.join()请求连接到某个线程上,这么做会等待该线程结束。

  • 解释器会一直保持运行,直到所有的线程都被终结为止。对于需要长时间运行或者一直不断运行的后台任务,应该考虑将这些线程设置为daemon(守护线程)

t = Thread(target=function,)

-   END  -

推荐阅读

AINLP年度阅读收藏清单

BERT源码分析(PART I)

Transformers Assemble(PART I)

站在BERT肩膀上的NLP新秀们(PART I)

站在BERT肩膀上的NLP新秀们(PART II)

站在BERT肩膀上的NLP新秀们(PART III)

大幅减少GPU显存占用:可逆残差网络(The Reversible Residual Network)

鼠年春节,用 GPT-2 自动写对联和对对联

transformer-XL与XLNet笔记

AINLP-DBC GPU 云服务器租用平台建立,价格足够便宜

征稿启示 | 稿费+GPU算力+星球嘉宾一个都不少

关于AINLP

AINLP 是一个有趣有AI的自然语言处理社区,专注于 AI、NLP、机器学习、深度学习、推荐算法等相关技术的分享,主题包括文本摘要、智能问答、聊天机器人、机器翻译、自动生成、知识图谱、预训练模型、推荐系统、计算广告、招聘信息、求职经验分享等,欢迎关注!加技术交流群请添加AINLP君微信(id:AINLP2),备注工作/研究方向+加群目的。

qIR3Abr.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK