1 xml模塊python
import xml.etree.ElementTree as ET tree=ET.parse('a.xml') root=tree.getroot() # for child in root: # print('====>',child.tag) # for i in child: # print(i.tag,i.attrib,i.text) #查找element元素的三種方式 # years=root.iter('year') #掃描整個xml文檔樹,找到全部 # for i in years: # print(i) # res1=root.find('country') #誰來調,就從誰下一層開始找,只找一個 # print(res1) # # res2=root.findall('country') #誰來調,就從誰下一層開始找,只找一個 # print(res2) #修改 # years=root.iter('year') #掃描整個xml文檔樹,找到全部 # for year in years: # year.text=str(int(year.text)+1) # year.set('updated','yes') # year.set('version','1.0') # tree.write('a.xml') #刪除 # for county in root.iter('country'): # # print(county.tag) # rank=county.find('rank') # if int(rank.text) > 10: # county.remove(rank) # tree.write('a.xml') #增長節點 # for county in root.iter('country'): # e=ET.Element('egon') # e.text='hello' # e.attrib={'age':'18'} # county.append(e) # # tree.write('a.xml')
2 configparser模塊linux
import configparser config=configparser.ConfigParser() config.read('a.ini') #取配置 # print(config.sections()) #看標題 # print(config.options(config.sections()[0])) #查看某個標題下的配置項 # res=config.get('alex','name')#查看某個標題下的某個配置項的值 # print(type(res)) # res1=config.getint('egon','age')#查看某個標題下的某個配置項的值 # print(type(res1)) # # res1=config.getboolean('egon','is_admin')#查看某個標題下的某個配置項的值 # print(type(res1)) # # res1=config.getfloat('egon','salary')#查看某個標題下的某個配置項的值 # print(type(res1)) #修改 # config.remove_section('alex') # config.remove_option('egon','age') config.add_section('alex') config.set('alex','name','SB') config.write(open('a.ini','w'))
3 hashlib模塊git
import hashlib # # m=hashlib.md5() # m.update('hello'.encode('utf-8')) # m.update('world'.encode('utf-8')) # print(m.hexdigest()) # # # m=hashlib.md5() # m.update('helloworld'.encode('utf-8')) # print(m.hexdigest()) # # m=hashlib.md5('helloworld'.encode('utf-8')) # print(m.hexdigest()) # # # m=hashlib.md5('h'.encode('utf-8')) # m.update('elloworld'.encode('utf-8')) # print(m.hexdigest()) # m=hashlib.md5() # with open('a.xml','rb') as f: # for line in f: # m.update(line) # print(m.hexdigest()) # # # #耗費內存不推薦使用 # m=hashlib.md5() # with open('a.xml','rb') as f: # m.update(f.read()) # print(m.hexdigest()) #加鹽 # password='alex3714' # m=hashlib.md5('yihangbailushangqingtian'.encode('utf-8')) # m.update(password.encode('utf-8')) # # passwd_md5=m.hexdigest() # # print(passwd_md5) import hmac h=hmac.new('hello'.encode('utf-8')) h.update('world'.encode('utf-8')) print(h.hexdigest()) h=hmac.new('hello'.encode('utf-8')) h.update('w'.encode('utf-8')) h.update('or'.encode('utf-8')) h.update('ld'.encode('utf-8')) print(h.hexdigest())
4 subprocess模塊shell
import subprocess # res=subprocess.Popen(r'deeddddir D:\04-視頻錄製存放目錄\python18期\day7\xml模塊', # shell=True, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE) # # print('=================>',res) # # print('-========>',res.stdout.read()) # print('-========>',res.stderr.read().decode('gbk')) # print('-========>',res.stderr.read().decode('gbk')) # print('-========>',res.stderr.read().decode('gbk')) # print('-========>',res.stderr.read().decode('gbk')) #dir file_path | findstr xml$ res1=subprocess.Popen(r'dir D:\04-視頻錄製存放目錄\python18期\day7\xml模塊', shell=True, stdout=subprocess.PIPE,) # stdin=res1.stout res2=subprocess.Popen(r'findstr xml$', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE,) print(res2.stdout.read().decode('gbk'))
5 面向對象介紹與類 6 對象app
''' 面向過程:核心是過程二字,過程指的是問題的解決步驟,即先幹什麼再幹什麼,基於 面向過程去設計程序就比如在設計一條流水線,是一種機械式的思惟方式 優勢:複雜的問題流程化,進而簡單化 缺點:可擴展性差 應用:腳本程序,好比linux系統管理腳本,著名案例:linux內核,httpd,git 面向對象:核心是對象二字,對象就是特徵與技能的結合體,若是把設計程序比喻成 創造一個世界,那你就是這個世界的上帝,與面向過程對機械流水的模擬形式鮮明的對比 面向對象更加註重的對現實時間的模擬。 優勢:可擴展性 缺點: 類即種類,類別,對象是特徵和與技能的結合體,那麼類就是一系列對象類似的 特徵與技能的結合體 在現實世界中:先有一個個具體存在的對象----》(總結類似之處)---》現實中的類 在程序中:必定是先定義類,後調用類來產生對象 #第一階段:現實中的對象----》現實中類 obj1: 特徵 學校=oldboy 名字=李大炮 年齡=18 性別=女 技能 學習 吃飯 obj2: 特徵 學校=oldboy 名字=張全蛋 年齡=28 性別=男 技能 學習 吃飯 obj3: 特徵 學校=oldboy 名字=牛榴彈 年齡=18 性別=女 技能 學習 吃飯 現實中的老男孩學生類: 類似的特徵 學校=oldboy 類似的技能 學習 吃飯 #第二階段:程序中的類----》程序中的對象 ''' # class OldboyStudent: # school = 'oldboy' #類的數據屬性 # def learn(self): #類的函數屬性 # print('is learning') # # def eat(self): # print('is eating') # print('======>') #類體的代碼在類定義階段就會執行,理所應當會產生類的名稱空間,用__dict__屬性查看 # print(OldboyStudent.__dict__) # print(OldboyStudent.__dict__['school']) # print(OldboyStudent.__dict__['learn']) #類的屬性操做 # print(OldboyStudent.school) # print(OldboyStudent.learn) # # def func():pass # print(func) # OldboyStudent.learn(123) # OldboyStudent.x=1111111111111111111111 # OldboyStudent.school='Oldboy' # del OldboyStudent.school # print(OldboyStudent.__dict__) # OldboyStudent.__dict__['x']=1111111111111111111111 #產生程序中的對象:類名加括號,調用類,產生一個該類的實際存在的對象,該調用過程稱爲實例化,產生的結果又能夠成爲實例 # class OldboyStudent: # school = 'oldboy' # #obj1,'李大炮',18,'女' # def __init__(self,name,age,sex): #在實例化時,產生對象以後執行 # # if not isinstance(name,str): # # raise TypeError('名字必須是字符串類型') # self.name=name # self.age=age # self.sex=sex # # return None #__init__方法必須返回None # #obj1.name='李大炮' # #obj1.age=18 # #obj1.sex='女' # # def learn(self): # print('is learning') # # def eat(self): # print('is eating') # obj1=OldboyStudent('李大炮',18,'女') # # 分兩步: # 第一步:先產生一個空對象obj1 # 第二步:OldboyStudent.__init__(obj1,'李大炮',18,'女') # print(obj1.__dict__) # # obj2=OldboyStudent('張全蛋',28,'男') # obj3=OldboyStudent('牛榴彈',18,'女') # # print(obj2.__dict__) # print(obj3.__dict__) # # # # print(obj1.name)#obj1.__dict__['name'] # # obj1.name='大炮' # print(obj1.__dict__) # obj1.__dict__['name']='炮' # print(obj1.name) # # obj1.__dict__.pop('name') # # print(obj1.name) # print(obj1.__dict__) # obj1=OldboyStudent(123123123,18,'女') # school='hahahahahahaah' class OldboyStudent: # school = 'oldboy' def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def learn(self): print('%s is learning' %self.name) def eat(self): print('is eating') obj1=OldboyStudent('李大炮',18,'女') obj2=OldboyStudent('張全蛋',28,'男') obj3=OldboyStudent('牛榴彈',18,'女') # print(obj1.__dict__) # print(obj1.name,obj1.age,obj1.sex) #對象能夠訪問類的數據屬性,結論:類的數據屬性共享給全部對象使用,id對同樣 # print(obj1.school,id(obj1.school)) # print(obj2.school,id(obj2.school)) # print(obj3.school,id(obj3.school)) # print(OldboyStudent.school,id(OldboyStudent.school)) #類的函數屬性是綁定給全部對象使用的,綁定給不一樣的對象是不一樣的綁定方法,綁定方法有何特殊之處? #暫且拋開綁定方法,類確定能夠訪問本身的函數屬性 # OldboyStudent.learn(obj1) # OldboyStudent.learn(obj2) # OldboyStudent.learn(obj3) # print(obj1.learn) # print(obj2.learn) # print(obj3.learn) # print(OldboyStudent.learn) #綁定方法:綁定給誰,就由誰來調用,誰來調用就把「誰」自己當作第一個參數傳入 # obj1.learn() #OldboyStudent.learn(obj1) # obj2.learn() #OldboyStudent.learn(obj1) # obj3.learn() #OldboyStudent.learn(obj1) #在python3中類型就類 # print(OldboyStudent) # print(list) # l1=list() # l2=list() # l3=list() # # print(type(l1)) # # print(type(obj1)) # # # l1.append(3) #list.append(l1,3) # list.append(l1,3) # print(l1) # print(l2) # print(l3) #屬性查找順序:先從對象的__dict__中找,而後到類的__dict__中找,而後父類.... # OldboyStudent.school='哈佛' # obj1.school='hahahahahahahhahahahahahah' # print(obj1.__dict__) # print(obj1.school) # # print(obj2.school) # print(obj3.school) class Foo: count=0 def __init__(self,x,y,z): self.x=x self.y=y self.z=z Foo.count+=1 obj1=Foo(1,1,1) obj2=Foo(1,2,1) obj3=Foo(1,2,3) # print(obj1.count) # print(Foo.count) class OldboyStudent: # school = 'oldboy' def __init__(self,name,age,sex='male'): self.name=name self.age=age self.sex=sex def learn(self): print('%s is learning' %self.name) def eat(self,y): print('is eating') obj1=OldboyStudent('李大炮',18) # OldboyStudent.eat() # obj1.eat(1) obj1.learn()
7 屬性查找函數
8 繼承學習
#繼承的基本形式 # class ParentClass1(object): #定義父類 # pass # # class ParentClass2: #定義父類 # pass # # class SubClass1(ParentClass1): #單繼承,基類是ParentClass1,派生類是SubClass # pass # # class SubClass2(ParentClass1,ParentClass2): #python支持多繼承,用逗號分隔開多個繼承的類 # pass # # # # # print(SubClass1.__bases__) # print(SubClass2.__bases__) # print(ParentClass1.__bases__) #經典類與新式類的區別 # class Animal: # x=1 # def __init__(self,name,age,sex): # self.name=name # self.age=age # self.sex=sex # # print('Animal.__init__') # def eat(self): # print('%s eat' %self.name) # # def talk(self): # print('%s say' %self.name) # # class People(Animal): # x=10 # def __init__(self,name,age,sex,education): # Animal.__init__(self,name,age,sex) # self.education=education # # print('People.__init__') # # def talk(self): # Animal.talk(self) # print('這是人在說話') # # class Dog(Animal): # pass # class Pig(Animal): # pass # # # peo1=People('alex',18,'male','小學肄業') # print(peo1.__dict__) # peo1.talk() # print(peo1.x) # dog1=Dog('yuanhao',28,'male') # pig1=Pig('wupeiqi',18,'male') # # # print(peo1.name) # print(dog1.name) # print(pig1.name) class OldboyPeople: school = 'oldboy' def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def eat(self): print('is eating') class OldboyStudent(OldboyPeople): def learn(self): print('%s is learning' %self.name) class OldboyTeacher(OldboyPeople): def __init__(self,name,age,sex,salary,title): OldboyPeople.__init__(self,name,age,sex) self.salary=salary self.title=title def teach(self): print('%s is teaching' %self.name) yl_obj=OldboyStudent('yanglei',28,'female') egon_obj=OldboyTeacher('egon',18,'male',3.1,'沙河霸道金牌講師') # # yl_obj.learn() # yl_obj.eat() print(egon_obj.__dict__) ''' 總結: 1 繼承的功能之一:解決類與類之間的代碼重複問題 2 繼承是類與類之間的關係,是一種,什麼是什麼的關係 3 在子類派生出的新的屬性,已本身的爲準 4 在子類派生出的新的方法內重用父類的功能的方式:指名道姓法 OldboyPeople.__init__ 這種調用方式自己與繼承是沒有關係 '''
9 組合ui
class OldboyPeople: school = 'oldboy' def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def eat(self): print('is eating') class OldboyStudent(OldboyPeople): def __init__(self,name,age,sex): OldboyPeople.__init__(self,name,age,sex) self.course=[] def learn(self): print('%s is learning' %self.name) class OldboyTeacher(OldboyPeople): def __init__(self,name,age,sex,salary,title): OldboyPeople.__init__(self,name,age,sex) self.salary=salary self.title=title self.course=[] def teach(self): print('%s is teaching' %self.name) class Course: def __init__(self,course_name,course_period,course_price): self.course_name=course_name self.course_period=course_period self.course_price=course_price def tell_info(self): print('<課程名:%s 週期:%s 價格:%s>' %(self.course_name,self.course_period,self.course_price)) python=Course('Python','6mons',3000) linux=Course('Lnux','3mons',2000) bigdata=Course('BigData','1mons',1000) # python.tell_info() egon_obj=OldboyTeacher('egon',18,'male',3.1,'沙河霸道金牌講師') # # egon_obj.course.append(python) # egon_obj.course.append(linux) # # for obj in egon_obj.course: # obj.tell_info() yl_obj=OldboyStudent('yanglei',28,'female') yl_obj.course.append(python) for i in yl_obj.course: # print(i.course_name,i.course_period,i.course_price) i.tell_info()
10 子類調用父類方法(super)spa
# class OldboyPeople: # school = 'oldboy' # def __init__(self,name,age,sex): # self.name=name # self.age=age # self.sex=sex # # def eat(self): # print('is eating') # def teach(self): # print('這是父類的teach') # # class OldboyTeacher(OldboyPeople): # def __init__(self,name,age,sex,salary,title): # # OldboyPeople.__init__(self,name,age,sex) # #在Python2中須要寫全:super(OldboyTeacher,self) # super().__init__(name,age,sex) # self.salary=salary # self.title=title # # def teach(self): # # OldboyPeople.teach(self) # super().teach() # print('%s is teaching' %self.name) # print(OldboyTeacher.mro()) # # egon_obj=OldboyTeacher('egon',18,'male',3.1,'沙河霸道金牌講師') # # print(egon_obj.title) # # print(egon_obj.__dict__) # egon_obj.teach() class A: def test(self): super().test() class B: def test(self): print('B') class C(A,B): pass # a=A() # a.test() print(C.mro()) c=C() c.test()
11 綁定方法與非綁定方法設計
# import settings # # class MySql: # def __init__(self,host,port): # self.host=host # self.port=port # # @classmethod # def from_conf(cls): # print(cls) # # return cls(settings.HOST,settings.PORT) # # def func1(self):pass # # # conn1=MySql('127.0.0.1',3306) # print(MySql.from_conf) # conn2=MySql.from_conf() # print(conn1.host,conn2.host) # print(conn1.func1) # print(conn1.from_conf) # print(MySql.from_conf) # conn1.from_conf() # MySql.from_conf() import settings import uuid class MySql: def __init__(self,host,port): self.host=host self.port=port self.id=self.create_id() # @classmethod # def from_conf(cls): #綁定給類的 # print(cls) # # return cls(settings.HOST,settings.PORT) # # def func1(self): #綁定給對象的 # pass @staticmethod def create_id(): #非綁定方法 return str(uuid.uuid1()) conn1=MySql('127.0.0.1',3306) conn2=MySql('127.0.0.2',3306) conn3=MySql('127.0.0.3',3306) # print(MySql.create_id) # print(conn1.create_id) print(conn1.id,conn2.id,conn3.id)