Resource loading method and device for python code

By acquiring and converting the target code in Python instructions into executable code, the problem of excessive system resource consumption caused by loading Python code resources is solved, achieving efficient resource utilization.

CN113391816BActive Publication Date: 2026-04-17BEIJING YUANLI WEILAI SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING YUANLI WEILAI SCI & TECH CO LTD
Filing Date
2021-06-15
Publication Date
2026-04-17

AI Technical Summary

Technical Problem

Conventional Python code resource loading methods lead to excessive system resource consumption, especially when loading the entire executable code of a Python instruction when only a portion of the functionality is needed.

Method used

Obtain the target code, convert it into executable code using a preset conversion tool, and load only the executable code that implements the target function, avoiding loading and converting the entire code of the entire Python instruction.

Benefits of technology

It reduces the amount of data during loading and the amount of code during conversion, saving system resources and improving resource utilization efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113391816B_ABST
    Figure CN113391816B_ABST
Patent Text Reader

Abstract

This invention discloses a method and apparatus for loading Python code resources, relating to the field of data processing technology, primarily addressing the problem of excessive system resource consumption caused by conventional Python code resource loading methods. The method comprises: obtaining target code, wherein the target code is a portion of Python code statements within a user-compiled Python instruction; the target code includes a fully functional Python code block or at least one Python code statement within the Python code block, and the target code is obtained before the user needs to execute the Python instruction; converting the target code into corresponding executable code using a preset conversion tool, the executable code being the code that implements the function of each Python code statement when the Python instruction is executed; and loading the executable code to implement the corresponding function. This invention is used in the resource loading process of Python code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing technology, and in particular to a method and apparatus for loading resources from Python code. Background Technology

[0002] Typically, when writing Python software code, users need to execute corresponding code to achieve certain functions. Python was designed in the early 1990s by engineers at the Netherlands Organization for Mathematical and Computer Science as an alternative to the ABC programming language. Due to its efficient high-level data structures and simple, effective object-oriented programming features, Python has gradually gained importance in the programming field.

[0003] However, in practical applications, the conventional method of loading Python code resources loads all the runtime code resources corresponding to the entire Python instruction. In other words, when a Python instruction contains a lot of Python code, or when the user only needs to implement the functionality of a part of the Python code in the Python instruction, the conventional method of loading Python code resources will load too many runtime code resources, resulting in unnecessary runtime code being loaded, which in turn leads to excessive system resource consumption. Summary of the Invention

[0004] In view of the above problems, the present invention provides a method and apparatus for loading resources of Python code, the main purpose of which is to solve the problem that conventional Python code resource loading methods result in large system resource consumption.

[0005] To address the aforementioned technical problems, in a first aspect, the present invention provides a resource loading method for Python code, the method comprising:

[0006] Obtain target code, wherein the target code is a portion of Python code statements in a Python instruction compiled by the user; the target code includes a Python code block with full functionality or at least one Python code statement in the Python code block, and the target code is obtained before the user needs to execute the Python instruction;

[0007] According to a preset conversion tool, the target code is converted into the corresponding executable code, which is the code that implements the function of each Python code statement when the Python instruction is executed;

[0008] Load the runtime code so that the corresponding function can be implemented through the runtime code.

[0009] Optionally, before converting the target code to the corresponding executable code according to a preset conversion tool, the method further includes:

[0010] Obtain a preset correspondence and determine whether the target code exists in the preset correspondence, wherein the preset correspondence includes each Python code statement and its corresponding running code;

[0011] The loading of the running code includes:

[0012] If the target code is found to exist in the preset correspondence, the corresponding running code is determined from the resource library according to the preset correspondence, and the running code is loaded. The resource library contains at least one corresponding running code, and the resource library is constructed by the user in advance by converting the Python code statement into the corresponding running code through the preset conversion tool.

[0013] Optionally, before obtaining the preset correspondence, the method further includes:

[0014] Determine whether the preset correspondence exists;

[0015] The step of obtaining the preset correspondence and determining whether the target code exists in the preset correspondence includes:

[0016] If the preset correspondence exists, the preset correspondence is obtained, and it is determined whether the target code exists in the preset correspondence.

[0017] Optionally, before obtaining the target code, the method further includes:

[0018] The Python code statement corresponding to the program pointer is determined from the Python instructions and used as the target code, wherein the program pointer is used to represent the Python code statement that needs to be executed in the Python instructions.

