Size: 4900
Comment:
|
Size: 7631
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 54: | Line 54: |
'''Example 14.6. roman3.py''' * This file is available in py/roman/stage3/ in the examples directory. * 这个程序可以 <!> 从 ^在例子目录下的^ py/roman/stage3/ --(的 examples)-- 目录中 <!> 获得 ^找到^ 。 ---- '''程序注释1''' * This is a nice Pythonic shortcut: multiple comparisons at once. This is equivalent to if not ((0 < n) and (n < 4000)), but it's much easier to read. This is the range check, and it should catch inputs that are too large, negative, or zero. * <!> 这是 Python 的一个很棒的缩写 ^这个写法很Pythonic^: <!> 多重比较可以写在一起^一次进行多个比较^。 这等价于if not ((0 < n) and (n < 4000)), 但是 <!> 读起来简单了很多^更容易让人理解^。这是 <!> 一个^在进行^范围检查,可以将过大的数,负数和零查出来。 ---- '''程序注释2最后一句''' * if given, it is displayed in the traceback that is printed if the exception is never handled. * 如果给定,则在异常未被处理时显示于追踪 <!> ^信息(trackback)^之中。 ---- '''Example 14.7. Watching toRoman handle bad input''' * <!> 查看^观察^toRoman <!> ^如何^处理无效输入 ---- '''Example 14.8. Output of romantest3.py against roman3.py''' * <!> 以 ^用^romantest3.py 测试 roman3.py 的 <!> 输出 ^结果^ ---- '''输出结果说明1''' * toRoman still passes the known values test, which is comforting. All the tests that passed in stage 2 still pass, so the latest code hasn't broken anything. * toRoman 仍然能通过 已知值测试,这令人 <!> 很舒服 ^鼓舞^。 所有 <!> 第 2 步 ^在第2阶段^通过的测试 <!> 都^仍然能^通过--(了)--, <!> 也就是说^这说明^新的代码没有对原有代码构成任何负面影响。 ---- '''输出结果说明2第1句''' * More exciting is the fact that all of the bad input tests now pass. This test, testNonInteger, passes because of the int(n) <> n check. When a non-integer is passed to toRoman, the int(n) <> n check notices it and raises the NotIntegerError exception, which is what testNonInteger is looking for. * 更令人振奋的是所有的 无效输入测试 现在都通过了。 <!> 测试 testNonInteger ^testNonInteger 这个测试^ 能够通过是因为有了 int(n) <> n 检查。 ---- '''输出结果说明3''' * This test, testNegative, passes because of the not (0 < n < 4000) check, which raises an OutOfRangeError exception, which is what testNegative is looking for. * <!> 测试 testNegative^testNegative这个测试^ 能够通过 <!> 适应为^是因为^--(,当)-- not (0 < n < 4000) <!> ^这个^检查引发了 testNegative 期待的 OutOfRangeError 异常。 ---- |
2006-03-24 校对记要
校对文件: roman.xml
第14章第2节 roman.py, 第2阶段
第3行
- This file is available in py/roman/stage2/ in the examples directory.
这个程序可以
从 在例子目录下的 py/roman/stage2/ 的 examples 目录中
获得 找到 。
Example 14.3 的注释1第1句
- romanNumeralMap is a tuple of tuples which defines three things:
romanNumeralMap 是一个
用来定义三个内容的元组的元组 元组的元组,定义了三方面的内容:
Example 14.3 的注释1第1点
- 1.The character representations of the most basic Roman numerals. Note that this is not just the single-character Roman numerals; you're also defining two-character pairs like CM (“one hundred less than one thousand”); this will make the toRoman code simpler later.
1.
代表大部分罗马数字
的字符。 注意不只是
罗马数字的单字符 单字符的罗马数字,你同样在这里定义诸如 CM (“
比一千少一百 表示900”)的双字符,这可以
另让稍后编写的 toRoman 简单一些。
Example 14.3 的注释1第2点
- Here's where your rich data structure pays off, because you don't need any special logic to handle the subtraction rule.
这里便显示出你丰富的数据结构带来的优势,你不
必处理 需要什么特定的逻辑
处理减法规则。
Example 14.5.
- Example 14.5. Output of romantest2.py against roman2.py
以 用romantest2.py 测试 roman2.py 的
输出 结果
Example 14.5.输出的注释1
- toRoman does, in fact, always return uppercase, because romanNumeralMap defines the Roman numeral representations as uppercase. So this test passes already.
toRoman 通过,事实上所有返回值都是大写的 事实上toRoman 的返回值总是大写的,因为 romanNumeralMap 定义的罗马字符都是以大写字母表示的。 因此这个测试已经通过了。
Example 14.5.输出的注释2
- Here's the big news: this version of the toRoman function passes the known values test. Remember, it's not comprehensive, but it does put the function through its paces with a variety of good inputs, including inputs that produce every single-character Roman numeral, the largest possible input (3999), and the input that produces the longest possible Roman numeral (3888). At this point, you can be reasonably confident that the function works for any good input value you could throw at it.
这有个大新闻 好消息来了 :这个版本的 toRoman 函数能够通过 已知值测试。 记住,这并不能证明完全没问题,但至少
已经通过了大量的测试 通过测试多种有效输入考验了这个函数:包括
生成每个单一
罗马数字字符字符的罗马数字,
最大可能可能的最大输入(3999),以及
可能的最长的罗马数字表示(3888 对应的) 。从这点来看,你可以有理由
自信的人为 相信这个函数对于任何有效输入
都不会出问题。
put sth. through his paces. 考查某人[某物]的本领[性能], 考验某人[某物]是否合用. 固定用法。
Example 14.5.输出的注释3
- However, the function does not “work” for bad values; it fails every single bad input test. That makes sense, because you didn't include any checks for bad input. Those test cases look for specific exceptions to be raised (via assertRaises), and you're never raising them. You'll do that in the next stage.
但是,函数
对无效输入仍然不 “起作用”还没办法处理无效输入,每个 无效输入测试 都失败
了。 这
可以 很好理解 ,
因为你还没有对无效输入进行检查
,。 <!> 独立测试
测试用例
希望捕捉到特定的异常(通过 assertRaises),而你根本没有让这些异常引发。 这是你下一
步阶段的工作。
最后一句
- Here's the rest of the output of the unit test, listing the details of all the failures. You're down to 10.
这 下面是单元测试结果的剩余部分,列出了所有的失败
,的详细信息。你已经让它降到了10个
。
句尾少了一个句号
第14章第3节 roman.py, 第3阶段
Example 14.6. roman3.py
- This file is available in py/roman/stage3/ in the examples directory.
这个程序可以
从 在例子目录下的 py/roman/stage3/ 的 examples 目录中
获得 找到 。
程序注释1
This is a nice Pythonic shortcut: multiple comparisons at once. This is equivalent to if not ((0 < n) and (n < 4000)), but it's much easier to read. This is the range check, and it should catch inputs that are too large, negative, or zero.
这是 Python 的一个很棒的缩写 这个写法很Pythonic:
多重比较可以写在一起一次进行多个比较。 这等价于if not ((0 < n) and (n < 4000)), 但是
读起来简单了很多更容易让人理解。这是
一个在进行范围检查,可以将过大的数,负数和零查出来。
程序注释2最后一句
- if given, it is displayed in the traceback that is printed if the exception is never handled.
如果给定,则在异常未被处理时显示于追踪
信息(trackback)之中。
Example 14.7. Watching toRoman handle bad input
查看观察toRoman
如何处理无效输入
Example 14.8. Output of romantest3.py against roman3.py
以 用romantest3.py 测试 roman3.py 的
输出 结果
输出结果说明1
- toRoman still passes the known values test, which is comforting. All the tests that passed in stage 2 still pass, so the latest code hasn't broken anything.
toRoman 仍然能通过 已知值测试,这令人
很舒服 鼓舞。 所有
第 2 步 在第2阶段通过的测试
都仍然能通过了,
也就是说这说明新的代码没有对原有代码构成任何负面影响。
输出结果说明2第1句
More exciting is the fact that all of the bad input tests now pass. This test, testNonInteger, passes because of the int(n) <> n check. When a non-integer is passed to toRoman, the int(n) <> n check notices it and raises the NotIntegerError exception, which is what testNonInteger is looking for.
更令人振奋的是所有的 无效输入测试 现在都通过了。
测试 testNonInteger testNonInteger 这个测试 能够通过是因为有了 int(n) <> n 检查。
输出结果说明3
This test, testNegative, passes because of the not (0 < n < 4000) check, which raises an OutOfRangeError exception, which is what testNegative is looking for.
测试 testNegativetestNegative这个测试 能够通过
适应为是因为,当 not (0 < n < 4000)
这个检查引发了 testNegative 期待的 OutOfRangeError 异常。