Dive Into Python-Chapter 16. Functional Programming
Số trang: 36
Loại file: pdf
Dung lượng: 0.00 B
Lượt xem: 12
Lượt tải: 0
Xem trước 4 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu dive into python-chapter 16. functional 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ả
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 16. Functional Programming Chapter 16. Functional Programming16.1. Diving inIn Chapter 13, Unit Testing, you learned about the philosophy of unittesting. In Chapter 14, Test-First Programming, you stepped through theimplementation of basic unit tests in Python. In Chapter 15, Refactoring, yousaw how unit testing makes large-scale refactoring easier. This chapter willbuild on those sample programs, but here we will focus more on advancedPython-specific techniques, rather than on unit testing itself.The following is a complete Python program that acts as a cheap and simpleregression testing framework. It takes unit tests that youve written forindividual modules, collects them all into one big test suite, and runs themall at once. I actually use this script as part of the build process for this book;I have unit tests for several of the example programs (not just the roman.pymodule featured in Chapter 13, Unit Testing), and the first thing myautomated build script does is run this program to make sure all myexamples still work. If this regression test fails, the build immediately stops.I dont want to release non-working examples any more than you want todownload them and sit around scratching your head and yelling at yourmonitor and wondering why they dont work.Example 16.1. regression.pyIf you have not already done so, you can download this and other examplesused in this book.Regression testing frameworkThis module will search for scripts in the same directory namedXYZtest.py. Each such script should be a test suite that tests amodule through PyUnit. (As of Python 2.1, PyUnit is included inthe standard library as unittest.) This script will aggregate allfound test suites into one big test suite and run them all at once.import sys, os, re, unittestdef regressionTest(): path = os.path.abspath(os.path.dirname(sys.argv[0])) files = os.listdir(path) test = re.compile(test.py$, re.IGNORECASE) files = filter(test.search, files) filenameToModuleName = lambda f: os.path.splitext(f)[0] moduleNames = map(filenameToModuleName, files) modules = map(__import__, moduleNames) load = unittest.defaultTestLoader.loadTestsFromModule return unittest.TestSuite(map(load, modules))if __name__ == __main__: unittest.main(defaultTest=regressionTest)Running this script in the same directory as the rest of the example scriptsthat come with this book will find all the unit tests, named moduletest.py,run them as a single test, and pass or fail them all at once.Example 16.2. Sample output of regression.py[you@localhost py]$ python regression.py -vhelp should fail with no object ... ok 1help should return known result for apihelper ... okhelp should honor collapse argument ... okhelp should honor spacing argument ... okbuildConnectionString should fail with list input ... ok 2buildConnectionString should fail with string input ... okbuildConnectionString should fail with tuple input ... okbuildConnectionString handles empty dictionary ... okbuildConnectionString returns known result with known input ... okfromRoman should only accept uppercase input ... ok 3toRoman should always return uppercase ... okfromRoman should fail with blank string ... okfromRoman should fail with malformed antecedents ... okfromRoman should fail with repeated pairs of numerals ... okfromRoman should fail with too many repeated numerals ... okfromRoman should give known result with known input ... oktoRoman should give known result with known input ... okfromRoman(toRoman(n))==n for all n ... oktoRoman should fail with non-integer input ... oktoRoman should fail with negative input ... oktoRoman should fail with large input ... oktoRoman should fail with 0 input ... okkgp a ref test ... okkgp b ref test ... okkgp c ref test ... okkgp d ref test ... okkgp e ref test ... okkgp f ref test ... okkgp g ref test ... ok----------------------------------------------------------------------Ran 29 tests in 2.799sOK1 The first 5 tests are from apihelpertest.py, which tests the examplescript from Chapter 4, The Power Of Introspection.2 The next 5 tests are from odbchelpertest.py, which tests the examplescript from Chapter 2, Your First Python Program.3 The rest are from romantest.py, which you studied in depth in Chapter13, Unit Testing.16.2. Finding the pathWhen running Python scripts from the command line, it is sometimes usefulto know where the currently running script is located on disk.This is one of those obscure little tricks that is virtually impossible to figureout on your own, but simple to remember once you see it. The key to it issys.argv. As you saw in Chapter 9, XML Processing, this is a list that holdsthe list of command-line arguments. How ...
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 16. Functional Programming Chapter 16. Functional Programming16.1. Diving inIn Chapter 13, Unit Testing, you learned about the philosophy of unittesting. In Chapter 14, Test-First Programming, you stepped through theimplementation of basic unit tests in Python. In Chapter 15, Refactoring, yousaw how unit testing makes large-scale refactoring easier. This chapter willbuild on those sample programs, but here we will focus more on advancedPython-specific techniques, rather than on unit testing itself.The following is a complete Python program that acts as a cheap and simpleregression testing framework. It takes unit tests that youve written forindividual modules, collects them all into one big test suite, and runs themall at once. I actually use this script as part of the build process for this book;I have unit tests for several of the example programs (not just the roman.pymodule featured in Chapter 13, Unit Testing), and the first thing myautomated build script does is run this program to make sure all myexamples still work. If this regression test fails, the build immediately stops.I dont want to release non-working examples any more than you want todownload them and sit around scratching your head and yelling at yourmonitor and wondering why they dont work.Example 16.1. regression.pyIf you have not already done so, you can download this and other examplesused in this book.Regression testing frameworkThis module will search for scripts in the same directory namedXYZtest.py. Each such script should be a test suite that tests amodule through PyUnit. (As of Python 2.1, PyUnit is included inthe standard library as unittest.) This script will aggregate allfound test suites into one big test suite and run them all at once.import sys, os, re, unittestdef regressionTest(): path = os.path.abspath(os.path.dirname(sys.argv[0])) files = os.listdir(path) test = re.compile(test.py$, re.IGNORECASE) files = filter(test.search, files) filenameToModuleName = lambda f: os.path.splitext(f)[0] moduleNames = map(filenameToModuleName, files) modules = map(__import__, moduleNames) load = unittest.defaultTestLoader.loadTestsFromModule return unittest.TestSuite(map(load, modules))if __name__ == __main__: unittest.main(defaultTest=regressionTest)Running this script in the same directory as the rest of the example scriptsthat come with this book will find all the unit tests, named moduletest.py,run them as a single test, and pass or fail them all at once.Example 16.2. Sample output of regression.py[you@localhost py]$ python regression.py -vhelp should fail with no object ... ok 1help should return known result for apihelper ... okhelp should honor collapse argument ... okhelp should honor spacing argument ... okbuildConnectionString should fail with list input ... ok 2buildConnectionString should fail with string input ... okbuildConnectionString should fail with tuple input ... okbuildConnectionString handles empty dictionary ... okbuildConnectionString returns known result with known input ... okfromRoman should only accept uppercase input ... ok 3toRoman should always return uppercase ... okfromRoman should fail with blank string ... okfromRoman should fail with malformed antecedents ... okfromRoman should fail with repeated pairs of numerals ... okfromRoman should fail with too many repeated numerals ... okfromRoman should give known result with known input ... oktoRoman should give known result with known input ... okfromRoman(toRoman(n))==n for all n ... oktoRoman should fail with non-integer input ... oktoRoman should fail with negative input ... oktoRoman should fail with large input ... oktoRoman should fail with 0 input ... okkgp a ref test ... okkgp b ref test ... okkgp c ref test ... okkgp d ref test ... okkgp e ref test ... okkgp f ref test ... okkgp g ref test ... ok----------------------------------------------------------------------Ran 29 tests in 2.799sOK1 The first 5 tests are from apihelpertest.py, which tests the examplescript from Chapter 4, The Power Of Introspection.2 The next 5 tests are from odbchelpertest.py, which tests the examplescript from Chapter 2, Your First Python Program.3 The rest are from romantest.py, which you studied in depth in Chapter13, Unit Testing.16.2. Finding the pathWhen running Python scripts from the command line, it is sometimes usefulto know where the currently running script is located on disk.This is one of those obscure little tricks that is virtually impossible to figureout on your own, but simple to remember once you see it. The key to it issys.argv. As you saw in Chapter 9, XML Processing, this is a list that holdsthe list of command-line arguments. How ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin quản trị mạng tin học computer networkTài liệu liên quan:
-
52 trang 434 1 0
-
24 trang 359 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 312 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0