A method for solving nested object dictionaryization and automatic refactoring of existing code

By implementing base classes and recursive algorithms in objects, existing code is automatically modified, solving the serialization and dictionaryization problems of complex nested objects. This enables rapid object expression and serialization, adapts to different naming styles, and improves development efficiency and compatibility.

CN115098071BActive Publication Date: 2025-11-18CHENGDU DBAPP SECURITY
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210724990.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-24
Publication Date
2025-11-18
Estimated Expiration
2042-06-24

AI Technical Summary

Technical Problem

Existing technologies cannot efficiently serialize complex nested objects, and dictionary processing for each object is time-consuming and labor-intensive. Furthermore, they cannot adapt to changes in different naming styles, leading to development difficulties and compatibility issues.

Method used

By implementing a base class that all objects inherit from, and using recursion and methods in the base class (to_json and to_dict) to automatically modify existing code, object expression and serialization are achieved. This includes reading parameters in the command-line module, adding base class files in the file addition module, modifying code in the file modification module, and retrieving attribute dictionaries in the attribute retrieval module.

Benefits of technology

It enables rapid dictionary generation and serialization of complex nested objects, reducing development workload and improving development efficiency. It adapts to different JSON naming styles, has the ability to automatically improve existing code, and has good compatibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115098071B_ABST
    Figure CN115098071B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of code data, and discloses a method for solving nested object dictionary and automatically modifying existing codes, which comprises the following steps: S1, a command line module reads and stores parameters input by a user, and obtains existing codes and related information; S2, a file adding module adds a base class file in a base class file module to root directory information of the existing codes, wherein the base class file module comprises a to_json method and a to_dict method; S3, a file modifying module transmits init code and serialization style to an attribute acquisition module; and S4, the attribute acquisition module returns an attribute dictionary to the file modifying module. By implementing a base class, all objects inherit the class, and expression and serialization of the objects are easily realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of code data technology. Specifically, it is a method for solving the dictionaryization of nested objects and automatically modifying existing code. By implementing a base class, all objects can inherit from the class, making it easy to express and serialize objects. Background Technology

[0002] An object is a data structure containing many attributes and methods, representing an abstract programming concept. Nested objects can express one-to-many relationships in relational databases. Object dictionaryging transforms objects into an ordered group of objects. Current code models cannot serialize complex nested objects, requiring separate dictionaryging for each object. Without dictionaryging, printing objects only outputs the memory addresses of nested objects, making serialization difficult. Performing dictionaryging for each object is time-consuming and labor-intensive. Furthermore, current technologies for class dictionaryging require implementing multiple methods like `_str_`, `_repr_`, and `_dict_`, and each nested class needs separate processing to convert objects to dictionaries. If a class's dictionary implementation is not standardized, dictionaryging may fail. Differences in developer skills can also lead to inconsistent naming styles, and the system cannot adapt dictionary keys to different naming conventions.

[0003] Therefore, by implementing a base class that all objects inherit from, this invention can easily achieve object expression and serialization. This invention provides an algorithm for nested object dictionaryization based on inheritance and recursion, and a method for automatically modifying existing code. Summary of the Invention

[0004] The purpose of this invention is to provide a method for solving the dictionaryization of nested objects and automatically modifying existing code. By implementing a base class, all objects can inherit from the class, making it easy to express and serialize objects.

[0005] This invention is achieved through the following technical solution: a method for solving the dictionaryization of nested objects and automatically transforming existing code, comprising the following steps:

[0006] Step S1. Read and store the parameters passed by the user in the command line module, and obtain the existing code and its related information;

[0007] Step S2. The file addition module adds the base class file from the base class file module to the root directory information of the existing code. The base class file module is configured to use methods including the to_json method and the to_dict method.

[0008] Step S3. The file modification module passes the init code and serialization style information to the attribute retrieval module;

[0009] Step S4. The attribute acquisition module returns the attribute dictionary of the nested object and the mapping of the attribute dictionary to the file modification module. The file modification module finds the code file corresponding to the serialization style information and writes the serialization style information into the code file to achieve automated transformation.

[0010] To better implement this invention, the parameters input by the user in step S1 further include:

[0011] The user-configured parameters include the root path of the existing code file to be modified, the dictionary key style parameters used for JSON conversion, the existing code file name parameters to be left unchanged, and the existing code file class name string parameters to be left unchanged.

[0012] To better realize the present invention, step S2 further includes:

[0013] The to_json method performs reverse parsing for complex nested objects;

[0014] The to_dict method parses nested object types based on recursion. It recursively calls the class's to_dict method until the obtained attribute type matches the type specified in the original type, and then returns.

