As a developer tool required by Python programmers

Source: Internet
Author: User
Tags rounds
Python has evolved into an extensive ecosystem that makes life easier for Python programmers and reduces repeated rounds. The same idea applies to the work of tool developers, even if the tools they developed do not appear in the final program. This article describes the developer tools required by Python programmers. Python has evolved into an extensive ecosystem that makes life easier for Python programmers and reduces repeated rounds. The same idea applies to the work of tool developers, even if the tools they developed do not appear in the final program. This article describes the developer tools required by Python programmers.

For developers, the most practical help is to help them write code documents.PydocThe module can generate a well-formatted document for any imported module based on docstrings in the source code. Python includes two testing frameworks to automatically test the code and verify the code correctness: 1)DoctestModule, which extracts test cases from examples of source code or independent files. 2)UnittestThis module is a fully functional automated testing framework that provides test fixtures, predefined test suite, and test discovery).

  TraceThe module can monitor how the Python program is executed and generate a report to display the number of executions of each row of the program. This information can be used to find the program execution path not covered by the automated test set, or to study the program call diagram, and then discover the dependencies between modules. Writing and executing tests can discover problems in most programs. Python makes debugging easier, because in most cases, Python can print unprocessed errors to the console, we call the error message traceback. If the program is not running in the text console, traceback can also output the error information to the log file or message dialog box. When the standard traceback cannot provide sufficient information, you can use the cgitb module to view detailed information in the stack and source code context at all levels, such as local variables.CgitbThe module can also output the tracking information in HTML format to report errors in web applications.

Once the problem is found, you need to use the interactive debugger to enter the code for debugging,PdbThe module is well qualified for this job. This module displays the execution path of the program when an error occurs, and dynamically adjusts the object and code for debugging. After the program passes testing and debugging, the next step is to focus on performance. Available to developersProfileAndTimitModule to test the program speed, find out where the program is very slow, and then tune this part of the code independently. Python programs are executed through the interpreter. the interpreter inputs the bytecode build of the original program. This bytecode compilation version can be dynamically generated during program execution or during Program packaging.CompileallThe module can handle program packaging. it exposes the packaging interface, which can be used by the installer and packaging tool to generate a file containing the module bytecode. In the development environment, the compileall module can also be used to verify whether the source file contains a syntax error.

At the source code level,PyclbrThe module provides a class viewer to facilitate text editors or other programs to scan interesting characters in Python programs, such as functions or classes. After the class viewer is provided, you do not need to introduce code, which avoids potential side effects.

Document string and doctest module

If the first line of a function, class, or module is a string, the string is a document string. It can be considered that containing document strings is a good programming habit, because these strings can provide some information to the Python program development tool. For example, The help () command can detect document strings, and Python-related IDE can also detect document strings. Since programmers tend to view document strings in interactive shell, it is better to write these strings briefly. For example

# mult.pyclass Test:    """    >>> a=Test(5)    >>> a.multiply_by_2()    10    """    def __init__(self, number):        self._number=number    def multiply_by_2(self):        return self._number*2

When writing a document, a common problem is how to synchronize the document with the actual code. For example, a programmer may modify the function implementation, but forgets to update the document. To solve this problem, we can use the doctest module. The doctest module collects document strings, scans them, and runs them as tests. To use the doctest module, we usually create an independent module for testing. For example, if the preceding example Test class is included in the file mult. py, you should create a new testmult. py file for testing, as shown below:

# testmult.pyimport mult, doctestdoctest.testmod(mult, verbose=True)# Trying:#     a=Test(5)# Expecting nothing# ok# Trying:#     a.multiply_by_2()# Expecting:#     10# ok# 3 items had no tests:#     mult#     mult.Test.__init__#     mult.Test.multiply_by_2# 1 items passed all tests:#    2 tests in mult.Test# 2 tests in 4 items.# 2 passed and 0 failed.# Test passed.

In this code, doctest. testmod (module) performs a test on a specific module and returns the number of test failures and the total number of tests. If all tests pass, no output is generated. Otherwise, you will see a failure report to show the difference between the expected value and the actual value. If you want to see the detailed output of the test, you can use testmod (module, verbose = True ).

