Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'dive into python-chapter 14. test-first programming', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Chapter 14. Test-First Programming 14.1. roman.py stage 1 Now that the unit tests are complete it s time to start writing the code that the test cases are attempting to test. You re going to do this in stages so you can see all the unit tests fail then watch them pass one by one as you fill in the gaps in roman.py. Example 14.1. roman1.py This file is available in py roman stage1 in the examples directory. If you have not already done so you can download this and other examples used in this book. .Convert to and from Roman numerals. Define exceptions class RomanError Exception pass 1 class OutOfRangeError RomanError pass 2 class NotlntegerError RomanError pass class InvalidRomanNumeralError RomanError pass 3 def toRoman n .convert integer to Roman numeral. pass 4 def fromRoman s .convert Roman numeral to integer. pass 1 This is how you define your own custom exceptions in Python. Exceptions are classes and you create your own by subclassing existing exceptions. It is strongly recommended but not required that you subclass Exception which is the base class that all built-in exceptions inherit from. Here I am defining RomanError inherited from Exception to act as the base class for all my other custom exceptions to follow. This is a matter of style I could just as easily have inherited each individual exception from the Exception class directly. 2 The OutOfRangeError and NotIntegerError exceptions will eventually be used by toRoman to flag various forms of invalid input as specified in T oRomanBadInput. 3 The InvalidRomanNumeralError exception will eventually be used by fromRoman to flag invalid input as specified in FromRomanBadInput. 4 At this stage you want to define the API of each of your functions but you don t want to code them yet so you stub them out using the Python reserved word pass. Now for the big moment drum roll please you re finally going to run the unit test against this stubby little module. At this point every test case should fail. In fact if any