[0019] Optionally, determining the Python code statement corresponding to the program pointer from the Python instructions, as the target code, via the program pointer, includes:

[0020] Determine whether the pointer parameter of the program pointer is null. The pointer parameter is used to represent the position of the Python code statement to be executed in the Python instruction.

[0021] If it is not empty, then in the Python instruction, the corresponding Python code statement is determined according to the pointing parameter, and it is used as the target code;

[0022] If the value is empty, a prompt message will be output, which will indicate to the user that the target code is not required.

[0023] Optionally, before converting the target code to the corresponding executable code according to a preset conversion tool, the method further includes:

[0024] Determine whether the target code is code to be converted. The code to be converted is Python code statements that need to be converted to the corresponding running code. The code to be converted includes import statements.

[0025] The step of converting the target code into corresponding executable code according to a preset conversion tool includes:

[0026] If the target code is determined to be the code to be converted, then the target code is converted into the corresponding runtime code according to the preset conversion tool.

[0027] Optionally, the running code includes Javascript code statements, and the preset conversion tool includes the Skulpt conversion tool, which is used to convert Python code statements into corresponding Javascript code statements;

[0028] The step of converting the target code into corresponding executable code according to a preset conversion tool includes:

[0029] A conversion request is sent to the backend server, wherein the conversion request contains the target code;

[0030] The system receives feedback information from the backend server and obtains the corresponding Javascript code statements from the feedback information. The backend server is used to convert the target code into the corresponding Javascript code statements using the Skulpt conversion tool and then provides feedback.

[0031] The loading of the runtime code, so as to implement the corresponding function through the runtime code, includes:

[0032] Load the JavaScript code statements so that the JavaScript code statements can implement the function corresponding to the target code.

[0033] Secondly, embodiments of the present invention also provide a resource loading device for Python code, comprising:

[0034] An acquisition unit is used to acquire target code, wherein the target code is a portion of Python code statements in a Python instruction compiled by the user; the target code includes a Python code block with complete functionality or at least one Python code statement in the Python code block, and the target code is acquired before the user needs to execute the Python instruction;

[0035] The conversion unit is used to convert the target code into corresponding executable code according to a preset conversion tool. The executable code is the code that implements the function of each Python code statement when the Python instruction is executed.

[0036] The loading unit is used to load the running code so that the corresponding function can be implemented through the running code.

[0037] To achieve the above objectives, according to a third aspect of the present invention, a storage medium is provided, the storage medium including a stored program, wherein, when the program is executed, a resource loading method for controlling the device where the storage medium is located to execute the Python code described in any one of the first aspects is provided.

[0038] To achieve the above objectives, according to a fourth aspect of the present invention, an apparatus is provided, the apparatus comprising at least one processor, and at least one memory and bus connected to the processor; wherein the processor and memory communicate with each other via the bus; the processor is configured to invoke program instructions in the memory to execute a resource loading method of Python code as described in any one of the first aspects.

[0039] By employing the above technical solution, the Python code resource loading method and apparatus provided by this invention address the problem of excessive system resource consumption caused by conventional Python code resource loading methods. This invention obtains target code, wherein the target code is a portion of Python code statements within user-compiled Python instructions; then, according to a preset conversion tool, the target code is converted into corresponding executable code, which is the code that implements the function of each Python code statement when the Python instructions are executed; finally, the executable code is loaded so that the corresponding function can be implemented, thereby achieving the resource loading function of Python code. Compared to existing technologies, the above solution, because the final loaded executable code is converted from target code, and the target code is a portion of Python code statements within a Python instruction, including a fully functional Python code block or at least one Python code statement within that block, and because the target code is obtained before the user executes the Python instruction—meaning the target executable code is not the entire Python code statement corresponding to the entire Python instruction, but rather a portion of it—ensures that during loading, only the executable code corresponding to a portion of the Python instruction needs to be loaded each time, without needing to load the entire Python code. This reduces the amount of data loaded each time, thereby reducing the system resource consumption during loading. Furthermore, because the above solution converts target code each time, rather than the entire Python instruction, there is no need to convert all Python code statements within the entire instruction. This reduces the amount of code that needs to be converted each time, thus reducing the amount of data converted during the conversion process. This ensures that when the user only needs to execute the functionality of a portion of the Python instruction, only that portion of code is converted, avoiding the conversion of unnecessary Python code statements and saving system resources.

