面向對象和pickle模塊結合

面向對象和pickle模塊相關
1.面向對象:
class 類名:
def init(self,參數1,參數2):
self.對象的屬性1 = 參數1
self.對象的屬性2 = 參數2
def 方法名(self):pass
def 方法名2(self):pass
對象名 = 類名(1,2) #對象就是實例,表明一個具體的東西python

2.pickle模塊:
只能是Python語言遵循的一種數據轉化格式,只能在python語言中使用。
支持Python全部的數據類型,包括實例化對象。pickle模塊是將Python全部的數據結構以及對象等轉化成bytes類型,而後還能夠反序列化還原回去。
用於網絡傳輸:dumps、loads
用於文件寫讀:dump、loadlinux

3.面向對象和pickle模塊結合:網絡

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']

相關文章
相關標籤/搜索