day27軟體開發的規範
阿新 • • 發佈:2019-01-03
========================================開發規範======================================= 1.軟體開發規範 1.1目錄結構----有組織的目錄結構 Project- bin start.py#啟動指令碼 conf setting.py#設定 db mysql.sql#資料庫 lib commons.py#公共的類庫 log log.py#日誌資訊 src/core #主要的核心的程式邏輯 ========================================call方法======================================= class Foo(): pass def __call__(self, *args, **kwargs): print('我執行啦obj()') f1=Foo() f1()#呼叫的就是__call__方法 ========================================doc屬性========================================= class Foo(): '我是描述資訊' pass class Bar(): pass print(Foo.__dict__) print(Bar.__dict__) ========================================反射的詳細介紹=================================== import test as obj print(obj) print(hasattr(obj,'say_hi')) print(hasattr(obj,'say_hissssssssssssssss')) if hasattr(obj,'say_hi'): func=getattr(obj,'say_hi') func() else: print('其他的函式') import sys print('來自自己的函式') b1=sys.modules[__name__] # import fangse as obj1 import sys obj1=sys.modules[__name__] print('你好',hasattr(obj1,'x')) ========================================file檔案======================================== import sys #print(sys.argv)#獲取平臺獲取當前的檔案路徑 sys.exit() sys.version sys.path sys.stdin sys.stdout sys.stderror ========================================getattribute==================================== class Foo(): def __init__(self,x): self.x=x def __getattr__(self, item): print('執行的是我') def __getattribute__(self, item): print('執行的是getattribute') raise AttributeError('丟擲異常啦') f1=Foo(10) f1.xxx#執行找不到的屬性訪問執行getattr方法 f1.x #呼叫首先執行—- __getattribute方法,不存在的情況下執行 ========================================getattribute補充================================ print('----------------------') print('----------------------') print('----------------------') # class Foo(): # def __init__(self,x): # self.x=x # # def __getattr__(self, item): # print('執行的是我') # def __getattribute__(self, item): # print('執行的是getattribute') # raise AttributeError('丟擲異常啦') # # f1=Foo(10) # f1.xxx#執行找不到的屬性訪問執行getattr方法 # f1.x # #呼叫首先執行—- __getattribute方法,不存在的情況下執行 raise AttributeError('自己找事') print('----------------------') print('----------------------') print('----------------------') ========================================index方法======================================= from lib.aa import C c1=C() print(c1.name) print(c1.__module__)#來之哪一個模組 print(c1.__class__)#來之哪一個類產生 ========================================item系列方法==================================== class Foo(): def __getitem__(self, item): print('getitem') def __setitem__(self, key, value): print('setitem') def __delitem__(self, key): print('delitem') f1=Foo() print(f1.__dict__) #通過點訪問的死 getattr #當呼叫字典的時候才呼叫item f1['name']='eoulen' f1['age']=18 print('---->>>',f1.__dict__) del f1['name'] print(f1.__dict__) ========================================slots方法======================================== #__slots__是什麼:是一個類變數,變數值可以使列表,元組, class Foo(): __slots__ = {'name','age'} f1=Foo() print(Foo.__slots__) print(f1.__slots__) #使用__slots主要是進行記憶體的減小 f1.name='oulen' f1.age=18 #f1.gender='male'/////AttributeError: 'Foo' object has no attribute 'gender' #噹噹前的類存在__slots__ , __dict__不會被使用 ========================================內建函式========================================= class Foo(): pass class Bar(Foo): pass f1=Foo() print(isinstance(f1,Foo))#檢查obj是否是CLS類的例項 print(issubclass(Foo,Bar))#檢查Foo是否是繼承BAR ========================================描述符=========================================== class Foo(): def __get__(self, instance, owner): print('get方法') def __set__(self, instance, value): print('set方法') instance.__dict__['x']=value def __delete__(self, instance): print('delete方法') f1=Foo() f1.name='oulen' #當前的例項方法不會觸發這個方法 #用來代理另外一個類的屬性的時候 class Bar(): x=Foo()#當另外一個類的屬性是另外一個類 #在何地 def __init__(self,n): self.x=n b1=Bar(11) print(b1.x) print(b1.__dict__) b1.x=18 #del b1.x print(b1.__dict__) #資料描述符號 #有__set__資料描述符 #沒有__set__就是非資料描述符 #描述符必須在新式類中 #注意事項: #1.類屬性 #2.資料描述符 #3.例項屬性 #4.非資料描述符 #5.找不到屬性觸發__getattr__() ========================================描述符優先順序====================================== class Foo: def __get__(self, instance, owner): print('===>get方法') def __set__(self, instance, value): print('===>set方法',instance,value) # instance.__dict__['x']=value #b1.__dict__ def __delete__(self, instance): print('===>delete方法') class Bar: x=Foo() #在何地? Bar.x=1 print(Bar.x) #----------------優先順序的定義 #注意事項: #1.類屬性 #2.資料描述符 #3.例項屬性 #4.非資料描述符 沒有__set__方法 #5.找不到屬性觸發__getattr__() b1=Bar()# b1.x #get b1.x=1 #set del b1.x #delete ========================================改變物件的字串顯示=============================== # l=list('hello') # print(1) # file=open('test.txt','w') # print(file) # # class Foo(): # def __init__(self,name,age): # self.name=name # self.age=age # def __str__(self): # return '名字是%s 年齡是%s'%(self.name,self.age) # # def __str__(self): # # return "自定製的顯示方式" # f1=Foo('engon',18) # print(f1) # print(f1.name)#--str __ f1.str # x=str(f1) # print(x) class Foo(): def __init__(self,name,age): self.name=name self.age=age # def __str__(self): # return '名字是%s 年齡是%s'%(self.name,self.age) # def __str__(self): # return "自定製的顯示方式" def __repr__(self): return '名字是%s 年齡是%s'%(self.name,self.age) f1=Foo() #repl 用於在直譯器使用 #str 使用的是print 使用 print -str _>> 沒有的情況下選擇到 __repl__的方法 # ''' str 函式或者是print 函式 ——》》 obj.__str__() repr 或是直譯器互動式——————》》obj.__repr__() 如果定義的__str__沒有被定義,那麼就使用__repr__來代替輸出 注意這兩者的方法的返回值必須是字串,否則丟擲異常 ''' ========================================析構方法========================================== class Foo(): def __init__(self,name): self.name=name def __del__(self): print('我執行啦') f1=Foo('oulen') del f1.name print('-----------》》》》》》') ========================================自定製格式化方法=================================== # x='{0}{0}{0}'.format('dog') # # print(x) # formate_dic={ 'ymd':'{0.year}:{0.mon}:{0.day}', 'm-d-y':'{0.year}:{0.mon}:{0.day}', 'y:m:d':'{0.mon}-{0.day}-{0.year}' } class Date: def __init__(self,year,mon,day): self.year=year self.mon=mon self.day=day def __format__(self, format_spec): print('我執行啦') print('我執',format_spec) if not format_spec or format_spec not in formate_dic: format_spec='ymd' fm=formate_dic[format_spec] return fm.format(self) #return '{0.year}:{0.mon}:{0.day}'.format(self) d1=Date(2016,12,26) format(d1) print(format(d1)) print(format(d1,'y:m:d')) print(format(d1,'m-d-y')) print(format(d1,'m-d')) # # x='{0.year}:{0.mon}:{0.day}'.format(d1) # print(x) # y='{0.year}:{0.mon}:{0.day}'.format(d1) # z='{0.mon}-{0.day}-{0.year}'.format(d1) # print(x) # print(y) # print(z) # ========================================迭代器協議========================================= class Foo(): def __init__(self,n): self.n=n def __iter__(self): print('你好') return self def __next__(self): if self.n==13: raise StopIteration('終止啦') self.n+=1 return self.n # l=list('hello') # for i in l: # print(i) # f1=Foo(10) print(f1.__next__()) print(f1.__next__()) print(f1.__next__()) print(f1.__next__()) print(f1.__next__()) print(f1.__next__()) print(f1.__next__()) # for i in f1:#iter(f1)---------f1.__iter() # print(i) ========================================迭代器實現斐波那契數列============================== class Fib(): def __init__(self): self._a=1 self._b=1 def __iter__(self): return self def __next__(self): if self._a > 5000: raise StopIteration('終止啦') self._a,self._b=self._b,self._a+self._b return self._a f1=Fib() print(next(f1)) print(next(f1)) print(next(f1)) print(next(f1)) print(next(f1)) print(next(f1)) for i in f1: print(i)