Python implements objects that support JSON storage and parsing, and pythonjson

Source: Internet
Author: User

Python implements objects that support JSON storage and parsing, and pythonjson

We know that the JSON module can be used to conveniently store basic Python data (such as dict and list) as files permanently, at the same time, you can use custom conversion functions and methods that inherit JSON encode & decode to store custom classes. This article is based on the previous article "Python JSON module" to implement python objects that support JSON storage.

It is of great significance that objects can be stored and parsed in JSON format. For example, a large amount of data computing exists in the training process of all classification algorithms in machine learning. If you need to re-train the classification algorithm every time you start classification, this is a waste of resources and inefficient, if you can save the classification algorithm objects generated by training, you only need to load them later unless you need algorithm optimization. On the other hand, objects can be parsed and stored in JSON format so that they can be transmitted over the network. This is of extraordinary significance in cloud computing and distributed data processing.

To achieve custom storage and resolution, key operations for defining objects include:

0. copy object_json.py to the package and import the module of the defined object to object_json: import object_json.

1 ,__ init _ () The function must support variable number of function calls, that is, it must be written as _ init _ (self,..., ** args ). So that the defined object can have attributes other than those that need to be initialized during the construction phase.

2. For attributes that must be initialized in the object construction phase, the form parameters in the __init _ () function must be identical with these attribute names so that the dictionary 'key' can be used ': value Pair.

3. Define an attribute '_ name _' -- name of the object instance, which is implemented using the inspect module. The '_ name _' attribute is mainly used to generate the default file name for Object Storage.

4. Define the jsonDumps () and jsonLoadTransfer () methods and use objectLoadFromFile () to load the object JSON file and create the new object.

(I) jsonDumps () is used to convert an object to dict and use json. dumps () Stores objects as json files. If you do not specify a file name, the object is stored as instancename. json is the default storage file. JSON only supports python basic types. Therefore, if the object contains some other types (such as numpy matrix), you need to convert it to Python basic types (such as matrix. tolist () converts a matrix to a list ).

(Ii) jsonLoadTransfer () is used to convert the data format and convert some object attributes from the basic type to the desired type (such as mat (list) to convert the type from list to matrix ), this method can be omitted if the object only has the Python basic type. Complete and available object creation process:

  1. Obj = objectLoadFromFile ()
  2. Obj. jsonLoadTransfer ()

The following code is the source code of the object_json module that supports JSON storage and parsing of custom objects.

Source Code: Invalid Copy
  1. Import json
  2. Import inspect
  3. Import pdb
  4. Def object2dict (obj ):
  5. # Convert object to a dict
  6. D = {'_ class _': obj. _ class _. _ name __, '_ module _': obj. _ module __}
  7. D. update (obj. _ dict __)
  8. Return d
  9. Def objectDumps2File (obj, jsonfile ):
  10. ObjDict = object2dict (obj)
  11. With open (jsonfile, 'w') as f:
  12. F. write (json. dumps (objDict ))
  13. Def dict2object (d ):
  14. '''Convert dict to object, the dict will be changed '''
  15. If '_ class _' in d:
  16. Class_name = d. pop ('_ class __')
  17. Module_name = d. pop ('_ module __')
  18. Module = _ import _ (module_name)
  19. # Print 'the module is: ', module
  20. Class _ = getattr (module, class_name)
  21. Args = dict (key. encode ('ascii '), value) for key, value in d. items () # get args
  22. # Print 'the atrriyun: ', repr (args)
  23. # Pdb. set_trace ()
  24. Inst = class _ (** args) # create new instance
  25. Else:
  26. Inst = d
  27. Return inst
  28. Def objectLoadFromFile (jsonFile ):
  29. '''Load json file and generate a new object instance whose _ name _ filed
  30. Will be 'instance ''''
  31. With open (jsonFile) as f:
  32. ObjectDict = json. load (f)
  33. Obj = dict2object (objectDict)
  34. Return obj
  35. # Test function
  36. If _ name _ = '_ main __':
  37. Class Person (object ):
  38. Def _ init _ (self, name, age, ** args ):
  39. Obj_list = inspect. stack () [1] [-2]
  40. Self. _ name _ = obj_list [0]. split ('=') [0]. strip () # object instance name
  41. Self. name = name
  42. Self. age = age
  43. Def _ repr _ (self ):
  44. Return 'person Object name: % s, age: % d' % (self. name, self. age)
  45. Def say (self ):
  46. # D = inspect. stack () [1] [-2]
  47. # Print d [0]. split ('.') [0]. strip ()
  48. Return self. _ name __
  49. Def jsonDumps (self, filename = None ):
  50. '''Essential transformation to Python basic type in order
  51. Store as json. dumps as objectname. json if filename missed '''
  52. If not filename:
  53. Jsonfile = self. _ name _ + '. json'
  54. Else: jsonfile = filename
  55. ObjectDumps2File (self, jsonfile)
  56. Def jsonLoadTransfer (self): # TBD
  57. '''Essential transformation to object required type, such
  58. Numpy matrix. call this function after newobject = objectLoadFromFile (jsonfile )'''
  59. Pass
  60. P = Person ('aida', 22)
  61. # Json. dumps (p) # error will be throwed
  62. # ObjectDumps2File (p, 'person. json ')
  63. P. jsonDumps ()
  64. P_l = objectLoadFromFile ('p. json ')
  65. Print 'the decoded obj type: % s, obj: % s' % (type (p_l), repr (p_l ))

There are two types of Python classes: new and Old. After py 2.2, the class definition inherits the object to make this class A new style class, if the object is not inherited, It is a traditional classic class (and the object will be inherited ).

The following two methods are used in class definition:

Source Code: Invalid Copy
  1. Class Person ():
  2. Class Person (object)

The difference is:

If a new Person instanc test is created, the output of type (test) is as follows:

Source Code: Invalid Copy
  1. <Type 'instance'>
  2. <Class '_ main _. Person'>

The inspect module provides a series of introspection functions that can obtain information about modules, classes, methods, functions, traceback, Frame Objects, and code objects. Common methods include getmembers, ismodule, getcallargs, and isclass. For more information, see http://docs.python.org/library/inspect.html. Refer'Analysis of python inspect Module'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.