[0040] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description

[0041] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:

[0042] Figure 1 A flowchart of a resource loading method for Python code provided by an embodiment of the present invention is shown;

[0043] Figure 2 This diagram illustrates the specific steps involved in executing a resource loading method for Python code according to an embodiment of the present invention.

[0044] Figure 3 This diagram illustrates the composition of a resource loading device for Python code according to an embodiment of the present invention.

[0045] Figure 4 A block diagram of a device for loading resources for Python code, provided by an embodiment of the present invention, is shown. Detailed Implementation

[0046] Exemplary embodiments of the invention will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the invention are shown in the drawings, it should be understood that the invention may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this invention will be thorough and complete, and will fully convey the scope of the invention to those skilled in the art.

[0047] To address the issue of excessive system resource consumption caused by conventional Python code resource loading methods, this invention provides a Python code resource loading method, such as... Figure 1 As shown, the method includes:

[0048] 101. Obtain the target code.

[0049] The target code is a portion of Python code statements within a user-compiled Python instruction. The target code includes a fully functional Python code block or at least one Python code statement within that block, and is obtained before the user executes the Python instruction.

[0050] Due to its syntax, dynamic typing, and interpreted language characteristics, Python has become a popular programming language for scripting and rapid application development on most platforms. With continuous updates and the addition of new features, it has increasingly been used for independent, large-scale project development. The Python interpreter is easily extensible, allowing for the addition of new functionalities and data types using C or C++ (or other languages ​​that can be called from C). Python can also be used as an extension language in customizable software. Python's rich standard library provides source code or machine code for various major system platforms.

[0051] In this embodiment of the invention, the target code is actually a part of the Python instructions formed by all the Python code statements compiled by the user. Specifically, the target code can be a Python code block with complete functionality, or a part of a Python code block with complete functionality, such as a single Python code statement within a Python code block. In specific implementation, since a Python instruction may contain multiple statements or multiple functions, and each function may require at least one statement, in this embodiment, the target code can be a Python code block corresponding to each complete function, or it can be a single Python code statement. For example, when the Python instruction is an instruction to control a robot to perform actions, containing code blocks A, B, and C, each code block corresponds to a function. In this embodiment, the target code can be any one of code blocks A, B, or C. Of course, in practical applications, assuming code block A contains statements a and b, the target code in this embodiment can also be statement a or statement b. Specifically, whether the target code is a Python code block or a Python code statement can be selected based on the number of statements in the Python instruction and the user's preference, and is not limited here.

[0052] Furthermore, in this embodiment, the method of obtaining the target code is not limited. For example, it can be obtained through user input, or the user can input complete Python instructions and then determine and obtain the required target code based on preset identifiers or specific statements. However, it should be noted that the target code is obtained after the user has compiled the complete Python instructions and before executing those instructions.

[0053] Since the target code obtained in this step is only a code block or a code statement in a Python instruction, it is not necessary to convert all Python instructions and obtain the corresponding running code during the loading process. This reduces the amount of code that needs to be converted in subsequent conversion processes during the execution of this method. In particular, when the user only needs to implement a specific function in the Python instruction, only that part of the code needs to be converted, reducing unnecessary system resource consumption. This also avoids the problem of large system resource consumption caused by conventional Python code resource loading methods during subsequent conversion and loading of running code.

[0054] 102. Using a preset conversion tool, convert the target code into the corresponding runtime code.

[0055] The running code is the code that implements the function of each Python code statement when the Python instruction is executed.

[0056] Typically, when a user compiles Python instructions built from Python code, all the runtime code resources corresponding to the entire Python instruction are loaded, so that the entire Python instruction is implemented based on the complete runtime code during subsequent execution. For example, when a Python code statement implements a part of its functionality, it needs to be implemented by a corresponding JavaScript code statement. Therefore, once the target code has been determined in the aforementioned steps, it can be converted using a preset conversion tool. Of course, in specific applications, the preset conversion tool required for the conversion process can be determined based on the type of subsequent runtime code. For example, when the runtime code needs to be in C language format, then the preset conversion tool in this embodiment is a C language converter.

[0057] 103. Load the running code so that the corresponding function can be implemented through the running code.

[0058] After the target code is converted into the corresponding executable code, since the implementation of Python code statements requires the executable code to be implemented, when the Python instruction is executed, the executable code corresponding to the corresponding Python code statement needs to be loaded, so as to implement the corresponding function based on the executable code.