[0015] To better realize the present invention, step S3 further includes:

[0016] The file modification module removes unnecessary file information from the existing code and related information obtained by the command-line module, obtains a file list, iterates through the file list, modifies the inheritance information of classes in the base class file, and obtains serialization style information by calling attributes, including attribute_types information and attribute_map information, and writes these two pieces of information to a file.

[0017] To better realize the present invention, step S4 further includes:

[0018] The attribute acquisition module obtains the class's init code, retrieves the class attributes defined after the corresponding code, and returns the attribute_types information and attribute_map information based on the dictionary key value style parameter;

[0019] The file modification module will find the code files corresponding to the attribute_types and attribute_map information, and write the attribute_types and attribute_map information into the code files to achieve automated modification.

[0020] Compared with the prior art, the present invention has the following advantages and beneficial effects:

[0021] (1) This invention provides an algorithm for converting complex nested objects into a visual dictionary, and also provides an algorithm for converting objects to different JSON naming styles, which can greatly reduce the amount of code development. At the same time, it provides fast methods for comparing and printing nested objects;

[0022] (2) This invention has the ability to automatically improve existing code, which can reduce the workload of code modification;

[0023] (3) The recursive logic algorithm of this invention has the ability to automatically improve existing project code;

[0024] (4) Based on the ideas of recursion and inheritance, this invention can be dictionary-based as long as the object inherits the BaseModel class when it is defined and the to_dict method is called. At the same time, it replaces the magic methods required by each class. User-defined classes do not need to implement any related methods to easily complete the dictionary-based process. In addition, we also provide the to_json method, which can reverse the parsing of complex nested objects and convert the key style according to the class attribute configuration. The dictionary obtained after conversion can be directly used for JSON serialization, which greatly accelerates the development efficiency. Moreover, the automation implemented by this solution improves existing projects and has almost no impact on the original framework code. It has good reliability and compatibility. Attached Figure Description

[0025] The present invention will be further described in conjunction with the following drawings and embodiments. All inventive concepts of the present invention should be considered as disclosed content and within the scope of protection of the present invention.

[0026] Figure 1 Partial flowchart a of a method for solving the dictionaryization of nested objects and automatically modifying existing code provided by the present invention.

[0027] Figure 2 Partial flowchart b of a method for solving the dictionaryization of nested objects and automatically modifying existing code provided by the present invention.

[0028] Figure 3 This is a schematic flowchart illustrating the overall process of a method for solving the dictionaryization of nested objects and automatically modifying existing code, provided by the present invention. Detailed Implementation

[0029] To more clearly illustrate the technical solutions of the embodiments of the present invention, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments, and therefore should not be regarded as a limitation on the scope of protection. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0030] In the description of this invention, it should be noted that: a Map is a data structure composed of key-value pairs; an object is a data structure containing many attributes and methods, representing an abstract programming concept; inheritance allows the use of all the functionalities of an existing class and extends these functionalities without rewriting the original class; JSON is a syntax for storing and exchanging text information. The programming technique of a program calling itself is called recursion.

[0031] Example 1:

[0032] like Figures 1-3 As shown, in order to solve the problems of representing, printing, comparing (different and the same), JSONifying, and dictionaryifying complex nested objects, this invention provides a method for solving the dictionaryification of nested objects and automatically modifying existing code. It mainly includes 5 modules: command line module, base class file, base class file addition module, file modification module, and attribute retrieval module. Each module is decoupled from each other, and different modules are connected by a main function entry point to complete the chained calls.

[0033] Current code models cannot serialize complex nested data, requiring separate dictionary representation for each object. Without implementing object representation methods, printing objects only outputs the memory addresses of nested objects, making serialization difficult due to the lack of dictionary representation. For example, printing object B might not print its nested object A. This invention, by inheriting from the BaseModel object, easily achieves map representation, solving this problem. When converting objects to JSON, for data without key conversion requirements, the `to_dict` method can be used for map representation followed by JSON conversion. For data requiring key conversion, such as when using a Python client to interface with a Java API (Python uses snake-like naming conventions while Java uses camelCase), this invention automatically collects class attributes and generates an `attribute_map` class attribute, then uses the `to_json` method implemented in this technology to complete the serialization after key conversion.

