Python-based code automatic generation and automatic inspection method
Through the automatic code generation and automatic inspection method based on Python, the high cost and error rate problems of code writing and inspection in software development are solved, automatic code generation and inspection are realized, suitable for multi-partition and branch code management, and development efficiency is improved. and accuracy.
Patent Information
- Application Number
- CN202411225971.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-03
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2044-09-03
Smart Images

Figure CN118733007A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a Python-based automatic code generation and automatic checking method. Background Art
[0002] In some scenarios, many repetitive and complex codes require developers to write and manage them according to the data and indicators of different models or devices. If developers review, write and check them by themselves, it will cost a lot of manpower. In addition, manually written codes will inevitably have errors, and the subsequent manually written documents will also cost a lot of manpower. Therefore, in the actual development process, developers urgently need a solution that can automatically generate code and support automatic code checking, and provide a necessary support environment tool for software development and testing. Summary of the invention
[0003] The purpose of the present invention is to develop a tool that can automatically generate code and support automatic code checking, and to provide a Python-based code automatic generation and automatic checking method.
[0004] In order to achieve the above-mentioned object of the invention, the present invention provides the following technical solutions: The Python-based code automatic generation and automatic checking method includes the following steps: Step 1, read configuration and common program class files; Step 2, extract the configuration file, develop a source code parsing program, and analyze and process the source code of each function provided by the user; Step 3, develop a Python-based code automatic generation program to generate corresponding source code files according to the extracted source code and configuration; Step 4: Develop a code constraint automated checking tool library to perform architecture compliance checks and generate check result files.
[0005] In step 1, the configuration and common program class files include yaml configuration files, re library, AST module, and csv library.
[0006] The step 2 comprises the following specific steps: Step 2-1, use the AST module to parse the source code into an abstract syntax tree; Step 2-2, traverse the abstract syntax tree to find the definitions of global and local variables, as well as the code that needs to be modified; Step 2-3, modify and rewrite the source code through regular expressions or other text processing methods according to the configured rules; Step 2-4, converting the abstract syntax tree after the modified code back to the source code form; Step 2-5, save the modified source code to the corresponding code path.
[0007] The step 2 specifically includes the following steps: Step 2-1, use Python's open method to read the source code file and process the file content line by line; Step 2-2: For each line of source code, use a regular expression to match the part that needs to be modified; Step 2-3, rewrite the part that needs to be modified according to the matching results and the rules configured in the re-library; Step 2-4, write the modified code into a new file, or overwrite the original source code file; Step 2-5, save the new file or modified source code file to the corresponding code path.
[0008] In step 3, the source code file is generated by one or a combination of the following solutions: (1) Static code analysis: Use the code parsing library in Python to analyze C / C++ source code and obtain the definition of the code; traverse the abstract syntax tree of the code, identify the definition of the code, and generate the corresponding declaration of the code; write a custom abstract syntax tree traverser to identify the definition of the code according to the node type of the abstract syntax tree traverser; (2) Source code scanning tool: Use Python to write a source code scanning tool to scan the code definitions in C / C++ source code; use Python's regular expressions and string processing functions to locate and extract code information; generate the corresponding declaration header file of the code based on the scanning results or insert declaration statements inside the declaration function; (3) Template generation: Define the template for code declaration, use Python's template engine to generate the corresponding declaration header file according to the template; reserve the code location in the template and fill in the name and type of the code; write Python scripts to automatically generate header files and integrate them into the platform construction process; (4) Code instrumentation: Use Python's code parsing library to analyze C / C++ source code, identify the definition and content of the declaration function; automatically insert the code declaration statement inside the declaration function according to the needs; write a custom stub to determine the insertion position according to the grammatical structure and rules of the declaration function, and generate the corresponding declaration statement of the code; (5) Regular expression matching: Use regular expressions in Python to match code definitions in C / C++ source code; write appropriate regular expression patterns to identify code declarations and extract relevant information; generate corresponding declaration statements for the code based on the matching results and write them into the declaration header file.
[0009] The step 4 specifically comprises the following steps: Step 4-1, check the architecture compliance of the automatically generated source code file; Step 4-2, call the architecture analysis tool Generator to check the code for variable initialization, assignment, and call, and output the result file; Step 4-3, use Python script to parse the source code file to complete the code inspection and generate the inspection result file.
[0010] The step 4-1 specifically includes the following steps: Step 4-1-1, parse the C / C++ source code through syntax and semantic analysis to obtain an abstract syntax tree; Step 4-1-2, traverse the abstract syntax tree to find nodes related to component state variables and output variables, which involves checking the use of variable declaration and initialization; Step 4-1-3, identify the state variables and output variables through annotations, specific naming conventions, or specific types; Step 4-1-4, check whether the state variables and output variables are correctly initialized on all execution paths in combination with control flow analysis; Step 4-1-5, generate a check report, list the variables that are not initialized correctly and the reasons, or provide repair suggestions, or automatically repair.
[0011] Also included: Step 5, providing a batch script to facilitate users to install and start the tool with one click.
[0012] Compared with the prior art, the beneficial effects of the present invention are as follows: the present invention supports automatic code generation, supports automatic code constraint checking, and provides a necessary support environment tool for software development and testing, including the ability to realize source code analysis function, automatic code generation, automatic document generation, design constraint automatic checking function, etc., reducing the workload of developers and avoiding errors caused by human development. At the same time, this tool can be configured accordingly with different partition codes of customers, can be directly installed in customer projects, perform git tracking, and can perform version management and release together. The tool provides an init one-key initialization script, and directly uses a bat script to start after initialization. In an actual production environment, the customer's code will have multiple different partitions such as A, B, C, D, etc., and may also have multiple different branches. At this time, only the yml and config configuration files of the corresponding partitions need to be configured to switch the partition or branch running tool at any time. After the initial configuration, the corresponding configuration file can be submitted to git together with the partition or branch code. A mode of maintenance together with the customer's partition or branch code will be achieved later, and the code of the partition required by the user can be generated and checked at any time. And the non-compilable C code generates a compilable C code that has been automatically checked by multiple constraints through the platform. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0014] Figure 1 is a flow chart of the method of the present invention; Figure 2 is a schematic diagram of the Generator tool of the present invention; Figure 3 A flowchart of a possible implementation method of parsing source code of the present invention; Figure 4 A flowchart of another possible implementation method of parsing source code of the present invention; Figure 5 A schematic diagram of the process flow of automatic checking of code constraints of the present invention; Figure 6 A schematic diagram of the flow chart of the architecture compliance check of the present invention. DETAILED DESCRIPTION
[0015] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. The components of the embodiments of the present invention generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the claimed invention, but merely represents the selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present invention.
[0016] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0017] The present invention is achieved through the following technical solutions: Figure 1 As shown, the Python-based automatic code generation and automatic checking method includes the following steps: Step 1: Read configuration and common program class files.
[0018] The tool developed in this solution implements source code parsing, automatic code generation, and automatic code constraint checking based on Python language. The tool provides support for configuration and common program class files, such as yaml configuration files, re libraries, abstract syntax trees, csv libraries, etc. The tool uses a configurable generator to perform source code parsing, automatic code generation, and automatic code constraint checking. Before executing the function, the configuration and common program class files need to be read.
[0019] The yaml configuration file contains the configuration rules for generating code.
[0020] The re library is a regular expression library based on Python language, which allows users to perform funny text matching and processing in Python. Regular expression is a text pattern matching tool that can be used to search, replace and parse text data. In Python, the re library provides a wealth of functions, making string processing more flexible and convenient. The re library can be used to perform operations such as finding specific patterns, replacing text, and extracting information. By defining specific patterns to match the content in the text, various text processing tasks can be achieved. The functions in the re library include search, match, findall, etc. These functions can help the tool find matching patterns in the text and perform corresponding operations.
[0021] Abstract Syntax Tree (AST) is a data structure commonly used in programming languages to represent the syntax structure of a program. In Python, the AST module provides the function of parsing and analyzing Python code, which can parse Python code into an abstract syntax tree, making it easier to perform code analysis, optimization, and conversion.
[0022] The csv library is a module in the Python standard library for processing CSV files. CSV files are a common text file format, usually used to store tabular data. Each line represents a record, and each field is separated by a comma or other delimiter. Using the csv library, tools can easily read, write, and manipulate CSV files. The csv library provides a variety of functions and methods to make processing CSV files simpler and more efficient. For example, tools can use the csv.reader() or csv.dictreader() functions to read rows of data from a CSV file, or use the csv.writer() function to write data to a CSV file. In addition to basic reading and writing functions, the csv library also supports processing CSV files containing special cases such as different delimiters and quote characters. Different parameters can be set to adapt to different CSV file formats, making processing various types of CSV files more flexible.
[0023] In general, the configuration and common program class files read enable the tool to control the execution effect through specific defined configurations. Before reading the configuration and common program class files, you need to clean up the last generated code.
[0024] When the Generator executes a function, each function has a corresponding switch. When the switch corresponding to the function is turned on, the Generator executes the function. Figure 2 As shown, when the source code parsing switch is turned on, the Generator performs source code parsing. In order to realize various functions, each function has corresponding configurations, which are called from the read configuration and public program class files.
[0025] Step 2, extract the configuration file, develop a source code parsing handler, and analyze and process the source code of each function provided by the user.
[0026] The tool performs source code parsing and modifies the code according to certain configured rules. The tool can use the AST module to parse the abstract syntax tree of the source code, and use regular expressions or other text processing methods to rewrite and replace the code.
[0027] As a possible implementation method of this step, Figure 3As shown, source code parsing is achieved through the following steps: Step 2-1, use the AST module to parse the source code into an abstract syntax tree; Step 2-2, traverse the abstract syntax tree to find the definitions of global and local variables, as well as the code that needs to be modified; Step 2-3, according to the configured rules, modify and rewrite the source code through regular expressions or other text processing methods, which may involve operations such as adding, deleting, and replacing code; Step 2-4, converting the abstract syntax tree after the modified code back to the source code form; Step 2-5, save the modified source code to the corresponding code path.
[0028] In this implementation, the abstract syntax tree of the source code is parsed first, and then source code parsing and code modification are implemented according to the required configuration.
[0029] As another possible implementation method of this step, Figure 4 As shown, source code parsing is achieved through the following steps: Step 2-1, use Python's open method to read the source code file and process the file content line by line; Step 2-2: For each line of source code, use a regular expression to match the part that needs to be modified, for example, matching the definition of global variables and local variables, or other patterns that need to be modified; Step 2-3, rewrite the parts that need to be modified according to the matching results and the rules configured in the re-library, which may involve operations such as adding, deleting, and replacing code; Step 2-4, write the modified code into a new file, or overwrite the original source code file; Step 2-5, save the new file or modified source code file to the corresponding code path.
[0030] In this embodiment, the actual operation is to use Python's re library combined with file operations to implement the parsing and rewriting of the source code. This solution mainly relies on regular expressions to match and process the content in the source code, and modify it according to the specific rules of the configuration. Compared with the previous embodiment, this embodiment is more convenient because the source code file can be processed directly without building an abstract syntax tree. However, it should be noted that all reliance on regular expression processing may face some complex situations, especially when the grammatical structure and format of the source code vary greatly. Therefore, when implementing this embodiment, a constant management module (constant.py) is used to manage all complex regular expression patterns, and the regular expression patterns are concentrated in one place for easy management and maintenance, and can be easily called when needed.
[0031] The extended description of the constant management module is: The constant management module contains all the regular expression patterns that need to be used, and each pattern is represented by a variable with a descriptive name. These patterns include matching global variables, local variables, function definitions, specific patterns, etc. The constant management module provides clear comments and documentation so that developers can understand the role and usage of each pattern.
[0032] The principle of the constant management module is: in the process of source code parsing and rewriting, the regular expression pattern defined in the constant management module is used by importing it. When a specific grammatical structure needs to be matched or processed, the corresponding pattern variable can be directly used without repeatedly writing complex regular expressions. This helps to improve the readability and maintainability of the code, while reducing the possibility of errors. If a regular expression pattern needs to be updated or modified, only the regular expression needs to be modified in the constant management module, and the corresponding places in the source code file can be uniformly modified without having to find and modify all related places in the entire source code file.
[0033] By using constant management modules, you can make your code more modular and maintainable, reduce the possibility of parsing and rewriting errors, and make the code easier to understand and modify.
[0034] Step 3: Develop a Python-based automatic code generation program to generate corresponding source code files based on the extracted source code and configuration.
[0035] When automatically generating code that declares functions or variables, you can use one of the following solutions or a combination of multiple solutions to automatically generate code based on the actual application scenario or actual needs of the code. The following uses the generation of global variable declaration files and the automatic insertion of global variable declarations as examples to explain the solution for automatically generating code based on Python.
[0036] (1) Static code analysis: Use the code parsing library in Python (such as the AST module) to analyze C / C++ source code and obtain the definition of global variables. Traverse the abstract syntax tree of the code, identify the definition of global variables, and generate the corresponding declaration of global variables. Write a custom abstract syntax tree traverser to identify the definition of global variables based on the node type of the abstract syntax tree traverser.
[0037] (2) Source code scanning tool: Use Python to write a source code scanning tool to scan the global variable definitions in C / C++ source code. Use Python's regular expression and string processing functions to quickly locate and extract global variable information. Generate the corresponding declaration header file for the global variable based on the scanning results or insert the declaration statement inside the declaration function.
[0038] (3) Template generation: Define the template for global variable declaration, and use Python's template engine (such as Jinja2) to generate the corresponding declaration header file of the global variable according to the template. Reserve the position of the global variable in the template, and fill in the name, type and other information of the global variable according to the actual situation. Write a Python script to automatically generate the header file and integrate it into the platform construction process.
[0039] (4) Code instrumentation: Use Python's code parsing library to analyze C / C++ source code, identify the definition and content of the declaration function, and automatically insert the declaration statement of global variables inside the declaration function as required. Write a custom stub to determine the insertion position based on the grammatical structure and rules of the declaration function, and generate the corresponding declaration statement of the global variable.
[0040] (5) Regular expression matching: Use regular expressions in Python (such as the re library) to match global variable definitions in C / C++ source code. Write appropriate regular expression patterns to identify global variable declarations and extract relevant information. Generate corresponding declaration statements for global variables based on the matching results and write them into the declaration header file.
[0041] Step 4: Develop a code constraint automated checking tool library to perform architecture compliance checks and generate check result files.
[0042] The inspection rules are implemented in the tool by scanning and parsing the automatically generated source code files. The scanning of this part needs to exclude the developer's unique tag code (which can support part of the screen code), and the code inspection is completed by parsing the file through the Python script. And some codes call the architecture analysis tool Generator to complete the variable initialization, assignment, call and other checks of the code. The architecture analysis tool outputs the result file after analysis, and the code inspection is completed by parsing the source code file through the Python script.
[0043] like Figure 5 As shown, this step specifically includes the following steps: Step 4-1, perform architecture compliance check on the automatically generated source code files.
[0044] The architecture compliance check is explained by taking the support for component state variables and output variable initialization integrity check as an example. Figure 6 As shown, it mainly involves data validation and state management: Step 4-1-1, parse the C / C++ source code through syntax and semantic analysis to obtain an abstract syntax tree; Step 4-1-2, traverse the abstract syntax tree to find nodes related to component state variables and output variables, which involves checking variable declaration, initialization, and possible usage; Step 4-1-3, identify the state variables and output variables through annotations, specific naming conventions, or specific types; Step 4-1-4, check whether the state variables and output variables are correctly initialized on all possible execution paths in combination with control flow analysis; Step 4-1-5, generate a check report, list the variables that are not initialized correctly and the reasons, or provide repair suggestions, or automatically repair.
[0045] Step 4-2, call the architecture analysis tool Generator to check the code for variable initialization, assignment, and call, and output the result file.
[0046] Step 4-3, use Python script to parse the source code file to complete the code inspection and generate the inspection result file.
[0047] Step 5: Provide a batch script to facilitate users to install and start the tool with one click.
[0048] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A Python-based automatic code generation and automatic checking method, characterized by: The following steps are involved: Step 1, read configuration and common program class files; Step 2, extract the configuration file, develop a source code parsing program, and analyze and process the source code of each function provided by the user; Step 3, develop a Python-based code automatic generation program to generate corresponding source code files according to the extracted source code and configuration; Step 4: Develop a code constraint automated checking tool library to perform architecture compliance checks and generate check result files.
2. The method for automatic code generation and automatic checking based on Python according to claim 1, characterized in that: In step 1, the configuration and common program class files include yaml configuration files, re library, AST module, and csv library.
3. The method for automatic code generation and automatic checking based on Python according to claim 2, characterized in that: The step 2 comprises the following specific steps: Step 2-1, use the AST module to parse the source code into an abstract syntax tree; Step 2-2, traverse the abstract syntax tree to find the definitions of global and local variables, as well as the code that needs to be modified; Step 2-3, modify and rewrite the source code through regular expressions or other text processing methods according to the configured rules; Step 2-4, converting the abstract syntax tree after the modified code back to the source code form; Step 2-5, save the modified source code to the corresponding code path.
4. The method for automatic code generation and automatic checking based on Python according to claim 2, characterized in that: The step 2 specifically includes the following steps: Step 2-1, use Python's open method to read the source code file and process the file content line by line; Step 2-2: For each line of source code, use a regular expression to match the part that needs to be modified; Step 2-3, rewrite the part that needs to be modified according to the matching results and the rules configured in the re-library; Step 2-4, write the modified code into a new file, or overwrite the original source code file; Step 2-5, save the new file or modified source code file to the corresponding code path.
5. The method for automatic code generation and automatic checking based on Python according to claim 2, characterized in that: In step 3, the source code file is generated by one or a combination of the following solutions: (1) Static code analysis: Use the code parsing library in Python to analyze C / C++ source code and obtain the definition of the code; traverse the abstract syntax tree of the code, identify the definition of the code, and generate the corresponding declaration of the code; write a custom abstract syntax tree traverser to identify the definition of the code according to the node type of the abstract syntax tree traverser; (2) Source code scanning tool: Use Python to write a source code scanning tool to scan the code definitions in C / C++ source code; use Python's regular expressions and string processing functions to locate and extract code information; generate the corresponding declaration header file of the code based on the scanning results or insert declaration statements inside the declaration function; (3) Template generation: Define the template for code declaration, use Python's template engine to generate the corresponding declaration header file according to the template; reserve the code location in the template and fill in the name and type of the code; write Python scripts to automatically generate header files and integrate them into the platform construction process; (4) Code instrumentation: Use Python's code parsing library to analyze C / C++ source code, identify the definition and content of the declaration function; automatically insert the code declaration statement inside the declaration function according to the needs; write a custom stub to determine the insertion position according to the syntax structure and rules of the declaration function, and generate the corresponding declaration statement of the code; (5) Regular expression matching: Use regular expressions in Python to match code definitions in C / C++ source code; write appropriate regular expression patterns to identify code declarations and extract relevant information; generate corresponding declaration statements for the code based on the matching results and write them into the declaration header file.
6. The method for automatic code generation and automatic checking based on Python according to claim 1, characterized in that: The step 4 specifically comprises the following steps: Step 4-1, check the architecture compliance of the automatically generated source code file; Step 4-2, call the architecture analysis tool Generator to check the code for variable initialization, assignment, and call, and output the result file; Step 4-3, use Python script to parse the source code file to complete the code inspection and generate the inspection result file.
7. The method for automatic code generation and automatic checking based on Python according to claim 6, characterized in that: The step 4-1 specifically includes the following steps: Step 4-1-1, parse the C / C++ source code through syntax and semantic analysis to obtain an abstract syntax tree; Step 4-1-2, traverse the abstract syntax tree to find nodes related to component state variables and output variables, which involves checking the use of variable declaration and initialization; Step 4-1-3, identify the state variables and output variables through annotations, specific naming conventions, or specific types; Step 4-1-4, check whether the state variables and output variables are correctly initialized on all execution paths in combination with control flow analysis; Step 4-1-5, generate a check report, list the variables that are not initialized correctly and the reasons, or provide repair suggestions, or automatically repair.
8. The method for automatic code generation and automatic checking based on Python according to claim 1, characterized in that: Also includes: Step 5: Provide a batch script to facilitate users to install and start the tool with one click.
Citation Information
Patent Citations
Code detection method and device, electronic equipment and storage medium
CN107506304A
Python code static analysis method and device
CN116303053A
Code generation method and device, equipment, storage medium and computer program
CN116679912A
Software refactoring systems and methods
US10534604B1
Method for implementing compiled embedded python
WO2022099888A1
Cited By
Clock reset network generation method based on multi-constraint driving and self-adaption
CN120562349A