[0059] As can be seen from the above scheme, since the final loaded executable code is transformed from the target code, and the target code is a portion of the Python code statements within the Python instruction—meaning the target executable code is not the entire Python code statement corresponding to the entire Python instruction, but only a portion of the Python instruction—this ensures that during the loading process, only the executable code corresponding to a portion of the Python instruction needs to be loaded each time, without loading the entire Python code. This reduces the amount of data loaded in each loading process, thereby reducing the system resource consumption during loading. Simultaneously, since the above scheme of this invention transforms the target code each time, rather than the entire Python instruction, it is unnecessary to transform all the Python code statements within the entire Python instruction. This reduces the amount of code that needs to be transformed in each loading process, thereby reducing the amount of data transformed during the transformation process. This ensures that when the user only needs to execute the functionality of a portion of the Python instruction, only that portion of code is transformed, avoiding the transformation of unnecessary Python code statements and saving system resources.

[0060] In an optional embodiment, before converting the target code to the corresponding runtime code according to a preset conversion tool in step 102, the method further includes:

[0061] Obtain a preset correspondence and determine whether the target code exists in the preset correspondence, wherein the preset correspondence includes each Python code statement and its corresponding running code;

[0062] Based on this, loading the runtime code in step 103 above includes:

[0063] If the target code is found to exist in the preset correspondence, the corresponding running code is determined from the resource library according to the preset correspondence, and the running code is loaded. The resource library contains at least one corresponding running code, and the resource library is constructed by the user in advance by converting the Python code statement into the corresponding running code through the preset conversion tool.

[0064] Since in practical applications, users or systems may have already pre-stored the corresponding target code's runtime code, before converting the target code into runtime code in step 102, it is also possible to determine whether the target code exists based on the preset correspondence. If it exists, it means that the target code has already been pre-converted into the corresponding runtime code. If it does not exist, it means that the target code conversion operation has not been performed before, which means that the target runtime code still needs to be converted by the preset conversion tool.

[0065] Once the target code is found in the preset correspondence, it can be determined that there is no need to convert the target code further; instead, the corresponding runtime code can be directly obtained based on the preset correspondence. Therefore, in the aforementioned embodiment, step 103 can directly obtain and load the runtime code based on the preset correspondence during the loading process.

[0066] By using the above method, before converting the target code into executable code, we first determine whether the target code exists in the preset correspondence. In other words, we pre-determine whether the target code has been converted by the user or the system before conversion. This ensures that after the target code is converted into executable code in advance, the executable code can be directly obtained and loaded based on the preset correspondence, avoiding unnecessary conversion processes and thus saving system resources and time consumed by the conversion process.

[0067] In an optional embodiment, before obtaining the preset correspondence in the foregoing embodiments, the method may further include:

[0068] Determine whether the preset correspondence exists;

[0069] In the foregoing embodiments, obtaining a preset correspondence and determining whether the target code exists in the preset correspondence can specifically include the following during execution:

[0070] If the preset correspondence exists, the preset correspondence is obtained, and it is determined whether the target code exists in the preset correspondence.

[0071] In some cases, users may not have pre-converted their Python code statements, or their devices or systems may not have a corresponding runtime code library for the Python code statements. In such cases, the preset correspondence described in the previous embodiments does not exist. Therefore, in this embodiment, it can be determined whether such a preset correspondence exists. If it does, it indicates that the Python code statements have been pre-converted, or that the system has a built-in runtime code library. Thus, by determining whether the preset correspondence exists, the problem of failure that may occur when directly obtaining the preset correspondence can be avoided.

[0072] It should be noted that, in practical applications, the methods described in these embodiments of the present invention can all be executed on the client side. That is, the entire process from obtaining the target code, converting the target code into executable code, and loading the executable code can be performed on a single device, such as a client or server. When the above methods are executed on the server, it can be understood that after the user completes the compilation of the Python instructions on the client side, the server obtains the target code from the Python instructions from the client, converts the target code into the corresponding executable code using a preset conversion tool, loads the executable code, and then sends the loading result back to the client.