[0034] In this embodiment, the base class file in the base class file module is added to the root directory information of the existing code. This invention specifically places the base class file in the root directory to facilitate reference by other classes to be modified. The base class file module sets the methods to be used, including the to_json method and the to_dict method. Strictly speaking, the relevant methods are defined in the base class file, and the algorithm is also implemented through these two methods. The base class file contains the BaseModel class, which contains the to_dict method, which converts a class into a dictionary, and the key value of the dictionary is the name of the class attribute. The to_json method converts a class into a dictionary, and the key value of the dictionary is the mapping value of the class attribute.

[0035] Example 2:

[0036] This embodiment further optimizes upon embodiment 1. In this embodiment, the command-line module executes the command line, passes in specified parameters, stores the parameters passed in the command line, and obtains the directory location of the project folder passed in as a parameter. The project folder is the code file. The command-line module can execute the command `python code_optimization.py --project_path "project_path"`: This means specifying the project root path. `--key_style "key_style"`: This means specifying the JSON-style dictionary key value style. `--except_class "except_class"`: This means classes that should not be modified. `--except_file "except_file"`: This means files that should not be modified, and multiple classes and files that should not be modified can be passed in.

[0037] The input parameters required for the command-line module are:

[0038] project_path: Project path, i.e., the root path of the project that needs to be modified with one click;

[0039] key_style: The style of key values ​​when converting to a JSON dictionary;

[0040] except_class: The exception class that does not need to be modified, and supports passing in multiple parameters;

[0041] except_file: The file that does not need to be modified; multiple parameters can be passed in.

[0042] The command-line module stores the corresponding parameters into a global variable and returns them.

[0043] The other parts of this embodiment are the same as those in Embodiment 1, so they will not be described again.

[0044] Example 3:

[0045] This embodiment further optimizes upon embodiment 1 or 2 described above. In this embodiment, the base class file module copies the base class file to the project root directory. The base class file module contains the base class and the `to_json` method, which will be used by the base class file addition module. The base class file in the base class file module implements the `BaseModel` class. The `to_dict` method implemented in this class is used for converting objects to dictionaries; the global attributes `attribute_types` and `attribute_map` are used for key style correction during JSON conversion; `to_str` is used for printing; `__repr__` is used for detailed representation of object content; and `__eq__` and `__ne__` are used for comparing object similarities and differences, respectively. The file addition module adds the `base.py` file, which comes with this software, to the project root directory. The `to_json` method can perform reverse parsing of complex nested objects and convert the key style according to the class's attribute configuration. The resulting dictionary can be directly used for JSON serialization, greatly accelerating development efficiency.

[0046] The base class file module contains a base.py file, the main contents of which are:

[0047] Primitive types are defined, which are data types that already exist in Python and can be directly converted to dictionaries. Specifically, `six.text_type` is compatible with `str` in Python 3 and represents Unicode in Python 2; `six.integer_type` represents `int` in Python 3 and `int` and `long` in Python 2.

[0048] A BaseModel class is defined, which has global attributes of two classes and implements the __init__ / to_dict / to_str / __repr__ / __eq__ / __ne__ methods, among which the to_dict method is the core content;

[0049] The __init__ method is defined in advance to facilitate overriding by inherited classes;

[0050] The `to_dict` method uses recursion to parse nested object types. Its logic involves recursively calling the class's `to_dict` method until the retrieved attribute type matches the type specified in the original type, at which point it returns. Specifically:

[0051] Iterate through the keys of the attribute_types dictionary. Since the keys in this global attribute dictionary are the attributes of the class, retrieve the attribute values ​​using getatrr and then begin the judgment:

[0052] If the attribute value is of type list, then the elements in the list are recursively checked to see if they have the to_dict method. If they do, the to_dict method is recursively called to get the dictionary value of the class. If they do not have the dictionary value, it means that the element is a primitive type, so the element itself is returned directly.

[0053] If the attribute value is of type tuple, then recursively check the elements in the tuple to see if the element has the to_dict method. If it does, then recursively call the to_dict method to get the dictionary value of the class.

[0054] If the type of the attribute value has a to_dict method, then the to_dict method is called directly to get its dictionary value;

[0055] If the attribute value is a dictionary, then it is necessary to iterate through the keys in the dictionary and determine whether the value corresponding to the key has a to_dict method. If it does, then the to_dict method is recursively called to get the dictionary value of the class. If it does not, it means that it is a primitive type, then the key and value themselves are returned directly.

[0056] If the attribute value is a primitive type, then return the primitive type directly.

[0057] Finally, determine if the class itself is a subclass of `dict`. If it is a subclass, simply iterate through it and return the result.

[0058] The `to_str` method is used after calling the `to_dict` method. Formatted printing is then used to print the nested relationships of the data more logically.

[0059] The __repr__ method calls the to_str method, which is convenient for printing using the print and pprint methods.

