Object-oriented and binding pickle module

Object-oriented and related pickle module
1. Object-oriented:
class class name:
DEF the init (self, parameter 1, parameter 2):
. Self properties of an object parameter = 1 1
. Self properties of the object 2 parameter 2 =
DEF method name (self ): Pass
DEF method name 2 (self): pass
the object class name name = (1,2) # is the object instance represents a specific thing

2.pickle Module:
only is a data format conversion followed Python language, the language can only be used in the python.
Python support all data types, including instantiating objects. The pickle module is Python all data structures and the like and the object type is converted into bytes, then the reduction may also be deserialized back.
For network transmission: dumps, loads
for read-write file: dump, load

3. The object-oriented and binding pickle module:

        1.使用pickle存取自定义类的对象的方式
                    #前提:必须有类
                    #通过pickle 存储和读取对象
                    class Course:
                        def __init__(self,name,period,price):
                            self.name = name
                            self.period = period
                            self.price = price

                    python = Course('python','6 week',1800)
                    linux = Course('linux','5 week',1500)
                    go = Course('go','4 week',1200)

                    import  pickle
                    with open('pickle_file','ab') as f:
                        pickle.dump(python,f)
                        pickle.dump(linux,f)
                        pickle.dump(go,f)

                    with open('pickle_file','rb') as f:
                        while True:
                            try:
                                obj = pickle.load(f)
                                print(obj.name,obj.period,obj.price)
                            except EOFError:
                                break

             2.自定义Pickle,借助pickle模块来完成简化的dump和load

                    # pickle dump
                        # 打开文件
                        # 把数据dump到文件里
                    # pickle load
                        # 打开文件
                        # 读数据
                    # 对象 = Mypickle('文件路径')
                    # 对象.load()  能拿到这个文件中所有的对象
                    # 对象.dump(要写入文件的对象)

                ##1:
                    import pickle
                    class Mypickle:
                        def __init__(self,path):
                            self.file = path
                        def dump(self,obj):
                            with open(self.file, 'ab') as f:
                                pickle.dump(obj, f)
                        def load(self):
                            with open(self.file,'rb') as f:
                                while True:
                                    try:
                                        yield pickle.load(f)
                                    except EOFError:
                                        break

                    pic = Mypickle('pickle_file')
                    s1 = Stack() #利用上面的栈
                    s1.put('aaa')
                    s1.put(123)
                    s1.put(456)
                    pic.dump(s1)

                    s2 = Stack()
                    s2.put('bbb')
                    s2.put(888)
                    s2.put(999)
                    pic.dump(s2)

                    for i in pic.load():
                        print(i.l)

                    # ['aaa', 123, 456]
                    # ['bbb', 888, 999]
                    

                ##2: load(self)与上面略有不同
                    import pickle
                    class Mypickle:
                        def __init__(self,path):
                            self.file = path

                        def dump(self,obj):
                            with open(self.file, 'ab') as f:
                                pickle.dump(obj, f)

                        def load(self):
                            l = []
                            with open(self.file,'rb') as f:
                                while True:
                                    try:
                                        l.append(pickle.load(f))
                                    except EOFError:
                                        break
                            return l

                    pic = Mypickle('pickle_file')
                    pic.dump('aaa')
                    pic.dump('bbb')
                    ret = pic.load()
                    print(ret)
                    #['aaa', 'bbb']

Guess you like

Origin www.cnblogs.com/xiaomage666/p/10992753.html