[0073] However, in some cases, the conversion process may consume some system resources. To mitigate resource consumption, the conversion process can be performed on a backend server. Using the method described in this embodiment, the execution process can involve the frontend device or client obtaining the target code, sending it to the backend server, and the backend server determining whether a preset correspondence exists locally. If a preset correspondence exists, the server queries the corresponding target code's execution code based on that correspondence and returns the query result to the client. If the target code exists in the preset correspondence, the server directly returns the retrieved execution code to the client. Conversely, if the target code does not exist in the preset correspondence or if the preset correspondence itself does not exist, the server can use a preset conversion tool to convert the target code to the target code.

[0074] In an optional embodiment, before obtaining the target code in step 101 above, the method further includes:

[0075] The Python code statement corresponding to the program pointer is determined from the Python instructions and used as the target code, wherein the program pointer is used to represent the Python code statement that needs to be executed in the Python instructions.

[0076] In this embodiment, since the Python code statement needs to be converted in many cases, the entire Python instruction has already been completed and is in the process of execution, in this case, the program pointer can be used to determine which Python code statement in the entire Python instruction needs to be executed. This Python code statement that needs to be executed may be the one that needs to be converted and loaded later. Therefore, the target code can be determined based on the program pointer.

[0077] Using the above method, the target Python code statement to be executed can be determined based on the program pointer during the execution process when the Python instruction is executed. This achieves the effect of only converting and loading the Python code statement to be executed during the execution of the Python instruction. As a result, the determination of the target code can be based on the execution needs without issuing additional corresponding instructions, which simplifies the execution steps of the method described in this embodiment and has good convenience.

[0078] In an optional embodiment, when determining the target code based on the program pointer, it can actually be determined by a specific pointer parameter. This pointer parameter can be understood as the specific content pointed to by the pointer. In the steps of the aforementioned embodiment, the Python code statement corresponding to the program pointer is determined from the Python instructions through the program pointer, which serves as the target code. During execution, it can be executed in the following specific manner:

[0079] 201. Determine if the parameter pointed to by the program pointer is null.

[0080] The pointer parameter is used to represent the position of the Python code statement to be executed in the Python instruction. Since the judgment result is of two kinds, if the pointer parameter is determined to be non-empty, it means that the program pointer points to a Python code statement, and step 202 is executed; otherwise, it means that the program pointer does not point to any Python code statement, and step 203 can be executed.

[0081] 202. In the Python instructions, the corresponding Python code statement is determined according to the pointing parameter, and is used as the target code.

[0082] In this case, since the pointer parameter is not empty and has specific content—that is, the specific content is actually the Python code statement pointed to by the program pointer—it is possible to directly determine which Python instruction is the target code based on the pointer parameter. This ensures that the determined target code is consistent with the statement pointed to by the program pointer, thus ensuring the accuracy of the target code determination result.

[0083] 203. Output a prompt message.

[0084] The prompt message is used to inform the user that there is no need to obtain the target code. In this case, it can be determined that the program instructions contain empty content, that is, the corresponding content does not exist. This also indicates that the Python instructions have been executed completely, and there is no need to perform the corresponding conversion and loading operations. Therefore, a prompt can be given to the user to avoid program errors caused by attempting to obtain the target code again.

[0085] In an optional embodiment, in some cases, there may be a need not to convert and load all Python code statements in the Python instructions. In this embodiment, before step 102 in the aforementioned embodiment, where the target code is converted to the corresponding runtime code according to a preset conversion tool, the method further includes:

[0086] Determine whether the target code is code to be converted. The code to be converted is Python code statements that need to be converted into the corresponding running code. The code to be converted includes import statements.

[0087] In this embodiment, the code to be converted can be understood as code that needs to use running code to achieve its function, represented by the import statement. When the Python code statement is such as the import statement, it means that the code definitely needs to rely on external running code to achieve its function.

[0088] Based on this, the foregoing embodiments convert the target code into corresponding executable code using a preset conversion tool, including:

[0089] If the target code is determined to be the code to be converted, then the target code is converted into the corresponding runtime code according to the preset conversion tool.

[0090] By determining whether the target code is something like an import statement during the conversion process, it can be ensured that the Python code statements that need to be converted are converted during the conversion process, thus avoiding the waste of time and system resources caused by converting unnecessary code statements.

[0091] In an optional embodiment, in some practical scenarios, the target code may subsequently be JavaScript code statements to implement functionality. In this case, the corresponding conversion tool, i.e., the preset conversion tool, may specifically be the Skulpt conversion tool. The Skulpt conversion tool is used to convert Python code statements into corresponding JavaScript code statements. JavaScript (JS) is a lightweight, interpreted or just-in-time (JIT) programming language with a function-first approach. Although it is best known as a scripting language for developing web pages, it is also used in many non-browser environments. JavaScript is a prototype-based, multi-paradigm dynamic scripting language that supports object-oriented, imperative, and declarative (such as functional programming) styles. Furthermore, Skulpt is an online Python code execution environment implemented in Java, meaning it allows users to run Python code in a browser; that is, it parses and converts Python code into JavaScript code that the Java platform can recognize.

[0092] In some scenarios, to reduce system overhead on the user's end during the compilation and execution of Python instructions, the Python code can be converted into Javascript code over the network. Based on this, in the aforementioned embodiment, step 102, which converts the target code into corresponding executable code using a preset conversion tool, includes:

[0093] A conversion request is sent to the backend server, wherein the conversion request contains the target code;

[0094] The system receives feedback information from the backend server and obtains the corresponding Javascript code statements from the feedback information. The backend server is used to convert the target code into the corresponding Javascript code statements using the Skulpt conversion tool and then provides feedback.

[0095] The loading of the runtime code, so as to implement the corresponding function through the runtime code, includes:

[0096] Load the JavaScript code statements so that the JavaScript code statements can implement the function corresponding to the target code.

[0097] In the above scheme, after the client compiles the Python instructions, it can obtain the corresponding running code from the backend server remotely during execution. This ensures that the client does not need to consume system resources to convert Python code statements during execution. Instead, the client can perform the conversion operation and provide feedback based on the backend server. This reduces the system pressure on the client side. As a result, the client only needs to have the function of compiling Python code, without having the function of converting Javascript code statements, while maintaining communication with the backend server. This reduces the system capability requirements of the client and makes it easier for users to compile Python code.

[0098] Furthermore, as a response to the above Figure 1 In addition to the implementation of the method shown, this embodiment of the invention also provides a resource loading device for Python code, used for loading the above-mentioned... Figure 1 The method shown is implemented accordingly. This device embodiment corresponds to the foregoing method embodiment. For ease of reading, this device embodiment will not repeat the details of the foregoing method embodiment, but it should be clear that the device in this embodiment can implement all the contents of the foregoing method embodiment. Figure 3 As shown, the device includes: an acquisition unit 31, a conversion unit 32, and a loading unit 33, wherein...

[0099] The acquisition unit 31 can be used to acquire target code, wherein the target code is a portion of Python code statements in a Python instruction compiled by the user; the target code includes a Python code block with complete functionality or at least one Python code statement in the Python code block, and the target code is acquired before the user needs to execute the Python instruction;

[0100] The conversion unit 32 can be used to convert the target code into corresponding executable code according to a preset conversion tool. The executable code is the code that implements the function of each Python code statement when executing the Python instruction.

[0101] The loading unit 33 can be used to load the running code so that the corresponding function can be implemented through the running code.

[0102] By employing the above technical solution, embodiments of the present invention provide a method and apparatus for loading Python code resources. Addressing the problem of excessive system resource consumption caused by conventional Python code resource loading methods, the present invention obtains target code, wherein the target code is a portion of Python code statements within user-compiled Python instructions; then, according to a preset conversion tool, the target code is converted into corresponding executable code, wherein the executable code is the code that implements the function of each Python code statement when the Python instructions are executed; finally, the executable code is loaded so that the corresponding function can be implemented through the executable code, thereby realizing the resource loading function of Python code. Compared to existing technologies, the above solution, because the final loaded executable code is converted from target code, and the target code is a portion of Python code statements within a Python instruction, including a fully functional Python code block or at least one Python code statement within that block, and this target code is obtained before the user needs to execute the Python instruction; that is, the target executable code is not the entire set of Python code statements corresponding to the entire Python instruction, but rather a portion of the Python instruction, this ensures that during the loading process, only the executable code corresponding to a portion of the Python instruction needs to be loaded each time, without needing to load the entire set of Python code. This reduces the amount of data loaded in each loading process, thereby reducing the system resource consumption during the loading process. Simultaneously, because the above solution of this invention converts target code each time, rather than the entire Python instruction, it eliminates the need to convert all Python code statements within the entire Python instruction, thus reducing the amount of code that needs to be converted in each loading process, and consequently reducing the amount of data converted during the conversion process. This ensures that when the user only needs to execute the functionality of a portion of the Python instruction, only that portion of code is converted, avoiding the conversion of unnecessary Python code statements and saving system resources.

