For Python source file learning, I personally think it is necessary to start with the concept of Python. In fact, Python is an object-oriented, literal translation computer programming language, it is also a powerful and complete general-purpose language and is very easy for beginners to learn.
First, we will introduce a build script that only compiles the Python sample code. <Project> the tag is always the root tag of the build script. <Taskdef> the tag declares the Python task used in the entire build script. At the bottom of the build script, you can define the compile target. The target element is a task executed during the compile operation. Specifically, the py-compile task is responsible for compiling all Python code starting from the src directory.
This task recursively traverses all subdirectories and compiles all Python source files. In the script, the src directory is not hardcoded to the calling place, but the property called src. dir is defined in the build script. Then, when you need to use this directory name, you can reference it through $ {src. dir.
To run the build script, open it from Eclipse. Eclipse has built-in Ant build script editing and browsing functions. The Outline View displays the structure of the build script:
- 1: <property name="pydoc.dir" value="pydoc"/>
- 2:
- 3: <target name="init">
- 4: <mkdir dir="${pydoc.dir}"/>
- 5: </target>
- 6:
- 7: <target name="pydoc" depends="init,compile">
- 8: <py-doc pythonpath="${src.dir}" destdir="${pydoc.dir}">
- 9: <fileset dir="${src.dir}">
- 10: <include name="**/*"/>
- 11: </fileset>
- 12: </py-doc>
- 13: </target>
In the Navigator view, right-click the build script and select "Run Ant ...". Select the compile target and click "Run ". The output information during script execution is displayed in the Console view, indicating that the script runs successfully.
Next, add a new target to the build script. For details about how to execute the Python script, see List 2 ). In this example, you can use the rss url as a parameter to execute the feedparser. py script. It can be seen from the analysis of the above pydoc target.
Row 7th declares the target name and points out that it depends on the init and compile targets. This means that before running the pydoc target, Ant must ensure that the init and compile targets are already running. If not, run the two targets first.
The init target on which the pydoc target depends is defined in rows 3rd to 5th. The init target only creates a directory for storing PyDoc API documentation files. As mentioned above, you need to define an attribute named pydoc. dir for the saved location of the generated document.
The first line is the py-doc task. As described above, you pass in the pythonpath used in the pydoc generation process. The destdir attribute tells the py-doc task where to output the generated HTML document. Lines 9th to 11th define which Python source files should be processed during document generation. A file set is a common structure in Ant scripts and can be used to define a group of files operated on.
This is a powerful feature that enables you to select the file to be operated through the name mode, Boolean logic, and file attributes. Ant has a complete description of this aspect. In this example, all files under the "src" directory are recursively selected.
- How to Understand Python Basics
- How to Create a Python language
- Advantages of Python
- In-depth analysis of the Python standard library
- Research on Python features
The Python source file has a standard unit test framework starting with Python 2.3. In Python 2.2, this is only an optional module), similar to the Java jUnit framework. The structure of the test case is the same as that of jUnit. Each class and module to be tested usually has its own test class. The test class contains the test device fixture), which is initialized in the setUp function.
Every test is written as an independent test function in the test class. The unittest framework repeats the test function cyclically. call setUp, test the function, and then clear tearDown) to test the function. See the example in Listing 4.