Fancy ML Fancy ML
  • RoadMap
  • 数理基础

    • 开始上手
  • Python

    • 开始上手
  • 教程

    • 开始上手
    • 斯坦福大学2014(吴恩达)机器学习教程
深度学习
  • Q&A
关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

让有意义的事变得有意思,让有意思的事变得有意义
  • RoadMap
  • 数理基础

    • 开始上手
  • Python

    • 开始上手
  • 教程

    • 开始上手
    • 斯坦福大学2014(吴恩达)机器学习教程
深度学习
  • Q&A
关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • RoadMap 机器学习路线
  • 数理基础

  • Python

    • 开始上手
    • IntermediatePython

      • 开始上手
      • Python 进阶 —— 第一节
      • Python 进阶 —— 第二节
      • Python 进阶 —— 第三节
      • Python 进阶 —— 第四节
        • 异常
        • lambda表达式
        • 一行式
        • For - Else
        • else从句
        • 使用C扩展
      • Python 进阶 —— 第五节
  • 基础
  • Python
  • IntermediatePython
jonsam
2022-04-13
目录

Python 进阶 —— 第四节

# 异常

异常处理是一种艺术,一旦你掌握,会授予你无穷的力量。我将要向你展示我们能处理异常的一些方式。

最基本的术语里我们知道了try/except从句。可能触发异常产生的代码会放到try语句块里,而处理异常的代码会在except语句块里实现。这是一个简单的例子:

try:
    file = open('test.txt', 'rb')
except IOError as e:
    print('An IOError occurred. {}'.format(e.args[-1]))
1
2
3
4

上面的例子里,我们仅仅在处理一个IOError的异常。大部分初学者还不知道的是,我们可以处理多个异常。

# 处理多个异常

我们可以使用三种方法来处理多个异常。

第一种方法需要把所有可能发生的异常放到一个元组里。像这样:

try:
    file = open('test.txt', 'rb')
except (IOError, EOFError) as e:
    print("An error occurred. {}".format(e.args[-1]))
1
2
3
4

另外一种方式是对每个单独的异常在单独的except语句块中处理。我们想要多少个except语句块都可以。这里是个例子:

try:
    file = open('test.txt', 'rb')
except EOFError as e:
    print("An EOF error occurred.")
    raise e
except IOError as e:
    print("An error occurred.")
    raise e
1
2
3
4
5
6
7
8

上面这个方式中,如果异常没有被第一个except语句块处理,那么它也许被下一个语句块处理,或者根本不会被处理。

现在,最后一种方式会捕获所有异常:

try:
    file = open('test.txt', 'rb')
except Exception:
    # 打印一些异常日志,如果你想要的话
    raise
1
2
3
4
5

当你不知道你的程序会抛出什么样的异常时,上面的方式可能非常有帮助。

# finally从句

我们把我们的主程序代码包裹进了try从句。然后我们把一些代码包裹进一个except从句,它会在try从句中的代码触发异常时执行。

在下面的例子中,我们还会使用第三个从句,那就是finally从句。包裹到finally从句中的代码不管异常是否触发都将会被执行。这可以被用来在脚本执行之后做清理工作。这里是个简单的例子:

try:
    file = open('test.txt', 'rb')
except IOError as e:
    print('An IOError occurred. {}'.format(e.args[-1]))
finally:
    print("This would be printed whether or not an exception occurred!")

# Output: An IOError occurred. No such file or directory
# This would be printed whether or not an exception occurred!
1
2
3
4
5
6
7
8
9

# try/else从句

我们常常想在没有触发异常的时候执行一些代码。这可以很轻松地通过一个else从句来达到。

有人也许问了:如果你只是想让一些代码在没有触发异常的情况下执行,为啥你不直接把代码放在try里面呢? 回答是,那样的话这段代码中的任意异常都还是会被try捕获,而你并不一定想要那样。

大多数人并不使用else从句,而且坦率地讲我自己也没有大范围使用。这里是个例子:

try:
    print('I am sure no exception is going to occur!')
except Exception:
    print('exception')
else:
    # 这里的代码只会在try语句里没有触发异常时运行,
    # 但是这里的异常将 *不会* 被捕获
    print('This would only run if no exception occurs. And an error here '
          'would NOT be caught.')
finally:
    print('This would be printed in every case.')

# Output: I am sure no exception is going to occur!
# This would only run if no exception occurs.
# This would be printed in every case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

else从句只会在没有异常的情况下执行,而且它会在finally语句之前执行。

# lambda表达式

lambda表达式是一行函数。 它们在其他语言中也被称为匿名函数。如果你不想在程序中对一个函数使用两次,你也许会想用lambda表达式,它们和普通的函数完全一样。

原型

    lambda 参数:操作(参数)
1

例子

    add = lambda x, y: x + y

    print(add(3, 5))
    # Output: 8