If you do not want to create a new test file, another option is to include the corresponding test code at the end of the file:

if __name__ == '__main__':    import doctest    doctest.testmod()

To perform such a test, we can use the-m option to call the doctest module. Generally, there is no output during the test. If you want to view the details, you can add the-v option.

$ python -m doctest -v mult.py
Unit test and unittest module

To thoroughly test the program, we can use the unittest module. Through unit testing, developers can compile a series of independent test cases for every element of the program (such as independent functions, methods, classes, and modules. When testing larger programs, these tests can be used as the cornerstone to verify the correctness of the program. As our programs grow larger, unit tests on different components can be combined to form a larger testing framework and testing tool. This greatly simplifies software testing and facilitates the discovery and resolution of software problems.

# splitter.pyimport unittestdef split(line, types=None, delimiter=None):    """Splits a line of text and optionally performs type conversion.    ...    """    fields = line.split(delimiter)    if types:        fields = [ ty(val) for ty,val in zip(types,fields) ]    return fieldsclass TestSplitFunction(unittest.TestCase):    def setUp(self):        # Perform set up actions (if any)        pass    def tearDown(self):        # Perform clean-up actions (if any)        pass    def testsimplestring(self):        r = split('GOOG 100 490.50')        self.assertEqual(r,['GOOG','100','490.50'])    def testtypeconvert(self):        r = split('GOOG 100 490.50',[str, int, float])        self.assertEqual(r,['GOOG', 100, 490.5])    def testdelimiter(self):        r = split('GOOG,100,490.50',delimiter=',')        self.assertEqual(r,['GOOG','100','490.50'])# Run the unittestsif __name__ == '__main__':    unittest.main()#...#----------------------------------------------------------------------#Ran 3 tests in 0.001s#OK

When using unit tests, we need to define a class that inherits from unittest. TestCase. In this class, each test is defined in the form of a method and named with the test header-for example, 'testsimplestring ', 'testtypeconvert' and similar naming methods (it is important to stress that, as long as the method name starts with "test", no matter how it is named ). In each test, assertions can be used to check different conditions.

Example:

If you have a method in the program, the output of this method points to the standard output (sys. stdout ). This usually means to output text information to the screen. If you want to test your code to prove this, you only need to provide the corresponding input, and the corresponding output will be displayed.

# url.pydef urlprint(protocol, host, domain):    url = '{}://{}.{}'.format(protocol, host, domain)    print(url)

The built-in print function sends output to sys. stdout by default. To test that the output has actually arrived, you can use a substitute object to simulate it and assert the expected value of the program. The patch () method in the unittest. mock module can replace the object only in the context of the running test, and immediately return the original state of the object after the test is completed. The following is the test code for the urlprint () method:

#urltest.pyfrom io import StringIOfrom unittest import TestCasefrom unittest.mock import patchimport urlclass TestURLPrint(TestCase):    def test_url_gets_to_stdout(self):        protocol = 'http'        host = 'www'        domain = 'example.com'        expected_url = '{}://{}.{}\n'.format(protocol, host, domain)        with patch('sys.stdout', new=StringIO()) as fake_out:            url.urlprint(protocol, host, domain)            self.assertEqual(fake_out.getvalue(), expected_url)

The urlprint () function has three parameters. the test code first assigns a dummy value to each parameter. The variable expected_url contains the expected output string. To be able to perform the test, we use unittest. mock. the patch () method is used as the context manager to output the standard sys. stdout is replaced with the StringIO object, so that the content of the standard output will be received by the StringIO object. The variable fake_out is the simulated object created in this process. this object can be used in the with code block for a series of test checks. When the with statement is complete, the patch method restores everything to the state before the test is executed, as if the test was not executed, without any additional work. But for some Python C extensions, this example is meaningless, because these C extensions bypass the sys. stdout settings and directly send the output to the standard output. This example is only applicable to programs with pure Python code (if you want to capture input and output similar to C extension, you can achieve this by opening a temporary file and redirecting the standard output to the file ).

The above is the details of the developer tools that must be used by Python programmers. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.