[0103] The processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured; by adjusting kernel parameters, the problem of excessive system resource consumption caused by the conventional resource loading method of Python code can be addressed.

[0104] This invention provides a storage medium storing a program that, when executed by a processor, implements a resource loading method for the Python code.

[0105] This invention provides a processor for running a program, wherein the program executes a resource loading method of the Python code during runtime.

[0106] This invention provides a device 40, such as... Figure 4 As shown, the device includes at least one processor 401, and at least one memory 402 and bus 403 connected to the processor; wherein, the processor 401 and the memory 402 communicate with each other through the bus 403; the processor 401 is used to call program instructions in the memory to execute the resource loading method of the Python code described above.

[0107] The devices mentioned in this article can be servers, PCs, tablets, mobile phones, etc.

[0108] This application also provides a computer program product, which, when executed on a process management device, is suitable for executing an initialization program with the following method steps: obtaining target code, wherein the target code is a portion of Python code statements in a user-compiled Python instruction; the target code includes a Python code block with complete functionality or at least one Python code statement in the Python code block, and the target code is obtained before the user needs to execute the Python instruction; converting the target code into corresponding executable code according to a preset conversion tool, the executable code being the code that implements the function of each Python code statement when the Python instruction is executed; loading the executable code so that the corresponding function can be implemented through the executable code.

[0109] Furthermore, before converting the target code to the corresponding executable code according to the preset conversion tool, the method further includes:

[0110] Obtain a preset correspondence and determine whether the target code exists in the preset correspondence, wherein the preset correspondence includes each Python code statement and its corresponding running code;

[0111] The loading of the running code includes:

[0112] If the target code is found to exist in the preset correspondence, the corresponding running code is determined from the resource library according to the preset correspondence, and the running code is loaded. The resource library contains at least one corresponding running code, and the resource library is constructed by the user in advance by converting the Python code statement into the corresponding running code through the preset conversion tool.

[0113] Furthermore, before obtaining the preset correspondence, the method further includes:

[0114] Determine whether the preset correspondence exists;

[0115] The step of obtaining the preset correspondence and determining whether the target code exists in the preset correspondence includes:

[0116] If the preset correspondence exists, the preset correspondence is obtained, and it is determined whether the target code exists in the preset correspondence.

[0117] Furthermore, prior to obtaining the target code, the method further includes:

[0118] The Python code statement corresponding to the program pointer is determined from the Python instructions and used as the target code, wherein the program pointer is used to represent the Python code statement that needs to be executed in the Python instructions.

[0119] Furthermore, determining the Python code statement corresponding to the program pointer from the Python instructions, as the target code, via the program pointer, includes:

[0120] Determine whether the pointer parameter of the program pointer is null. The pointer parameter is used to represent the position of the Python code statement to be executed in the Python instruction.

[0121] If it is not empty, then in the Python instruction, the corresponding Python code statement is determined according to the pointing parameter, and it is used as the target code;

[0122] If the value is empty, a prompt message will be output, which will indicate to the user that the target code is not required.

[0123] Furthermore, before converting the target code to the corresponding executable code according to the preset conversion tool, the method further includes:

[0124] Determine whether the target code is code to be converted. The code to be converted is Python code statements that need to be converted to the corresponding running code. The code to be converted includes import statements.

[0125] The step of converting the target code into corresponding executable code according to a preset conversion tool includes:

[0126] If the target code is determined to be the code to be converted, then the target code is converted into the corresponding runtime code according to the preset conversion tool.

[0127] Furthermore, the running code includes Javascript code statements, and the preset conversion tool includes the Skulpt conversion tool, which is used to convert Python code statements into corresponding Javascript code statements;

[0128] The step of converting the target code into corresponding executable code according to a preset conversion tool includes:

[0129] A conversion request is sent to the backend server, wherein the conversion request contains the target code;

[0130] The system receives feedback information from the backend server and obtains the corresponding Javascript code statements from the feedback information. The backend server is used to convert the target code into the corresponding Javascript code statements using the Skulpt conversion tool and then provides feedback.

[0131] The loading of the runtime code, so as to implement the corresponding function through the runtime code, includes:

[0132] Load the JavaScript code statements so that the JavaScript code statements can implement the function corresponding to the target code.

[0133] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable process management device to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable process management device, generate instructions for implementing the process... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0134] In a typical configuration, the device includes one or more processors (CPUs), memory, and a bus. The device may also include input / output interfaces, network interfaces, etc.