1
2
3
4

这还有一些lambda表达式的应用案例,可以在一些特殊情况下使用:

列表排序

    a = [(1, 2), (4, 1), (9, 10), (13, -3)]
    a.sort(key=lambda x: x[1])

    print(a)
    # Output: [(13, -3), (4, 1), (1, 2), (9, 10)]
1
2
3
4
5

列表并行排序

    data = zip(list1, list2)
    data.sort()
    list1, list2 = map(lambda t: list(t), zip(*data))
1
2
3

# 一行式

本章节,我将向大家展示一些一行式的Python命令,这些程序将对你非常有帮助。

简易Web Server

你是否想过通过网络快速共享文件?好消息,Python为你提供了这样的功能。进入到你要共享文件的目录下并在命令行中运行下面的代码:

    # Python 2
    python -m SimpleHTTPServer

    # Python 3
    python -m http.server
1
2
3
4
5

漂亮的打印

你可以在Python REPL漂亮的打印出列表和字典。这里是相关的代码:

    from pprint import pprint

    my_dict = {'name': 'Yasoob', 'age': 'undefined', 'personality': 'awesome'}
    pprint(my_dict)

1
2
3
4
5

这种方法在字典上更为有效。此外,如果你想快速漂亮的从文件打印出json数据,那么你可以这么做:

    cat file.json | python -m json.tool
1

脚本性能分析 这可能在定位你的脚本中的性能瓶颈时,会非常奏效:

    python -m cProfile my_script.py
1

备注:cProfile是一个比profile更快的实现,因为它是用c写的

CSV转换为json

在命令行执行这条指令

    python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"
1

确保更换csv_file.csv为你想要转换的csv文件

列表辗平

您可以通过使用itertools包中的itertools.chain.from_iterable轻松快速的辗平一个列表。下面是一个简单的例子:

    a_list = [[1, 2], [3, 4], [5, 6]]
    print(list(itertools.chain.from_iterable(a_list)))
    # Output: [1, 2, 3, 4, 5, 6]

    # or
    print(list(itertools.chain(*a_list)))
    # Output: [1, 2, 3, 4, 5, 6]
1
2
3
4
5
6
7

一行式的构造器

避免类初始化时大量重复的赋值语句

    class A(object):
        def __init__(self, a, b, c, d, e, f):
            self.__dict__.update({k: v for k, v in locals().items() if k != 'self'})
1
2
3

更多的一行方法请参考Python官方文档 (opens new window)。

# For - Else

循环是任何语言的一个必备要素。同样地,for循环就是Python的一个重要组成部分。然而还有一些东西是初学者并不知道的。我们将一个个讨论一下。

我们先从已经知道的开始。我们知道可以像这样使用for循环:

fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
    print(fruit.capitalize())

# Output: Apple
#         Banana
#         Mango
1
2
3
4
5
6
7

这是一个for循环非常基础的结构。现在我们继续看看,Python的for循环的一些鲜为人所知的特性。

# else从句

for循环还有一个else从句,我们大多数人并不熟悉。这个else从句会在循环正常结束时执行。这意味着,循环没有遇到任何break. 一旦你掌握了何时何地使用它,它真的会非常有用。我自己对它真是相见恨晚。

有个常见的构造是跑一个循环,并查找一个元素。如果这个元素被找到了,我们使用break来中断这个循环。有两个场景会让循环停下来。 - 第一个是当一个元素被找到,break被触发。 - 第二个场景是循环结束。

现在我们也许想知道其中哪一个,才是导致循环完成的原因。一个方法是先设置一个标记,然后在循环结束时打上标记。另一个是使用else从句。

这就是for/else循环的基本结构:

for item in container:
    if search_something(item):
        # Found it!
        process(item)
        break
else:
    # Didn't find anything..
    not_found_in_container()
1
2
3
4
5
6
7
8

考虑下这个简单的案例,它是我从官方文档里拿来的:

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(n, 'equals', x, '*', n/x)
            break
1
2
3
4
5

它会找出2到10之间的数字的因子。现在是趣味环节了。我们可以加上一个附加的else语句块,来抓住质数,并且告诉我们:

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print( n, 'equals', x, '*', n/x)
            break
    else:
        # loop fell through without finding a factor
        print(n, 'is a prime number')
1
2
3
4
5
6
7
8

# 使用C扩展

CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码

开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/C API。每种方式也都有各自的利弊。

首先,我们要明确为什么要在Python中调用C?

常见原因如下: - 你要提升代码的运行速度,而且你知道C要比Python快50倍以上 - C语言中有很多传统类库,而且有些正是你想要的,但你又不想用Python去重写它们 - 想对从内存到文件接口这样的底层资源进行访问 - 不需要理由,就是想这样做

# CTypes

Python中的ctypes模块 (opens new window)可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改。也正是如此奠定了这种方法的简单性。

示例如下

实现两数求和的C代码,保存为add.c

//sample C file to add 2 numbers - int and floats

#include <stdio.h>

int add_int(int, int);
float add_float(float, float);

int add_int(int num1, int num2){
    return num1 + num2;

}

float add_float(float num1, float num2){
    return num1 + num2;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

接下来将C文件编译为.so文件(windows下为DLL)。下面操作会生成adder.so文件

#For Linux
$  gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c

#For Mac
$ gcc -shared -Wl,-install_name,adder.so -o adder.so -fPIC add.c

1
2
3
4
5
6

现在在你的Python代码中来调用它

from ctypes import *

#load the shared object file
adder = CDLL('./adder.so')

#Find sum of integers
res_int = adder.add_int(4,5)
print "Sum of 4 and 5 = " + str(res_int)

#Find sum of floats
a = c_float(5.5)
b = c_float(4.1)

add_float = adder.add_float
add_float.restype = c_float
print "Sum of 5.5 and 4.1 = ", str(add_float(a, b))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

输出如下

Sum of 4 and 5 = 9
Sum of 5.5 and 4.1 =  9.60000038147
1
2

在这个例子中,C文件是自解释的,它包含两个函数,分别实现了整形求和和浮点型求和。

在Python文件中,一开始先导入ctypes模块,然后使用CDLL函数来加载我们创建的库文件。这样我们就可以通过变量adder来使用C类库中的函数了。当adder.add_int()被调用时,内部将发起一个对C函数add_int的调用。ctypes接口允许我们在调用C函数时使用原生Python中默认的字符串型和整型。

而对于其他类似布尔型和浮点型这样的类型,必须要使用正确的ctype类型才可以。如向adder.add_float()函数传参时, 我们要先将Python中的十进制值转化为c_float类型,然后才能传送给C函数。这种方法虽然简单,清晰,但是却很受限。例如,并不能在C中对对象进行操作。

# SWIG

SWIG是Simplified Wrapper and Interface Generator的缩写。是Python中调用C代码的另一种方法。在这个方法中,开发人员必须编写一个额外的接口文件来作为SWIG(终端工具)的入口。

Python开发者一般不会采用这种方法,因为大多数情况它会带来不必要的复杂。而当你有一个C/C++代码库需要被多种语言调用时,这将是个非常不错的选择。

示例如下(来自SWIG官网 (opens new window))

example.c文件中的C代码包含了不同的变量和函数

#include <time.h>
double My_variable = 3.0;

int fact(int n) {
    if (n <= 1) return 1;
    else return n*fact(n-1);

}

int my_mod(int x, int y) {
    return (x%y);

}

char *get_time()
{
    time_t ltime;
    time(&ltime);
    return ctime(&ltime);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

编译它

unix % swig -python example.i
unix % gcc -c example.c example_wrap.c \
    -I/usr/local/include/python2.1
unix % ld -shared example.o example_wrap.o -o _example.so

1
2
3
4
5

最后,Python的输出

>>> import example
>>> example.fact(5)
120
>>> example.my_mod(7,3)
1
>>> example.get_time()
'Sun Feb 11 23:01:07 1996'
>>>
1
2
3
4
5
6
7
8

我们可以看到,使用SWIG确实达到了同样的效果,虽然下了更多的工夫,但如果你的目标是多语言还是很值得的。

# Python/C API

Python/C API (opens new window)可能是被最广泛使用的方法。它不仅简单,而且可以在C代码中操作你的Python对象。

这种方法需要以特定的方式来编写C代码以供Python去调用它。所有的Python对象都被表示为一种叫做PyObject的结构体,并且Python.h头文件中提供了各种操作它的函数。例如,如果PyObject表示为PyListType(列表类型)时,那么我们便可以使用PyList_Size()函数来获取该结构的长度,类似Python中的len(list)函数。大部分对Python原生对象的基础函数和操作在Python.h头文件中都能找到。

示例

编写一个C扩展,添加所有元素到一个Python列表(所有元素都是数字)

来看一下我们要实现的效果,这里演示了用Python调用C扩展的代码

#Though it looks like an ordinary python import, the addList module is implemented in C import addList

l = [1,2,3,4,5] print "Sum of List - " + str(l) + " = " + str(addList.add(l))

上面的代码和普通的Python文件并没有什么分别,导入并使用了另一个叫做addList的Python模块。唯一差别就是这个模块并不是用Python编写的,而是C。

接下来我们看看如何用C编写addList模块,这可能看起来有点让人难以接受,但是一旦你了解了这之中的各种组成,你就可以一往无前了。

//Python.h has all the required function definitions to manipulate the Python objects
#include <Python.h>

//This is the function that is called from your python code
static PyObject* addList_add(PyObject* self, PyObject* args){

    PyObject * listObj;

    //The input arguments come as a tuple, we parse the args to get the various variables
    //In this case it's only one list variable, which will now be referenced by listObj
    if (! PyArg_ParseTuple( args, "O", &listObj ))
        return NULL;

    //length of the list
    long length = PyList_Size(listObj);

    //iterate over all the elements
    int i, sum =0;
    for (i = 0; i < length; i++) {
        //get an element out of the list - the element is also a python objects
        PyObject* temp = PyList_GetItem(listObj, i);
        //we know that object represents an integer - so convert it into C long
        long elem = PyInt_AsLong(temp);
        sum += elem;
    }

    //value returned back to python code - another python object
    //build value here converts the C long to a python integer
    return Py_BuildValue("i", sum);

}

//This is the docstring that corresponds to our 'add' function.
static char addList_docs[] =
"add(  ): add all elements of the list\n";

/* This table contains the relavent info mapping -
   <function-name in python module>, <actual-function>,
   <type-of-args the function expects>, <docstring associated with the function>
 */
static PyMethodDef addList_funcs[] = {
    {"add", (PyCFunction)addList_add, METH_VARARGS, addList_docs},
    {NULL, NULL, 0, NULL}

};

/*
   addList is the module name, and this is the initialization block of the module.
   <desired module name>, <the-info-table>, <module's-docstring>
 */
PyMODINIT_FUNC initaddList(void){
    Py_InitModule3("addList", addList_funcs,
            "Add all ze lists");

}
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

逐步解释 - Python.h头文件中包含了所有需要的类型(Python对象类型的表示)和函数定义(对Python对象的操作) - 接下来我们编写将要在Python调用的函数, 函数传统的命名方式由{模块名}_{函数名}组成,所以我们将其命名为addList_add

  • 然后填写想在模块内实现函数的相关信息表,每行一个函数,以空行作为结束 - 最后的模块初始化块签名为PyMODINIT_FUNC init{模块名}。

函数addList_add接受的参数类型为PyObject类型结构(同时也表示为元组类型,因为Python中万物皆为对象,所以我们先用PyObject来定义)。传入的参数则通过PyArg_ParseTuple()来解析。第一个参数是被解析的参数变量。第二个参数是一个字符串,告诉我们如何去解析元组中每一个元素。字符串的第n个字母正是代表着元组中第n个参数的类型。例如,"i"代表整形,"s"代表字符串类型, "O"则代表一个Python对象。接下来的参数都是你想要通过PyArg_ParseTuple()函数解析并保存的元素。这样参数的数量和模块中函数期待得到的参数数量就可以保持一致,并保证了位置的完整性。例如,我们想传入一个字符串,一个整数和一个Python列表,可以这样去写

int n;
char *s;
PyObject* list;
PyArg_ParseTuple(args, "siO", &n, &s, &list);
1
2
3
4

在这种情况下,我们只需要提取一个列表对象,并将它存储在listObj变量中。然后用列表对象中的PyList_Size()函数来获取它的长度。就像Python中调用len(list)。

现在我们通过循环列表,使用PyList_GetItem(list, index)函数来获取每个元素。这将返回一个PyObject*对象。既然Python对象也能表示PyIntType,我们只要使用PyInt_AsLong(PyObj *)函数便可获得我们所需要的值。我们对每个元素都这样处理,最后再得到它们的总和。

总和将被转化为一个Python对象并通过Py_BuildValue()返回给Python代码,这里的i表示我们要返回一个Python整形对象。

现在我们已经编写完C模块了。将下列代码保存为setup.py

#build the modules

from distutils.core import setup, Extension

setup(name='addList', version='1.0',  \
      ext_modules=[Extension('addList', ['adder.c'])])
1
2
3
4
5
6

并且运行

python setup.py install
1

现在应该已经将我们的C文件编译安装到我们的Python模块中了。

在一番辛苦后,让我们来验证下我们的模块是否有效

#module that talks to the C code
import addList

l = [1,2,3,4,5]
print "Sum of List - " + str(l) + " = " +  str(addList.add(l))
1
2
3
4
5

输出结果如下

Sum of List - [1, 2, 3, 4, 5] = 15
1

如你所见,我们已经使用Python.h API成功开发出了我们第一个Python C扩展。这种方法看似复杂,但你一旦习惯,它将变的非常有效。

Python调用C代码的另一种方式便是使用Cython (opens new window)让Python编译的更快。但是Cython和传统的Python比起来可以将它理解为另一种语言,所以我们就不在这里过多描述了。

编辑 (opens new window)
上次更新: 2022/04/13, 23:38:30
Python 进阶 —— 第三节
Python 进阶 —— 第五节

← Python 进阶 —— 第三节 Python 进阶 —— 第五节→

最近更新
01
Roadmap 路线
04-16
02
Plan 计划
04-16
03
开始上手
04-13
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy ML | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式