python模块编程_python编程实践_模块

1、一直在纠结编译器是如何找到所要导入的module的;类似于math这些已有的module可以在/Libs/site-package里导入,而自己定义的模块呢

而后发现在pydev下同一源文件下的模块可以直接导入- -

2、练习

1)

1 importmath2

3 a = math.fabs(-4.3) #4.3

4 b = math.sin(34.5) #0.0574874781049

5 c = math.floor(-4.3) #-5.0

6 d = math.ceil(-4.3) #-4.0

7 e = math.ceil(4.1) #5.0

8

9

10 help(math.floor)11 help(math.ceil)

几个挺有用的math函数。

顺便:假如想找math.ceil()的作用,在help(math)中找ceil的作用可以,直接help(math.ceil)也可以。[当然,在导入模块后才能使用help() - -]

1 Help on built-in function floor inmodule math:2

3 floor(...)4 floor(x)5

6 Return the floor of x as a float.7 This is the largest integral value <=x.8

9 Help on built-in function ceil inmodule math:10

11 ceil(...)12 ceil(x)13

14 Return the ceiling of x as a float.15 This is the smallest integral value >= x.

2)自己编写help

上述的help是库函数内的,假如自己要为自己写的模块写help呢?

这是我的文件树,可以看下/src下有两个.py,其中一个为自定义的模块,另一个为测试文件。

代码:

1 #test.py

2

3 importtest_module4 help(test_module)5

6

7 #test_module.py

8

9 defto_celsius(t):10 '''it is a function'''

11 return (t - 32.0) * 5.0 / 9.0

而后的显示:

1 Help on module test_module:2

3 NAME4 test_module5

6 FILE7 c:\users\dsdn\workspace_python\test\src\test_module.py8

9 FUNCTIONS10 to_celsius(t)11 it is a function

test_module.py中的三引号中的内容已经可以显示在help()中了

3)代码测试

使用nose模块来测试代码——python自带库unittest也可以,以后我试着写下。

源码【平时测试时最好不要只写一条assert(line 8),写多种特殊结果保证测试】

1 #test.py

2

3 importnose4 from test_module importto_celsius5 #import test_module

6

7 def test_to_celsius(): #def test_to_celsius(t):

8 assert to_celsius(32) == 34

9

10 if __name__ == '__main__':11 nose.runmodule()12

13 #test_module.py

14

15 defto_celsius(t):16 '''it is a function'''

17 return (t + 1)

为了方便些写测试,我把to_celsius()这个函数的return 改为简单的 t+1 了。

几点需要注意:

1、要使用line 4的导入而非line 5的导入,不然会报错

1 E2 ======================================================================

3 ERROR: __main__.test_to_celsius4 ----------------------------------------------------------------------

5 Traceback (most recent call last):6 File "C:\Python27\lib\site-packages\nose\case.py", line 197, inrunTest7 self.test(*self.arg)8 File "C:\Users\dsdn\workspace_python\test\src\test.py", line 10, intest_to_celsius9 assert to_celsius(32) == 34

10 NameError: global name 'to_celsius' is notdefined11

12 ----------------------------------------------------------------------

13 Ran 1 test in0.001s14

15 FAILED (errors=1)

line 10指出函数to_celsius 这个未定义。

使用line 4也可以,不过测试的时候得具体指出(line 8处)

assert test_module.to_celsius(32) == 34

2、line 7处,不要使用注释掉的函数,即测试函数不能接受参数。

3、其实这段代码执行起来会报错。

1 F2 ======================================================================

3 FAIL: __main__.test_to_celsius4 ----------------------------------------------------------------------

5 Traceback (most recent call last):6 File "C:\Python27\lib\site-packages\nose\case.py", line 197, inrunTest7 self.test(*self.arg)8 File "C:\Users\dsdn\workspace_python\test\src\test.py", line 10, intest_to_celsius9 assert test_module.to_celsius(32) == 34

10 AssertionError11

12 ----------------------------------------------------------------------

13 Ran 1 test in0.001s14

15 FAILED (failures=1)

我已经贴了两个报错的调试信息,不同的是,第一个为 NameError,提示为E;第二个报错为AssertionError,提示为F。

原因,就是我故意把“asserttest_module.to_celsius(32) ==33“写成”asserttest_module.to_celsius(32) == 34“了= =

测试代码执行后会得出下列三种结果之一:

1、Pass,即实际值与期望值相符;

2、Error,即测试本身出了问题,代码中有bug,就像import module之后调用方法得module.method;

3、Fail,即实际值与期望值不符。

4、关于AssertionError

assert test_module.to_celsius(32) == 34, ‘wrong’

我们可以通过这样显示报错信息,这里只是随手写了一个wrong,以后具体情况具体提示。

4)关于测试的练习

这本书有一点,有很多开放性题目,而我往往回答不上来- -

sorry for no conments :(

1 #test.py

2

3 importnose4 from test_module importdistance5

6 defclose(left, right):7 return abs(left - right) < 1.0e-6

8

9 deftest_distance():10 assert close(distance(1.0, 0.0, 1.0, 0.0), 0.0), 'Identical points fail.'

11 assert close(distance(0.0, 0.0, 1.0, 0.0), 1.0), 'Unit distance fail.'

12

13

14 if __name__ == '__main__':15 nose.runmodule()16

17 #test_module.py

18

19 importmath20

21 defdistance(x0, y0, x1, y1):22 return math.sqrt((x1 - x0) ** 2 + (y1 - y0) **2 )

简单来说,就是测两点间距离的。

Q1:test_module.py中,函数返回一浮点数,说明为什么这样会使测试变得更加困难。【私以为是浮点数比较麻烦,浮点数的比较,计算,存储什么的挺复杂】

Q2:test.py中,这样的测试有哪几个缺点【我觉得挺好,要说不足,测试太少,加个distance(0.0, 0.0, 0.0, 0.0)的测试如何?】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值