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:
- obj = Objectloadfromfile ()
- 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
- Import JSON
- Import Inspect
- Import PDB
- def object2dict (obj):
- #convert object to a dict
- D = {' __class__ ': obj.__class__. __name__, ' __module__ ': obj.__module__}
- D.update(obj.__dict__)
- return D
- def objectdumps2file (obj, jsonfile):
- objdict = object2dict (obj)
- With Open(jsonfile, ' W ') as F:
- F.write(Json.dumps (objdict))
- def Dict2object (d):
- "Convert Dict to object, the dict would be changed"
- if' __class__ ' in D:
- Class_name = d.pop(' __class__ ')
- Module_name = d.pop(' __module__ ')
- module = __import__(module_name)
- #print ' The module is: ', module
- Class_ = getattr(module,class_name)
- args = dict((Key.encode (' ASCII '), value) for key, value in D.items()) #get args /c5>
- #print ' The Atrribute: ', repr (args)
- #pdb. Set_trace ()
- Inst = Class_ (**args) #create new instance
- Else:
- Inst = d
- return Inst
- def Objectloadfromfile (jsonfile):
- ' load JSON file and generate a new object instance whose __name__ filed
- would be ' inst '
- With Open(Jsonfile) as F:
- Objectdict =json.load (f)
- obj = Dict2object (objectdict)
- return obj
- #test function
- if __name__ = = '__main__':
- class Person (object):
- def __init__ (Self, Name,age, **args):
- Obj_list = Inspect. Stack () [1][-2]
- Self. __name__ = obj_list[0].split (' = ') [0].strip ()#object instance name
- self. Name = Name
- self. Age = Age
- def __repr__ (Self):
- return ' Person Object Name:%s, age:%d ' percent (self. Name,self. Age)
- def say (self):
- #d = Inspect.stack () [1][-2]
- #print d[0].split ('. ') [0].strip ()
- return Self. __name__
- def jsondumps (Self, filename=none):
- "' essential transformation to Python basic type in order to
- Store as JSON. Dumps as Objectname.json if filename missed "
- if not filename:
- Jsonfile = self. __name__+ '. JSON '
- Else: jsonfile = filename
- Objectdumps2file (Self, jsonfile)
- def Jsonloadtransfer (self):#TBD
- "' essential transformation to object required Type,such as
- NumPy Matrix.call This function after NewObject = Objectloadfromfile (jsonfile) "
- Pass
- p = person (' Aidan ', 22)
- #json. Dumps (p) #error 'll be throwed
- #objectDumps2File (P, ' Person.json ')
- P.jsondumps ()
- p_l = Objectloadfromfile (' P.json ')
- 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
- class Person ():
- class Person (object)
The difference is:
If you create a new person Instanc test, the output of type (test) is:
Source Code:Copy
- <type ' instance ' >
- <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