程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Xiaobai learns python series ——【Day48】Singleton and pickle module of design pattern

編輯:Python

今日內容概要

  • 設計模式
  • pickle模塊
  • 選課系統項目分析
  • Course selection system catalog construction

設計模式

1.How to understand design patterns
在ITThere are many predecessors in the industry who have designed fixed solutions to fixed problems.
設計模式總共有23種.
設計模式的分類:創建型;結構型;行為型.

2.設計模式之單例模式
Multiple parenthesized invocations of a class are allowed to produce only one object.
(1)引出:正常情況下,The class name only needs to be instantiated with parentheses to produce an object,Executing it a few times yields several different objects.

class MyClass:
pass
obj1 = MyClass()
obj2 = MyClass()
obj3 = MyClass()
print(id(obj1),id(obj2),id(obj3))
''' 2962171860192 2962171860752 2962171860640 '''

(2)If there are too many places,Then a certain amount of memory space will be wasted,So lead to singleton.
(3)Singleton methods use metaclasses to intervene in the creation of objects

class MyMeTaClass(type):
# Records whether the class has created objects
instance = None
def __call__(self, *args, **kwargs):
if self.instance:
return self.instance
# 獲取空對象
obj = super().__call__(*args, **kwargs)
# 保存對象
self.instance = obj
# 返回空對象
return obj
class Single(metaclass=MyMeTaClass):
def __init__(self, name):
self.name = name
obj1 = Single('jason')
obj2 = Single('kevin')
obj3 = Single('tony')
print(id(obj1), id(obj2), id(obj3))
''' 2578464315768 2578464315768 2578464315768 '''
print(obj1.name) # jason
print(obj2.name) # jason
print(obj3.name) # jason 

(4)A singleton method defines a decorator implementation

import settings
def singleton(cls): #cls=Mysql
_instance=cls(settings.HOST,settings.PORT)
def wrapper(*args,**kwargs):
if args or kwargs:
obj=cls(*args,**kwargs)
return obj
return _instance
return wrapper
@singleton # Mysql=singleton(Mysql)
class Mysql:
def __init__(self,host,port):
self.host=host
self.port=port
obj1=Mysql()
obj2=Mysql()
obj3=Mysql()
print(obj1 is obj2 is obj3) #True
obj4=Mysql('1.1.1.3',3307)
obj5=Mysql('1.1.1.4',3308)
print(obj3 is obj4) #False

pickle模塊

1.什麼是pickle模塊
pickle模塊使用的數據格式是python專用的,能夠把Python對象直接保存到文件,而不須要把他們轉化為字符串,也不用底層的文件訪問操作把它們寫入到一個二進制文件中.

2.pickle模塊中常用函數
(1)pickle.dump(obj, file, [,protocol])
功能:接受一個文件句柄和一個數據對象作為參數,把數據對象obj以特定的格式保存到給定的文件file裡.

import pickle
l1=[1,2,3,4,5]
t1=(1,2,3,4,5)
dic1={
"k1":"v1","k2":"v2","k3":"v3"}
#把列表l1Serialize into a filef1中
with open("f1","wb") as f:
pickle.dump(l1,f)
pickle.dump(t1,f)
pickle.dump(dic1,f)
Generate one in the current directory after serializationf1文件,打開文件f1A bunch of gibberish can be seen,如下所示:
€]q (KKKKKe.€(KKKKKtq .€}q (X k1qX v1qX k2qX v2qX k3qX v3qu.

(2)pickle.load(file)
功能:將file中的對象序列化讀出.

import pickle
#序列化到文件
obj = 123, "abcdedf", ["ac", 123], {
"key": "value", "key1": "value1"}
print(obj)
#wb Read and write to binary files
f = open("./a.txt",'wb')
pickle.dump(obj,f)
f.close()
f = open("./a.txt",'rb')
print(pickle.load(f))
f.close()

(3)pickle.dumps(obj[, protocol])
功能:將obj對象序列化為string形式,而不是存入文件中.

import pickle
ls = ['12', '34', '56']
# dumps 將數據通過特殊的形式轉換為只有python語言認識的字符串
str = pickle.dumps(ls)
print(str)

(4)pickle.loads(string)
功能:從string中讀出序列化前的obj對象.

import pickle
# loads 將pickle數據轉換為python的數據結構
ls = ['12', '34', '56']
str = pickle.dumps(ls)
mes = pickle.loads(str)
print(mes)
['aa', 'bb', 'cc']

選課系統項目分析

功能分析
(1)管理員功能(最核心)
1.注冊
2.登錄
3.創建講師
4.創建學校
5.創建課程
(2)講師功能
1.注冊
2.登錄
3.選擇教授課程
4.查看教授課程
5.Manage course grades
(3)學生功能
1.注冊
2.登錄
3.選擇學校
4.選擇課程
5.查看分數

Course selection system catalog construction

三層架構
(1)function display layer
src.py
admin_view.py
teacher_view.py
student_view.py
(2)核心邏輯層
admin_interface.py
teacher_interface.py
student_interface.py
(3)數據處理層
db_hanlder.py
model.py


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved