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 异常。
最后两段
- You're down to 6 failures, and all of them involve fromRoman: the known values test, the three separate bad input tests, the case check, and the sanity check. That means that toRoman has passed all the tests it can pass by itself. (It's involved in the sanity check, but that also requires that fromRoman be written, which it isn't yet.) Which means that you must stop coding toRoman now. No tweaking, no twiddling, no extra checks “just in case”. Stop. Now. Back away from the keyboard.
你已将失败降至6个,而且他们都是关于 fromRoman 的:已知值测试,三
种不同个独立的无效输入测试,大小写检查和
回旋完备性检查。这意味着 toRoman 通过了所有可以独立通过的测试(
回旋完备性测试也测试它,但需要 fromRoman 编写后一起测试)。 这就是说,你应该停止对 toRoman 的代码编写。不必再推敲,不必再做额外的检查 “恰到好处”。 停下来吧! 现在,别再敲键盘了。
- The most important thing that comprehensive unit testing can tell you is when to stop coding. When all the unit tests for a function pass, stop coding the function. When all the unit tests for an entire module pass, stop coding the module.
全面的单元测试能够告诉你
的最重要的事情是什么时候停止编写代码。当一个函数的所有单元测试都通过了,停止编写这个函数。一旦一个完整模块的单元测试通过了,停止编写这个模块。
第14章第4节 roman.py, 第4阶段
第1段第2句
- Thanks to the rich data structure that maps individual Roman numerals to integer values, this is no more difficult than the toRoman function.
感谢
那个将
逐每个罗马数字和对应整数关连的完美数据结构,这个工作不比 toRoman 函数复杂。
Example 14.9. roman4.py
- This file is available in py/roman/stage4/ in the examples directory.
这个程序可以
从 在例子目录下的 py/roman/stage4/ 的 examples 目录中
获得 找到 。
Example 14.11. Output of romantest4.py against roman4.py
以 用romantest4.py 测试 roman4.py 的
输出 结果
测试结果注释1
- Two pieces of exciting news here. The first is that fromRoman works for good input, at least for all the known values you test.
这儿有两个
令人激动的消息。 第一个是 fromRoman 对于所有有效输入运转正常,至少对于你测试的 已知值 是这样。
测试结果注释2
- The second is that the sanity check also passed. Combined with the known values tests, you can be reasonably sure that both toRoman and fromRoman work properly for all possible good values. (This is not guaranteed; it is theoretically possible that toRoman has a bug that produces the wrong Roman numeral for some particular set of inputs, and that fromRoman has a reciprocal bug that produces the same wrong integer values for exactly that set of Roman numerals that toRoman generated incorrectly. Depending on your application and your requirements, this possibility may bother you; if so, write more comprehensive test cases until it doesn't bother you.)
第二个好消息是,
回旋完备性测试也通过了。 与已知值测试的通过一起来看,你有理由相信 toRoman 和 fromRoman 对于所有有效输入值工作正常(
这并不是完全肯定的还不能完全相信,理论上
讲存在这种可能性, toRoman 存在错误而导致一些特定输入产生错误罗马数字表示
和恰巧 fromRoman 存在相应的错误,把特定) toRoman 错误产生的这些罗马数字错误地转换为最初的整数。 取决于你的应用程序和你的 <!>要求需求,你或许需要考虑这个可能性。如果是这样,编写更全面的
独立测试测试用例直到解决这个问题)。