[0060] The __eq__ method first checks whether the object being compared is an instance of the same class. If not, it returns false directly. If it is, it returns the result of comparing the dictionaries obtained by calling the to_dict method of the two object instances.

[0061] The __ne__ method first checks whether the object being compared is an instance of the same class. If not, it returns True directly. If it is, it returns the result of a non-equality comparison of the dictionaries obtained by calling the to_dict method of the two object instances.

[0062] The `to_json` method is used to convert instance objects to a specified naming style. For example, in Python development, underscore concatenation is commonly used, but if the target JSON uses camelCase, a mismatch will occur. To solve this problem, an `attribute_map` type is added to the class to handle the key-value type conversion issue. Its core logic remains recursive until all attributes are of primitive types before returning.

[0063] Check if the object is None; if it is, return None.

[0064] Determine if the object is a primitive type; if it is, return the object itself directly.

[0065] Determine if the object is a list; if so, iterate through the elements of the object and recursively call the to_json function on each element.

[0066] Determine if the object is a tuple; if so, iterate through the elements of the object and recursively call the to_json function on each element.

[0067] If the object itself is a dictionary, assign `obj` to `obj_dict` for easy iteration. If the object is not a dictionary, it means it is a custom class instance. Iterate through the object's `attribute_types` to retrieve its attributes, extract the key values ​​from `attribute_map` that need to be converted for each attribute name, and use the `getattr` function to retrieve the attribute values, thus forming a new dictionary `obj_dict`.

[0068] Finally, this new dictionary obj_dict may contain nested objects. Continue traversing the dictionary's keys and values, and recursively use the to_json function on the values ​​to achieve recursive calls, until all values ​​are None or primitive types.

[0069] Adding a module to the file mainly involves copying the base.py file to the project root directory.

[0070] In this embodiment, serialization style refers to a naming style. For example, camelCase means that the first letter of each word is capitalized - NewAttrValue, and snakeCase means that each word is lowercase and separated by underscores - new_attr_value.

[0071] As the name suggests, attribute_types refers to the type of an attribute, that is, the data type of the attribute. This defines which attributes a class has and the corresponding values ​​of the attributes.

[0072] The other parts of this embodiment are the same as those in Embodiment 1 or 2 above, so they will not be described again.

[0073] Example 4:

[0074] This embodiment is a further optimization based on any one of embodiments 1-3 above. In this embodiment, the file modification module includes a file traversal part:

[0075] Step a1. The file traversal part will traverse all .py files. If the file name is in the exception file name list or is an __init__.py file, it will not be stored in the list of file names to be modified. After the traversal is completed, it will return a list of the complete file paths. The init file is the identifier of the package in Python.

[0076] Step a2. After obtaining the file list, iterate through the file list, and pass a single file path to step a3;

[0077] Step a3. After obtaining the file path from step a2, add "from base import *" to the first line of the file. Traverse the file content. If a keyword defined in a class is found, modify the class inheritance relationship to add a class that inherits from BaseModel.

[0078] Step a4. After obtaining the file path from step a2, pass it to the attribute acquisition module to modify the global attribute dictionary of the test case class.

[0079] Step a5. Repeat steps a3 and a4 until all files in step a2 have been traversed.

[0080] In this embodiment, the file modification module takes the project root directory path as input. After obtaining the path, it iterates through all files under the root path, determining whether the file is an init file or not a .py file. If it meets the above conditions, it is not added to the file modification list. If it does not meet the conditions, it is added to the file to be modified list. The file list is then iterated through and processed to determine whether it matches the string "do not modify file". If it matches, no further content iteration is performed. If no match is found, the content is traversed; after opening the file, add `from base import *` to the first line to import the base class file for later reference; after opening, read all the line ending list files, check if each line contains a class definition, and if so, match the class definition and add its inheritance relationship, inheriting the `BaseModel` class; repeat reading all the line ending list files until all classes in the file have completed the inheritance relationship; load the files in the file list to be modified as members, and traverse the members and member names; pass the file paths of the file list to be modified to the attribute retrieval module to obtain the generated `attribute_types` and `attribute_map` dictionaries; rematch the file content, and if the currently traversed class is found, add the two obtained global attributes `attribute_types` and `attribute_map` dictionaries under it to complete the content modification; repeat matching and traversal until the file members are traversed; repeat traversal until the file traversal and modification are complete.

[0081] The other parts of this embodiment are the same as any one of the embodiments 1-3 above, so they will not be described again.

[0082] Example 5:

[0083] This embodiment is a further optimization based on any one of embodiments 1-4 above. In this embodiment, the attribute acquisition module performs the following operations:

[0084] Step b1. After obtaining the file path, load the source code as a module list, and iterate through the modules and module names in the module list.

[0085] Step b2. Iterate through the member names and member classes of members of type class in the module;

[0086] Step b3. Obtain the source code of the class's __init__ method, find the attributes defined in __init__, and return them to the attribute acquisition module. Obtain the attribute dictionary attribute_types and pass it to step b5.

[0087] Step b4. Step b5 generates an attribute_map based on the key naming rules obtained from the command-line module and returns it to step b4;

[0088] Step b5. Traverse the file content, find the name of the module class, and write the content generated in steps b4 and b5 into the file;

[0089] Step b6. Repeat steps b3-b5 until the class members in step b2 have been traversed.

[0090] In this embodiment, after the attribute generation module obtains a member of type `class`, it retrieves the code of its `__init__` method, matches the attributes defined therein, generates the corresponding `attribute_types` dictionary, and returns it. Based on the generated `attribute_types` and the `key_style` parameter saved by the command-line module, it modifies the key value of the parameter and returns `attribute_map` after modification. In this embodiment, returning the attribute dictionary of nested objects and the mapping of the attribute dictionary is equivalent to returning `attribute_types` and `attribute_map`. After returning to the file modification module, these two dictionaries are returned. The file modification module will find the code corresponding to this class and write `attribute_types` and `attribute_map` into the code file to achieve automated modification.

[0091] For example:

[0092] The original code is:

[0093] class Student(object):

[0094] def __init__(self, name, age):

[0095] self.name = name

[0096] self.age = age

[0097] The modified code is:

[0098] class Student(object, BaseModel):

[0099] attribute_types = {

[0100] "name": 'str',

[0101] "age": 'int'

[0102] }

[0103] attribute_map = {

[0104] 'name': 'Name',

[0105] 'age': 'Age'

[0106] }

[0107] def __init__(self, name, age):

[0108] self.name = name

[0109] self.age = age

[0110] After the modification, the Student class inherits from BaseModel and has the to_dict method, as well as the attribute_types and attribute_map mappings, which enable dictionary-based representation.

[0111] The other parts of this embodiment are the same as any one of the embodiments 1-4 above, so they will not be described again.

[0112] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention in any way. Any simple modifications or equivalent changes made to the above embodiments based on the technical essence of the present invention shall fall within the protection scope of the present invention.

Claims

1. A method for solving the problem of dictionaryging nested objects and automatically transforming existing code, characterized in that, Includes the following steps: Step S1. Read and store the parameters passed by the user in the command line module, and obtain the existing code and its related information; Step S2. The file addition module adds the base class file from the base class file module to the root directory information of the existing code. The base class file module is configured to use methods including the to_json method and the to_dict method. Step S3. The file modification module passes the init code and serialization style information to the attribute retrieval module; Step S4. The attribute acquisition module returns the attribute dictionary of the nested object and the mapping of the attribute dictionary to the file modification module. The file modification module finds the code file corresponding to the serialization style information and writes the serialization style information into the code file to achieve automated transformation. The parameters input by the user in step S1 include: The user-configured parameters include the root path of the existing code file that needs to be modified, the dictionary key style parameter used for JSON conversion, the existing code file name parameter that should not be modified, and the existing code file class name string parameter that should not be modified. Step S4 includes: The attribute acquisition module obtains the class's init code, retrieves the class attributes defined after the corresponding code, and returns the attribute_types information and attribute_map information based on the dictionary key value style parameter; The file modification module will find the code files corresponding to the attribute_types and attribute_map information, and write the attribute_types and attribute_map information into the code files to achieve automated modification.

2. The method for solving the problem of dictionaryization of nested objects and automatically transforming existing code according to claim 1, characterized in that, Step S2 includes: The to_json method performs reverse parsing for complex nested objects; The to_dict method parses nested object types based on recursion. It recursively calls the class's to_dict method until the obtained attribute type matches the type specified in the original type, and then returns.

3. The method for solving the problem of dictionaryization of nested objects and automatically transforming existing code according to claim 1, characterized in that, Step S3 includes: The file modification module removes unnecessary file information from the existing code and related information obtained by the command-line module, obtains a file list, iterates through the file list, modifies the inheritance information of classes in the base class file, and obtains serialization style information by calling attributes, including attribute_types information and attribute_map information, and writes these two pieces of information to a file.

Citation Information

Patent Citations

  • Configurable method for automatically adding code annotations

    CN112965748A