[0135] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, like read-only memory (ROM) or flash RAM, and memory includes at least one memory chip. Memory is an example of computer-readable media.

[0136] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.

[0137] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.

[0138] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0139] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.

Claims

1. A resource loading method for Python code, characterized in that, include: The program pointer determines the Python code statement to be executed as the target code. If the pointer parameter is empty, a prompt message is output to inform the user that the target code is not needed. If it is not empty, the corresponding Python code statement in the Python instruction is determined according to the pointer parameter and used as the target code. The program pointer represents the Python code statement to be executed in the Python instruction. The target code is a portion of the Python code statements in the user-compiled Python instruction. The target code includes a fully functional Python code block or at least one Python code statement in the Python code block. The target code is obtained before the user needs to execute the Python instruction. The pointer parameter represents the position of the Python code statement to be executed in the Python instruction. Obtain a preset correspondence and determine whether the target code exists in the preset correspondence. If the target code is found to exist in the preset correspondence, determine the running code corresponding to the target code from the resource library according to the preset correspondence and load the running code. The preset correspondence includes each Python code statement and its corresponding running code. If no target code exists in the preset correspondence, determine whether the target code is code to be converted. The code to be converted is a Python code statement that needs to be converted to the corresponding running code. The code to be converted includes import statements and Python code statements that require external running code support. According to the preset conversion tool, the target code is converted into the corresponding running code. The running code is the code that implements the function of each Python code statement when executing the Python instruction. If the target code is determined to be the code to be converted, the target code is sent to the backend server, and the backend converts the target code into the corresponding Javascript code statement using the Skulpt conversion tool and provides feedback. Load the runtime code so that the corresponding function can be implemented through the runtime code.

2. The method according to claim 1, characterized in that, The resource library contains at least one corresponding executable code, and the resource library is built by the user in advance by converting the Python code statements into corresponding executable code using the preset conversion tool.

3. The method according to claim 2, characterized in that, Before obtaining the preset correspondence, the method further includes: Determine whether the preset correspondence exists; The step of obtaining the preset correspondence and determining whether the target code exists in the preset correspondence includes: If the preset correspondence exists, the preset correspondence is obtained, and it is determined whether the target code exists in the preset correspondence.

4. The method according to any one of claims 1-3, characterized in that, The step of converting the target code into corresponding executable code according to a preset conversion tool includes: If the target code is determined to be the code to be converted, then the target code is converted into the corresponding runtime code according to the preset conversion tool.

5. The method according to claim 4, characterized in that, The Skulpt conversion tool is used to convert Python code statements into corresponding Javascript code statements. The step of converting the target code into corresponding executable code according to a preset conversion tool includes: A conversion request is sent to the backend server, wherein the conversion request contains the target code; The system receives feedback information from the backend server and obtains the corresponding Javascript code statements from the feedback information. The backend server is used to convert the target code into the corresponding Javascript code statements using the Skulpt conversion tool and then provides feedback. The loading of the runtime code, so as to implement the corresponding function through the runtime code, includes: Load the JavaScript code statements so that the JavaScript code statements can implement the function corresponding to the target code.

6. A resource loading device for Python code, characterized in that, The apparatus comprising, using the method as described in any one of claims 1 to 5, includes: An acquisition unit is used to acquire target code, wherein the target code is a portion of Python code statements in a Python instruction compiled by the user; the target code includes a Python code block with complete functionality or at least one Python code statement in the Python code block, and the target code is acquired before the user needs to execute the Python instruction; The conversion unit is used to convert the target code into corresponding executable code according to a preset conversion tool. The executable code is the code that implements the function of each Python code statement when the Python instruction is executed. The loading unit is used to load the running code so that the corresponding function can be implemented through the running code.

7. A storage medium, characterized in that, The storage medium includes a stored program, wherein, when the program is executed, it controls the device where the storage medium is located to execute the resource loading method of Python code as described in any one of claims 1 to 5.

8. A device, characterized in that, The device includes at least one processor, and at least one memory and bus connected to the processor; wherein the processor and memory communicate with each other through the bus; the processor is used to call program instructions in the memory to execute the resource loading method of Python code as described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • On-demand loading of dynamic scripting language code for reduced memory usage

    CN106796525A

  • A python code online editing method and electronic equipment

    CN109725896A