Object calling method and device, equipment and storage medium
By automatically generating the calling code of the interface method, the problem of a large number of mechanical repetition in interface automation testing is solved, which improves writing efficiency and saves development time.
Patent Information
- Application Number
- CN202311018054.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-11
- Publication Date
- 2025-02-18
AI Technical Summary
In the process of writing interface automation test cases, a large number of interface methods need to be called, resulting in a large number of mechanical repetitions and reducing work efficiency.
By obtaining the method object in the pending code, determining its method type, obtaining the corresponding setting information, generating a call statement according to the preset splicing rules, and automatically generating the call code required to call the method object.
It improves the efficiency of calling code writing, saves code development time, and reduces mechanical repetition work of manual writing.
Smart Images

Figure CN119473239A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to an object calling method, apparatus, device and storage medium. Background Art
[0002] In the process of writing interface automated test cases, each business scenario needs to call a large number of interface methods, such as import statements, instance object initialization statements, interface method / function call statements, interface method / function parameter input statements, call result receiving assignment, etc. When manually writing the above statements to implement method calls, there is a lot of mechanical repetitive work, which reduces work efficiency. Therefore, it is necessary to develop a method to automatically generate the above basic code to implement interface method calls. Summary of the invention
[0003] In view of this, the present application provides an object calling method, apparatus, device and storage medium, which can automatically generate the code required for calling the object, improve the efficiency of writing calling code, and save code development time.
[0004] To achieve the above objectives, in a first aspect, an embodiment of the present application provides an object calling method, the method comprising:
[0005] Obtaining a method object in the code to be processed, and determining a method type of the method object;
[0006] Acquire setting information corresponding to the method type, where the setting information is key information for generating a call statement for the method object;
[0007] splicing the setting information according to a preset splicing rule to obtain the calling statement;
[0008] The calling code of the method object is generated according to the calling statement, and the calling code is executed to call the method object.
[0009] In a second aspect, an embodiment of the present application provides an object calling device, the device comprising:
[0010] A first acquisition unit acquires a method object in the code to be processed and determines a method type of the method object;
[0011] A second acquisition unit is used to acquire setting information corresponding to the method type, where the setting information is key information for generating a call statement of the method object;
[0012] A splicing unit, used for splicing the setting information according to a preset splicing rule to obtain the calling statement;
[0013] A generating unit is used to generate a calling code of the method object according to the calling statement, and execute the calling code to call the method object.
[0014] In a third aspect, an embodiment of the present application provides an electronic device, including:
[0015] processor;
[0016] a memory for storing processor-executable instructions;
[0017] The processor implements the above method by running the executable instructions.
[0018] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having computer instructions stored thereon, which implement the above method when executed by a processor.
[0019] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium storing executable instructions for causing a processor to execute the executable instructions to implement the above-mentioned marketing strategy determination method.
[0020] In a sixth aspect, an embodiment of the present application provides a computer program product, which includes executable instructions, and the executable instructions are stored in a computer-readable storage medium; wherein, when the processor of the electronic device reads the executable instructions from the computer-readable storage medium and executes the executable instructions, the above-mentioned marketing strategy determination method is implemented.
[0021] In the object calling method provided in the present application, firstly, the method object in the code to be processed is obtained, and the method type of the method object is determined; next, the setting information corresponding to the method type is obtained, and the setting information is the key information for generating the calling statement of the method object; the setting information is spliced according to the pre-set splicing rules to obtain the calling statement; finally, the calling code of the method object is generated according to the calling statement, and the calling code is executed to realize the calling of the method object. Through the above method, the corresponding setting information can be obtained according to the method type of the method object, and the setting information can be spliced according to the pre-set splicing rules to obtain the calling statement, so that the calling code required to call the method object can be automatically generated, which improves the efficiency of writing calling code and saves code development time. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 It is a schematic diagram of an application environment of the object calling method provided by this application.
[0023] Figure 2 It is a flowchart of an object calling method provided by this application.
[0024] Figure 3 It is a flowchart of another object calling method provided by this application.
[0025] Figure 4 It is a block diagram of an object calling device provided by the present application.
[0026] Figure 5 This is a hardware structure diagram of an electronic device where an object calling device provided by the present application is located. DETAILED DESCRIPTION
[0027] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with one or more embodiments of this specification. Instead, they are merely examples of devices and methods consistent with some aspects of one or more embodiments of this specification as detailed in the appended claims.
[0028] It should be noted that: in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this specification. In some other embodiments, the steps included in the method may be more or less than those described in this specification. In addition, a single step described in this specification may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this specification may be combined into a single step for description in other embodiments.
[0029] In the process of writing interface automation test cases, each business scenario needs to call a large number of interface methods, such as import statements, instance object initialization statements, interface method / function call statements, interface method / function parameter input statements, call result receiving assignments, etc. In the case of manually writing the above statements to implement method calls, there is a large amount of mechanical repetitive work, which reduces work efficiency. In view of this, this specification proposes an object calling method, which obtains corresponding setting information according to the method type of the method object, and splices the setting information according to the pre-set splicing rules to obtain a calling statement, so as to automatically generate the calling code required to call the method object.
[0030] In order to better understand the object calling method, apparatus, device and storage medium provided by the present application, the application environment applicable to the present application is described below.
[0031] See also Figure 1 , Figure 11 shows a schematic diagram of an application environment of the object call provided by the present application. Optionally, the object call method provided by the present application can be applied to an electronic device, which can be Figure 1 The server 110 shown in the figure can be connected to the terminal device 120 through a network. The network is used to provide a medium for a communication link between the server 110 and the terminal device 120. The network can include various connection types, such as a wired communication link, a wireless communication link, etc., which are not limited in this application. Optionally, the electronic device can also be a smart phone, a laptop computer, etc.
[0032] It should be understood that Figure 1 The server 110, network and terminal device 120 are merely illustrative. Depending on the implementation requirements, there may be any number of servers, networks and terminal devices. Exemplarily, the server 110 may be a physical server or a server cluster composed of multiple servers, and the terminal device 120 may be a mobile phone, a tablet, a desktop computer, a laptop computer and the like. It is understandable that the present application may also allow multiple terminal devices 120 to access the server 110 at the same time. The terminal device 120 may receive the pending code imported by the user. Furthermore, the terminal device 120 sends the pending code to the server 110 via the network. After the server 110 receives the pending code, the pending code may be processed by the object calling method proposed in the present application.
[0033] Figure 2 This is a flow chart of an object calling method proposed in this application, which includes steps 201-204.
[0034] In step 201, a method object in the code to be processed is obtained, and a method type of the method object is determined.
[0035] The code to be processed can be the code imported by the user, such as the module object or class object imported by the user. For some programming languages, a script file can be regarded as a module, so the script file can also be directly imported. For example, for the Python language, a specified Python script file (py file) can be directly imported.
[0036] The code to be processed may also be the code under the file path input by the user. For example, the class object, module object, or script file contained in the file path may be used as the code to be processed.
[0037] A method object can include functions and / or methods. Functions can also be called ordinary methods, and methods refer to methods in class objects, that is, methods associated with objects. The method type of a method object refers to the result of classification according to attribution. Method types can include ordinary methods, instance methods, class methods, static methods, etc.
[0038] Optionally, it may be first determined whether the method object is a method in a class object. In the case where the method object is a method in a class object, the method type of the method object is determined to be a first type. The first type may include at least one of the following: an instance method, a class method, and a static method. Next, the specific type of the method object of the first type is further determined, that is, it is determined whether the method type is an instance method, a class method, or a static method.
[0039] When the method object is not a method in a class object, the method type is determined to be the second type (in this case, the method object can be called a common method). That is, first determine whether the method object is a method in a class object or a common method.
[0040] In step 202, setting information corresponding to the method type is obtained, where the setting information is key information for generating a calling statement of a method object.
[0041] For each method type, the corresponding setting information can be pre-set according to the key information of the call statement that generates the method object of this type. For example, when the method object is a common method, the corresponding setting information may include the method parameter list, method return value, method name, and the module name of the module where the method object is located; when the method object is a class method or a static method, the corresponding setting information may include the method parameter list, method return value, method name, the class name where the method object is located, and the module name of the module where the class is located; when the method object is an instance method, the corresponding setting information may include the method parameter list, method return value, method name, the class name where the method object is located, the class constructor parameter list, and the module name of the module where the class is located.
[0042] In step 203, the setting information is spliced according to a preset splicing rule to obtain a calling statement.
[0043] The calling code required to call method objects of different method types is different. For example, the import statements for common methods and methods in class objects are different; for instance methods, instantiation statements are required; the calling statements contained in the calling code required for method objects of different method types, such as common methods, instance methods, static methods, and class methods, are also different.
[0044] Therefore, for different method types, corresponding code splicing rules can be pre-set according to the grammatical format of the calling statement to be generated, so as to splice the setting information corresponding to the method type and obtain the import statement, instantiation statement, method calling statement, etc. required for the method call.
[0045] In step 204, a calling code of the method object is generated according to the calling statement, and the calling code is executed to call the method object.
[0046] For the method object of each method type, a calling code for calling the method object can be obtained according to the calling statement generated in step 203, and the method object can be called by executing the calling code.
[0047] In the object calling method provided in the present application, the method object in the code to be processed is first obtained, and the method type of the method object is determined; next, the setting information corresponding to the method type is obtained, and the setting information is the key information for generating the calling statement of the method object; according to the pre-set splicing rules, the setting information is spliced to obtain the calling statement; finally, the calling code of the method object is generated according to the calling statement, and the calling code is executed to realize the calling of the method object. Through the above method, the corresponding setting information can be obtained according to the method type of the method object, and the setting information can be spliced according to the pre-set splicing rules to obtain the calling statement, so that the calling code required to call the method object can be automatically generated, which improves the efficiency of writing calling code and saves code development time.
[0048] The following takes the Python code as an example to describe the object calling method proposed in this application. It should be understood by those skilled in the art that this method is also applicable to programming languages with the same calling rules, not limited to the Python language.
[0049] Optionally, when the method object includes a function or a method, the method object in the code to be processed may be obtained by the following method.
[0050] If the code to be processed includes a module object, the functions and first-class objects in the module object can be obtained by parsing the metadata information of the module object. For the parsed first-class objects, the methods in the first-class objects can be further filtered out by parsing the metadata information in the first-class objects.
[0051] If the code to be processed includes a second type of object, such as a class object directly imported by a user or a class object under a file path, the metadata information of the second type of object can be directly parsed to filter out the methods in the class object.
[0052] For example, the statement "inspect.getmembers" can be used to parse the metadata information of module objects, first-class objects, and second-class objects, and the method objects filtered out in this case do not include private methods and magic functions. In the case where the user inputs a py path, the py file path can be converted into a module object, and then the module object can be parsed using the above method to obtain a list of all methods under the file path.
[0053] Optionally, the following method can be used to determine whether a method object is a method in a class object.
[0054] Get the method full name (__qname__) and method name (__name__) in the method object metadata, and compare whether the method full name and method name are equal. If they are not equal, determine that the method object is a method in the class object, that is, a method object of the first type; if they are equal, determine that the method object is not a method in the class object, that is, a method object of the second type (ordinary method). For example, if for a method object, the method full name in the metadata is f, and the method name is f, since the method full name and the method name are equal, the method object is determined to be an ordinary method; if for a method object, the method full name in the metadata is Ap, and the method name is p, since the method full name and the method name are not equal, the method object is determined to be a method in the class object, and the class name of the method can be known as A. When it is determined that the method type is a method in a class object, it is necessary to determine whether it is an instance method, a class method, or a static method.
[0055] Optionally, the method type can be determined by parsing the decorators on the method through the abstract syntax tree. Specifically, the source code string saved in the method object can be obtained based on the Python built-in library inspect.getsource, and the source code of the method can be parsed separately based on the Python built-in library ast to generate an abstract syntax tree. Next, traverse the abstract syntax tree generated by the method to determine whether there are decorators listed in Table 2 on the method, and then the method type in the class object can be determined. Specifically, in the absence of a decorator, the method is determined to be an instance method; in the presence of a @classmethod decorator, the method is determined to be a class method; in the presence of a @staticmethod decorator, the method is determined to be a static method.
[0056] In Python, method objects in class objects can be divided into Function objects and Method objects according to whether they are instantiated or not. Table 1 is a list of the two object types. Whether instantiated specifically refers to whether the method in the class object is a method object obtained through an instance object. For example, Ap is a method object that is not instantiated, while A().p is a method object obtained through an instantiated object.
[0057] Table 1
[0058]
[0059] The main difference between Method and Function is the different display of metadata information. The metadata information of the Method object includes _self_ and _func_. For the parameter _self_, it represents the class object in the class method and the instance object in the instance method; for the parameter _func_, it points to the original method object Function object. Since the first parameter of the method in the class object needs to be passed in the instance object, the method Function object in the original class can be converted to a Method object, and the information is saved with _self_ metadata, and the original method object is saved with _func_.
[0060] For the method object obtained in step 202, when the method object is obtained based on the metadata information of the class object, the method object is of Function type. Therefore, before executing step 202, the method object Function in the class object can be restored to the Method object, which can ensure the accuracy and universality of the metadata information, so that the method name of various types of method objects, the class where the method belongs, and other related information can be obtained.
[0061] Specifically, for various method types, conversion can be performed in the following ways: when the method type is a class method, the first object in the method object is converted to the second object; when the method type is an instance method, after the method object is instantiated, the first object in the instantiated object is converted to the second object; the first object is a Function object and the second object is a Method object.
[0062] That is, the method for determining whether to convert a Function object to a Method object is as follows:
[0063] 1) When the method type is a static method, whether it is instantiated or not, the method object of the static method is Function, and there is no need to convert the Function to a Method object.
[0064] 2) When the method type is a class method, the method object of the class method is Method regardless of whether it is instantiated or not, so the Function object in the method object needs to be converted to a Method object.
[0065] Python provides a built-in library types.MethodType method, which accepts two parameters: Function object and object saved to __self__. The class method Method needs to pass in the class object as the metadata value of __self__, so the corresponding method object Method can be created by passing in the Function object and the class object.
[0066] 3) When the method type is an instance method, after the method object is instantiated, the Function object in the instantiated object is converted into a Method object.
[0067] Since the second parameter of the types.MethodType method needs to pass in an instance object, the object needs to be instantiated first. Specifically, an instance object can be created based on the class object's parameterless constructor, for example, through the pseudo code "class object.__new__(class object)".
[0068] After converting the method object Function in the class object to the Method object according to the above method, the compatibility of the metadata information of the method object is guaranteed, so that the class name can be parsed through the metadata information of the method object.
[0069] Specifically, according to the relationship between the method full name (__qname__) and the method name (__name__) in the metadata, the class name of the method can be obtained through qname. For example, if qname is Ap, the class name is A.
[0070] After parsing the class name, you can use Python's module reflection getattr to get the class object.
[0071] In addition, you can use the Python built-in library inspect.getmodule to get the module object module where the method is located based on the passed method object.
[0072] For methods in class objects, the method object, method type, parsed class name, and class object can be stored in a data container; for common methods, there is no need to parse the class name, and the method object, method type, and module object where the method is located can be stored in a data container. The data container can be, for example, MethodInfo.
[0073] Optionally, the metadata in the method object may be parsed according to the method type in the MethodInfo data container to obtain setting information corresponding to the method type.
[0074] For ordinary methods and methods in class objects, the method parameter list, method return value, and method name of the method object are obtained.
[0075] In an example, you can use the Python built-in library inspect.signature to build a method signature object to get the method parameter list and method return value.
[0076] The method signature object can be used to obtain the parameter list of the member variable parameters to return the method, and the obtained parameter list will automatically filter the self parameter in the instance method and the cls parameter in the class method. For example, if there is a method "def f(self,name,age)->str{}", the parameter list of the method signature object will return the list set of name and age ["name", "age"], and the method parameter list can be saved in the data container. The method signature object can be used to determine whether the method has a return value based on whether the member variable return_annotation is an empty object. For example, if there is a method "def f(self,name,age)->str{}", return_annotation is not empty, then there is a method return value; for example, if there is a method "def f(self,name,age){}", return_annotation is empty, then there is no method return value.
[0077] If the method signature object determines that there is no method return value, the method can be constructed by traversing the syntax tree to determine whether there is a method return value, that is, to determine whether there is a return keyword in the syntax tree. In the above two judgment methods, if it is determined that there is a method return value, the method name is used as the receiving variable name of the method return value and saved in the data container.
[0078] The method name of the method object can be obtained through the metadata information __name__ of the method object, and the method name is saved in the data container.
[0079] The following describes the setting information of the feature type obtained for a specific type of method object.
[0080] (a) Ordinary method
[0081] Parse the module where the method is located. In summary, the setting information corresponding to a common method may include: method name, method parameter list, method return value, and the module name of the module where the method object is located.
[0082] (b) Class methods
[0083] The class object where the method is located is parsed through the method object's metadata __self__, and the module name of the module where the class is located is parsed.
[0084] (c) Static methods
[0085] The method full name qname is parsed through the metadata information of the method object, the class object is obtained through module reflexivity, and the module name of the module where the class is located is parsed. In summary, the setting information corresponding to the class method or static method may include: method name, method parameter list, method return value, class name where the method is located, and module name of the module where the class is located.
[0086] (d) Instance methods
[0087] Get the instance object where the method is located through the metadata _self__ of the method object, and get the class object through the metadata __class__ of the instance object; directly get the Method object of the constructor based on the pseudo code "instance object.__init__", and get the parameter list of the constructor through the method signature object; parse the module name of the module where the class is located. In summary, the setting information corresponding to the instance method may include: method name, method parameter list, method return value, class name where the method is located, class constructor parameter list, and module name of the module where the class is located.
[0088] The setting information obtained in the above steps is stored in a data container, such as ClassDescriptor. For one or more method objects contained in the code to be processed, the corresponding setting information is obtained from the data container ClassDescriptor according to the method type of the method object to obtain the calling code.
[0089] When calling a function or method, the required calling code includes at least the following types of calling statements: i) package import statements; ii) instantiation statements required for instance methods; iii) method calling statements. The following describes a method for obtaining a calling statement by splicing the setting information of a method object according to the method type.
[0090] (a) Ordinary method
[0091] The module name and the method name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, and the method name is concatenated according to the grammatical format of the method call statement to obtain the method call statement. The pseudo code of the concatenated package import statement is, for example, "from module name import method name"; the pseudo code of the concatenated method call statement is, for example, "return value variable name = method name (parameter 1, parameter 2, ..., parameter n)", where parameter i (i = 1, 2, ..., n, n is a positive integer) is a parameter in the method parameter list.
[0092] (b) Instance methods
[0093] The module name and the class name are concatenated according to the syntax format of the package import statement to obtain the package import statement, the class name and the constructor parameter list are concatenated according to the syntax format of the instantiation statement to obtain the instantiation statement, and the instance variable name, method name, and method parameter list are concatenated according to the syntax format of the method call statement to obtain the method call statement. The pseudo code of the concatenated package import statement is, for example, "from module name import class name"; the pseudo code of the concatenated instantiation statement is, for example, "instance variable name = class name (constructor parameter 1, constructor parameter 2, ..., constructor parameter m)", where the constructor parameter i (i = 1, 2, ..., m, m is a positive integer) is a parameter in the constructor parameter list; the pseudo code of the concatenated method call is, for example, "return value variable name = instance variable name.method name (parameter 1, parameter 2, ..., parameter n)", where the parameter i (i = 1, 2, ..., n, n is a positive integer) is a parameter in the method parameter list.
[0094] (c) Static method or class method
[0095] The module name and class name are spliced according to the grammatical format of the package import statement to obtain the package import statement, and the class name, method name, and method parameter list are spliced according to the grammatical format of the method call statement to obtain the method call statement. The pseudocode of the spliced package import statement is, for example, "from module name import class name"; the pseudocode of the spliced method call statement is, for example, "return value variable name = class name.method name (parameter 1, parameter 2, ..., parameter n)", where parameter i (i = 1, 2, ..., n, n is a positive integer) is a parameter in the method parameter list. The call code generated by the call statement obtained by the above method can be stored as a code call file in the file path specified by the user. When the user needs to make an interface call, he only needs to find the corresponding call code in the code call file, copy it, and apply it directly, saving the time of writing basic call methods.
[0096] Figure 3 It is a flowchart of another object calling method proposed in this application.
[0097] like Figure 3As shown, the electronic device executing the object calling method proposed in the present application receives the file path passed in by the user, or receives the class object and / or module object passed in by the user, and obtains the code to be processed. By parsing the received data, all functions and methods in the class object defined in the code to be processed can be obtained; then the method type of each function and method is determined, and the corresponding setting information is obtained according to the different method types, and the setting information is the key information for generating the calling statement of the method object. Specifically, when it is determined that the method type is a common method, the method name, method parameter list, method return value, and module name of the module where the method is located are parsed; when it is determined that the method type is an instance method, the method name, method parameter list, method return value, class name where the method is located, class constructor parameter list, and module name of the module where the class is located are parsed; when it is determined that the method type is a class method or a static method, the method name, method parameter list, method return value, class name where the method is located, and module name of the module where the class is located are parsed. The setting information corresponding to the various method types obtained is saved in a data container. Afterwards, the corresponding setting information in the data container is retrieved according to the method type, the calling code is obtained by splicing, and the generated calling code is saved in the user's designated script file.
[0098] The above method can automatically generate the code required to call the functions and methods defined in the code to be processed, thereby improving the efficiency of writing calling codes and saving code development time.
[0099] Assume that there is a py file (module object) a.py, and a.py contains a class object A and a common method (function) f. The user needs to call all methods in the py file.
[0100] The object calling method proposed in this application is described below by taking the processing of the a,py file as an example.
[0101] First, receive the module object a passed in by the user;
[0102] Next, parse all metadata information in module object a. For example, the statement "inspect.getmembers" can be used to parse metadata information, obtain all common methods f and class object A in module object a, and further process class object A, parse all metadata information in class object A (the statement "inspect.getmembers" can also be used), filter out all method objects, and parse the method list Ap, Ac, Ad in class object A. The parsed methods do not include private methods and magic functions. The method list is obtained by summarizing the common function f obtained by parsing module object a and the methods in the class (i.e., methods in the class object) Ap, Ac, Ad obtained by parsing class object A, and the method list can be stored in the list container.
[0103] For each method in the methods list, determine the method type.
[0104] First, the method objects in the list container can be traversed to determine whether the method is a common method or a method in a class. Specifically, __qname__ (full method name) and __name__ (method name) in the method object metadata can be taken out to compare whether qname and name are equal. In the method list obtained above, the full method name and method name of Ap, Ac, and Ad are not equal, so they can be determined as methods in the class, while the full method name and method name of f are the same, so they can be determined as common methods. For methods in the class, the specific type of the method is further determined, that is, whether it is an instance method, a class method, or a static method. For example, the decorator on the method can be parsed by the abstract syntax tree to make the judgment. Specifically, the source code string saved in the method object can be obtained according to the python built-in library inspect.getsource, and the source code of the method can be parsed separately based on the python built-in library ast to generate an abstract syntax tree; and the abstract syntax tree generated by the method can be traversed to determine the type of method in the class through the decorator. For example, if Ap has no decorator, then Ap is judged to be an instance method, if Ac has the decorator @classmethod, then Ac is judged to be a class method, and if Ad has the decorator @staticmethod, then it is judged to be a static method.
[0105] For the Function objects Ap, Ac, Ad in the class, determine whether to restore them to Method objects A().p, Ac, Ad according to the specific method type. 1) If it is a static method, since the method objects of static methods are all Function, they are not restored, that is, Ad is returned directly without processing; 2) If it is a class method, since the method objects of class methods are all Method, it is necessary to convert the Function object Ac to the Method object Ac. The conversion code is, for example: types.MethodType(Ac,A);
[0106] 3) If it is an instance method, an instance object is created based on the class object's parameterless constructor. The pseudo code used is, for example, a=A.__new__(A). Then, the Function object like Ap is converted to a Method object A().p. The conversion code is, for example: types.MethodType(Ap,a).
[0107] After the above conversion, the class name is parsed for the method in the class. Specifically, through the relationship between qname and name in the method object, it can be known that the class name string where the Ap, Ac, and Ad methods are located is "A".
[0108] After parsing the class name "A", we can use Python's module reflection getattr to get the class object A.
[0109] The method objects in the class parsed through the above steps, as well as the method objects of common methods, can be saved together with the method type and the module where the method is located in a data container, such as MethodInfo. The specific saved results are shown in Table 2:
[0110] Table 2
[0111] Method Object Method Type Module where the method is located F Common method a A().d Instance Methods a Ac Class Methods a Ad Static methods a
[0112] Next, obtain the corresponding setting information according to the specific method type.
[0113] (1) Parsing method parameter list
[0114] According to the method object in MethodInfo, use the Python built-in library inspect.signature to obtain the method signature object. The method signature object can obtain the member variable parameters to return the parameter list of the method, and the obtained parameter list will automatically filter the self parameter in the instance method and the cls parameter in the class method.
[0115] For the method object obtained in the above steps, the method parameter list obtained by parsing is as follows:
[0116] Table 3
[0117] Method Object Method parameter list f ["a","b"] A().p ["a","b"] Ac ["name","age"] Ad ["map","list"]
[0118] (2) Parsing method return value
[0119] First, determine whether the method has a return value. Using the method signature object, you can determine whether the method has a return value by checking whether the member variable return_annotation is an empty object. For the method object obtained in the above steps, the judgment result is, for example: f has no return value, A().p has a return value, Ac has a return value, and Ad has no return value.
[0120] If it is determined that there is no return value, then whether there is a method return value is determined based on the abstract syntax tree, that is, whether there is a return keyword in the syntax tree. The result is, for example: f has no return value, and Ad has a return value.
[0121] For a method object Ac that has a method return value, the method name c is used as the name of the variable that receives the method return value.
[0122] (3) Parsing method name
[0123] The method name is obtained through the metadata information __name__ of the method object in the MethodInfo data container. For example, the method name of f is f, the method name of A().p is c, and the method name of Ad is d.
[0124] The following describes the setting information of the feature type obtained for a specific type of method object.
[0125] (a) Ordinary method f
[0126] Parse out the module a where the method is located.
[0127] The setting information corresponding to a common method may include: method name, method parameter list, method return value, and the module name of the module where the method object is located. The specific information parsed in this example is as follows:
[0128] Table 4
[0129]
[0130] (b) Class Method Ac
[0131] The class object A where the method is located is parsed through the method object's metadata __self__, and the module a where the class is located is parsed.
[0132] The setting information corresponding to the class method may include: method name, method parameter list, method return value, class name where the method is located, and module name of the module where the class is located. The specific information parsed in this example is as follows:
[0133] Table 5
[0134]
[0135]
[0136] (c) Static method Ad
[0137] The method full name qname is parsed through the metadata information of the method object, the class object A is obtained through module reflexivity, and the module a where the class is located is parsed.
[0138] The setting information corresponding to the static method may include: method name, method parameter list, method return value, class name where the method is located, and module name of the module where the class is located. The specific information parsed in this example is as follows:
[0139] Table 6
[0140]
[0141] d) Instance method A().p
[0142] Get the instance object where the method is located through the method object's metadata _self__, and get the class object A through the instance object's metadata __class__;
[0143] Directly obtain the Method object of the constructor based on the pseudocode "a.__init__", and obtain the parameter list of the constructor ["name", "age"] through the method signature object;
[0144] Parse the module a where the class is located.
[0145] The setting information corresponding to the instance method may include: method name, method parameter list, method return value, class name where the method is located, class constructor parameter list, and module name of the module where the class is located. The specific information parsed in this example is as follows:
[0146] Table 7
[0147]
[0148]
[0149] The setting information obtained in the above steps is stored in a data container, such as a ClassDescriptor. Afterwards, for each method object, the setting information in the data container is taken out according to its method type and spliced to obtain a calling code.
[0150] First, concatenate the import code. If f is a common method, then take out the module object a, and get the module name "a" through __name__, then take out the method name "f", and concatenate the import statement "from a import f"; for the method in the class, get the module name, then take out the class name, and concatenate the import statement pseudo code "from module name import class name", as follows: the import statement of A().p is: from a import A; the import statement of Ac is: from a import A; the import statement of Ad is: from a import A. For the instance method A().p, you need to take out the class name A and the constructor parameter list ["name", "age"] in the data container, and concatenate the instantiation statement: "instance=A(name, age)". Finally, concatenate the method call statement.
[0151] Specifically, the method name, method parameter list, and method return value in the data container are retrieved (if there is no return value, the "return value variable name" and "=" in the following pseudo code are set to an empty string). Different method call statements are generated according to different method types.
[0152] (a) Ordinary method f
[0153] Directly concatenate method call statements: "f(a,b)";
[0154] (b) Static method Ad,
[0155] Directly concatenate method statements: "d = Ad (map, list)";
[0156] (c) Class Method Ac
[0157] Direct concatenation method statement: "c = Ac (name, age)";
[0158] (d) Instance method A().p
[0159] Based on the "instance variable name instance", the method call statement is concatenated: "instance.p(a, b)". Finally, the calling code of the method object obtained according to the calling statement corresponding to each type is stored in the py file under the file path specified by the user. When the user needs to make an interface call, he only needs to find the corresponding calling code in the code calling file, copy it and apply it directly, which saves the time of writing basic calling methods.
[0160] Please refer to Figure 4 , Figure 4 This is a block diagram of an object calling device proposed in this application. The device includes:
[0161] A first acquisition unit 401 acquires a method object in the code to be processed and determines a method type of the method object;
[0162] The second acquisition unit 402 is used to acquire setting information corresponding to the method type, where the setting information is key information for generating a call statement of a method object;
[0163] The splicing unit 403 is used to splice the setting information according to a preset splicing rule to obtain a calling statement;
[0164] The generating unit 404 is used to generate the calling code of the method object according to the calling statement, and execute the calling code to realize the calling of the method object.
[0165] In some embodiments, the method object includes a function or a method, and the first acquisition unit, when used to acquire the method object in the code to be processed, is specifically used to:
[0166] If the code to be processed includes a module object, then according to the metadata information of the module object, the function and the first-class object in the module object are obtained, and according to the metadata information of the first-class object, the method in the first-class object is obtained;
[0167] If the code to be processed includes a second type of object, the method in the second type of object is obtained according to the metadata information of the second type of object.
[0168] In some embodiments, when the first acquisition unit is used to determine the method type of the method object, it is specifically used to:
[0169] Determine whether the method object is a method in the class object;
[0170] In the case where the method object is a method in a class object, determining that the method type of the method object is a first type, the first type includes at least one of the following: an instance method, a class method, and a static method;
[0171] When the method object is not a method in a class object, the method type is determined to be the second type.
[0172] In some embodiments, the apparatus further comprises a conversion unit, configured to:
[0173] In the case where the method type is a class method, convert the first object in the method object to the second object;
[0174] In the case where the method type is an instance method, after the method object is instantiated, the first object in the instantiated object is converted into the second object;
[0175] The first object is a Function object, and the second object is a Method object.
[0176] In some embodiments, the setting information includes a method parameter list, a method return value, and a method name;
[0177] In the case where the method type of the method object is the first type, if the method type is a class method or a static method, the setting information also includes the class name of the method object and the module name of the module where the class is located; if the method type is an instance method, the setting information also includes the class name of the method object, the parameter list of the class constructor, and the module name of the module where the class is located;
[0178] When the method type of the method object is the second type, the setting information further includes a module name of the module where the method object is located.
[0179] In some embodiments, when the method type is an instance method and the setting information includes the class name of the method object and a constructor parameter list of the class, the second acquisition unit, when used to acquire the setting information corresponding to the method type, is specifically used to:
[0180] Get the instance object where the method object is located through the first parameter of the method object, where the first parameter represents the method object itself;
[0181] Get the class name of the method object and the constructor parameter list of the class based on the metadata of the instance object.
[0182] In some embodiments, when the second acquisition unit is used to acquire the setting information corresponding to the method type, it is specifically used to:
[0183] In the case of determining whether the method return value of the method object is null through the method signature object, constructing an abstract syntax tree of the method object, and determining whether the method return value is null according to whether a return keyword exists in the abstract syntax tree;
[0184] When it is determined that the method return value is not empty, the method name of the method object is used as the receiving variable name of the method return value.
[0185] In some embodiments, the splicing unit is specifically used for:
[0186] When the method type of the method object is the second type,
[0187] The module name and the method name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, and the method name is concatenated according to the grammatical format of the method call statement to obtain the method call statement;
[0188] In the case where the method object is an instance method,
[0189] The module name and the class name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, the class name and the constructor parameter list are concatenated according to the grammatical format of the instantiation statement to obtain the instantiation statement, and the instance variable name, method name, and method parameter list are concatenated according to the grammatical format of the method call statement to obtain the method call statement;
[0190] When the method object is a static method or a class method, the module name and the class name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, and the class name, method name, and method parameter list are concatenated according to the grammatical format of the method call statement to obtain the method call statement.
[0191] The implementation process of the functions and effects of each module in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, which will not be repeated here.
[0192] For the device embodiment, it basically corresponds to the method embodiment, so the relevant parts refer to the partial description of the method embodiment. The device embodiment described above is only schematic, wherein the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this specification. Ordinary technicians in this field can understand and implement it without paying creative work.
[0193] Figure 5 This is a schematic diagram of a device provided by this application. Please refer to Figure 5At the hardware level, the device includes a processor 502, an internal bus 504, a network interface 506, a memory 508, and a non-volatile memory 510, and may also include other required hardware. One or more embodiments of this specification may be implemented based on software, such as the processor 502 reading the corresponding computer program from the non-volatile memory 510 into the memory 508 and then running it. Of course, in addition to the software implementation, one or more embodiments of this specification do not exclude other implementations, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic module, but may also be hardware or logic devices.
[0194] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, a game console, a tablet computer, a wearable device or a combination of any of these devices.
[0195] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0196] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0197] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information by any method or technology. Information can be computer-readable instructions, data structures, program modules 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 technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0198] The embodiments of the present application also provide a computer program product, which implements the method of any embodiment when running.
[0199] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0200] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0201] The terms used in one or more embodiments of this specification are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of this specification. The singular forms of "a", "said" and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.
[0202] It should be understood that although the terms first, second, third, etc. may be used to describe various information in one or more embodiments of this specification, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of this specification, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0203] The above description is merely a preferred embodiment of one or more embodiments of the present specification and is not intended to limit one or more embodiments of the present specification. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of the present specification shall be included in the scope of protection of one or more embodiments of the present specification.
Claims
1. An object calling method, characterized in that: include: Obtaining a method object in the code to be processed, and determining a method type of the method object; Acquire setting information corresponding to the method type, where the setting information is key information for generating a call statement for the method object; splicing the setting information according to a preset splicing rule to obtain the calling statement; The calling code of the method object is generated according to the calling statement, and the calling code is executed to call the method object.
2. The method according to claim 1, characterized in that The method object includes a function or a method; the obtaining the method object in the code to be processed includes: If the code to be processed includes a module object, then according to the metadata information of the module object, the function and the first class object in the module object are obtained, and according to the metadata information of the first class object, the method in the first class object is obtained; If the code to be processed includes a second type of object, methods in the second type of object are obtained according to metadata information of the second type of object.
3. The method according to claim 2, characterized in that The determining the method type of the method object comprises: Determine whether the method object is a method in a class object; In the case where the method object is a method in a class object, determining that the method type of the method object is a first type, the first type including at least one of the following: an instance method, a class method, and a static method; When the method object is not a method in a class object, the method type is determined to be a second type.
4. The method according to claim 3, characterized in that The setting information includes method parameter list, method return value, and method name; In the case where the method type of the method object is the first type, if the method type is a class method or a static method, the setting information further includes the class name of the method object and the module name of the module where the class is located; if the method type is an instance method, the setting information further includes the class name of the method object, a constructor parameter list of the class, and the module name of the module where the class is located; When the method type of the method object is the second type, the setting information further includes a module name of the module where the method object is located.
5. The method according to claim 4, characterized in that When the method type is an instance method and the setting information includes the class name of the method object and a constructor parameter list of the class, obtaining the setting information corresponding to the method type includes: Obtaining the instance object where the method object is located through the first parameter of the method object, where the first parameter represents the method object itself; The class name of the method object and the constructor parameter list of the class are obtained according to the metadata of the instance object.
6. The method according to claim 4, characterized in that The obtaining of setting information corresponding to the method type includes: In the case of determining whether the method return value of the method object is null through the method signature object, constructing an abstract syntax tree of the method object, and determining whether the method return value is null according to whether a return keyword exists in the abstract syntax tree; When it is determined that the method return value is not empty, the method name of the method object is used as the receiving variable name of the method return value.
7. The method according to claim 4, characterized in that The step of splicing the setting information according to a preset splicing rule to obtain the calling statement includes: When the method type of the method object is the second type, the module name and the method name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, and the method name is concatenated according to the grammatical format of the method call statement to obtain the method call statement; In the case where the method object is an instance method, the module name and the class name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, the class name and the constructor parameter list are concatenated according to the grammatical format of the instantiation statement to obtain the instantiation statement, and the instance variable name, method name, and method parameter list are concatenated according to the grammatical format of the method call statement to obtain the method call statement; When the method object is a static method or a class method, the module name and the class name are concatenated according to the grammatical format of the package import statement to obtain the package import statement, and the class name, method name, and method parameter list are concatenated according to the grammatical format of the method call statement to obtain the method call statement.
8. An object calling device, characterized in that: include: A first acquisition unit acquires a method object in the code to be processed and determines a method type of the method object; A second acquisition unit is used to acquire setting information corresponding to the method type, where the setting information is key information for generating a call statement of the method object; A splicing unit, used for splicing the setting information according to a preset splicing rule to obtain the calling statement; A generating unit is used to generate a calling code of the method object according to the calling statement, and execute the calling code to call the method object.
9. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; The processor implements the object calling method according to any one of claims 1 to 7 by running the executable instructions.
10. A computer-readable storage medium, characterized in that: Computer instructions are stored thereon, and when the instructions are executed by a processor, the object calling method according to any one of claims 1 to 7 is implemented.