Python implements objects that support JSON storage and parsing

Source: Internet
Author: User

We know that the JSON module makes it easy to store data for Python primitives (dict, lists, and so on) permanently into files, as well as to implement custom class storage through custom conversion functions and inherited JSON encode&decode methods. This article is based on the previous "Python JSON module", the implementation of Python support JSON storage objects.

It is significant that an object can take JSON storage and parsing. For example, the training process of all classification algorithms in machine learning has a lot of data calculation, if each start classification needs retraining classification algorithm waste resources and inefficient, if the training generated by the classification algorithm object can be saved, then unless the algorithm tuning, then only need to load. On the other hand, the object can be JSON parsing and storage can also be transmitted on the network, which in the current cloud computing, distributed data processing has an extraordinary significance.

To achieve self-storage and resolution, key operations for defining objects are:

0, copy the object_json.py to the package, define the object's module import Object_json:import Object_json.

The 1,__init__ () function supports a variable number of function calls, which are written as __init__ (self, ..., **args). This defines an object so that it can have properties other than those that need to be initialized during the construction phase.

2, for properties that must be initialized during the object construction phase, the parameters in the __init__ () function must be exactly the same as these property names so that the object can be constructed through the dictionary ' key ': value.

3, define a property ' __name__ '--the name of the object instance, implemented using the inspect module. The ' __name__ ' property is primarily used to produce the default file name when the object is stored.

4, define the Jsondumps () and Jsonloadtransfer () methods, complete the object JSON file load and new object creation through Objectloadfromfile ().

(i) Jsondumps () is used to convert an object to dict and to store the object as a JSON file through Json.dumps (), and to Instancename.json the default storage file if the user does not specify a file name. Since JSON only supports Python primitives, if there are some other types in the object (such as the NumPy matrix), you need to convert it to a Python base type (such as matrix.tolist () to transform the matrix into a list).

(ii) Jsonloadtransfer () is used to complete the conversion of the data format, converting some object properties from the basic type to the desired type (such as the mat (list) to convert the type from list to matrix), which can be omitted if the object has only a python base type. The process of creating a complete, usable object is:

    1. obj = Objectloadfromfile ()
    2. Obj.jsonloadtransfer ()

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

Source Code: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 would 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 /c5>
  22. #print ' The Atrribute: ', 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. would be ' inst '
  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 ' percent (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 to
  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 as
  58. NumPy Matrix.call This function after NewObject = Objectloadfromfile (jsonfile) "
  59. Pass
  60. p = person (' Aidan ', 22)
  61. #json. Dumps (p) #error 'll 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))

The Python class has both old and new, and Py 2.2 class definition inherits object to make the Class A new style class, without inheriting object as the classic classic Class (which will eventually inherit object).

There are two ways to do this in the class definition:

Source Code:Copy
    1. class Person ():
    2. class Person (object)

The difference is:

If you create a new person Instanc test, the output of type (test) is:

Source Code: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 Getmembers,ismodule,getcallargs,isclass, etc., for more details see http://docs.python.org/library/inspect.html. Refer to 'Python inspect module parsing '

Python implements objects that support JSON storage and parsing

Related Article

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.