Python开发【第六篇】:面向对象

configparser模块

configparser用于处理特定格式的文件,其本质是利用open来操作文件。

文件a.txt

  1. [section1]
  2. k1 = 123
  3. k2:v2
  4.  
  5. [section2]
  6. k1 = 234

加载文件a.txt

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('a.txt',encoding='utf-8')

对文件相关操作

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('a.txt',encoding='utf-8')
  5.  
  6. #获取所有节点
  7. ret = config.sections()
  8. print(ret)
  9.  
  10. #获取指定节点下的所有键值对
  11. ret = config.items('section1')
  12. print(ret)
  13.  
  14. #获取指定节点下所有的键
  15. ret = config.options('section1')
  16. print(ret)
  17.  
  18. #获取指定节点下指定key的值
  19.  
  20. ret = config.get('section1','k1')
  21. #转换成int
  22. # ret = config.getint('section1','k1')
  23. #转换成float
  24. # ret = config.getfloat('section1','k1')
  25. #转换成boolean
  26. # ret = config.getboolean('section1','k1')
  27.  
  28. print(ret)
  29.  
  30. #检查、删除、设置指定组内的键值对
  31.  
  32. #检查
  33. has_opt = config.has_option('section1')
  34. print(has_opt)
  35.  
  36. #添加节点
  37. config.add_section('SEC_1')
  38. config.write(open('a.txt','w'))
  39.  
  40. #删除节点
  41. config.remove_section('SEC_1')
  42. config.write(open('a.txt','w'))
  43.  
  44. #检查、删除、设置指定组内的键值对
  45.  
  46. #检查
  47. has_opt = config.has_option('section1','k1')
  48. print(has_opt)
  49.  
  50. #删除
  51. config.remove_option('section1','k1')
  52. config.write(open('a.txt','w'))
  53.  
  54. #设置
  55. config.set('section1','k10','123')
  56. config.write(open('a.txt','w'))

XML模块

xml是实现不同语言或程序之间进行数据交换的协议。

文件xml_text.xml

  1. <data>
  2.     <country name="Liechtenstenin">
  3.         <rank update="yes">2</rank>
  4.         <year>2023</year>
  5.         <gdppc>141100</gbppc>
  6.         <neighbor direction="E" name="Austria"/>
  7.         <neighbor direction="W" name="Switzerland"/>
  8.     </country>
  9.     <country name="Sinagapore">
  10.         <rank update="yes">5</rank>
  11.         <year>2026</year>
  12.         <gdppc>59900</gdppc>
  13.         <neighbor direction="N" name="Malaysia"/>
  14.     </country>
  15.     <country name="Panama">
  16.         <rank update="yes">69</rank>
  17.         <year>2026</year>
  18.         <gdppc>13600</gdppc>
  19.         <neighbor direction="W" name="Costa Rica"/>
  20.         <neighbor direction="E" name="Costa Rica"/>
  21.     </country>
  22. </data>

xml相关操作

解析xml文件

  1. from xml.etree import ElementTree as ET
  2. #直接解析xml文件
  3. tree = ET.parse('xml_test.xml')
  4.  
  5. #获取xml文件的根节点
  6. root = tree.getroot()
  7. print(root)
  8. print(root.tag)
  9. print(root.attrib)
  10. """
  11. 输出:
  12. <Element 'data' at 0x00000000006D0688>
  13. data
  14. {'title_2': 'test_2', 'title_1': 'test_1'}
  15. """

解析字符串

  1. from xml.etree import ElementTree as ET
  2.  
  3. #打开文件,读取xml内容
  4. str_xml = open('xm_test.xml','r').read()
  5.  
  6. #将字符串解析成xml特殊对象,root代指xml文件的根节点
  7. root = ET.XML(str_xml)

遍历xml文档的所有内容

  1. from xml.etree import ElementTree as ET
  2. #直接解析xml文件
  3. tree = ET.parse('xml_test.xml')
  4.  
  5. #获取xml文件的根节点
  6. root = tree.getroot()
  7. #遍历xml文档的第二层
  8. for child in root:
  9.     #第二层节点的标签名称和标签属性
  10.     print(child.tag,child.attrib)
  11.     #遍历xml文档的第三层
  12.     for i in child:
  13.         #第三层节点的标签名称和内容
  14.         print(i.tag,i.text)
  15. """
  16. 输出:
  17. country {'name': 'Liechtenstenin'}
  18. rank 2
  19. year 2023
  20. gdppc 141100
  21. neighbor None
  22. neighbor None
  23. country {'name': 'Sinagapore'}
  24. rank 5
  25. year 2026
  26. gdppc 59900
  27. neighbor None
  28. country {'name': 'Panama'}
  29. rank 69
  30. year 2026
  31. gdppc 13600
  32. neighbor None
  33. neighbor None
  34. """

修改xml

解析字符串方式进行修改

  1. from xml.etree import ElementTree as ET
  2. str_xml = open('xml_test.xml','r').read()
  3. root = ET.XML(str_xml)
  4.  
  5. #获取顶层标签
  6. print(root.tag)
  7.  
  8. #循环所有的year节点
  9. for node in root.iter('year'):
  10.     #将year节点中的内容自增一
  11.     new_year = int(node.text) + 1
  12.     node.text = str(new_year)
  13.  
  14.     #设置属性
  15.     node.set('name','alex')
  16.     node.set('age','19')
  17.  
  18.     #删除属性
  19.     del node.attrib['name']
  20.  
  21. #更新文件
  22. tree = ET.ElementTree(root)
  23. tree.write("new_xml_test.xml",encoding='utf-8')

解析文件方式进行修改

直接调用tree.write写入即可。

  1. from xml.etree import ElementTree as ET
  2. tree = ET.parse('xml_test.xml')
  3. root = tree.getroot()
  4.  
  5. """
  6. 操作
  7. """
  8.  
  9. #更新文件
  10. tree.write("new_xml_test2.xml",encoding='utf-8')

添加节点

  1. from xml.etree import ElementTree as ET
  2. tree = ET.parse('xml_test.xml')
  3. root = tree.getroot()
  4.  
  5. ele = ET.Element('Alex',{'k1':'v1'})
  6. ele.text = "test"
  7. root.append(ele)
  8.  
  9. tree.write("new.xml",encoding='utf-8')

结果new.xml:

  1. <data title_1="test_1" title_2="test_2">
  2.  
  3. """
  4. 原内容
  5. """
  6.  
  7. <Alex k1="v1">test</Alex></data>

由于原生保存xml时默认无缩进,如果要设置缩进需要修改保存方式。

  1. from xml.etree import ElementTree as ET
  2. from xml.dom import minidom
  3.  
  4. def prettify(elem):
  5.     """
  6.     将节点转换成字符串,并添加缩进
  7.     :param elem:
  8.     :return:
  9.     """
  10.     rough_string = ET.tostring(elem,'utf-8')
  11.     reparsed = minidom.parseString(rough_string)
  12.     return reparsed.toprettyxml(indent=" ")
  13.  
  14. #创建根节点
  15. root = ET.Element("family")
  16.  
  17. #创建大儿子
  18. # son1 = ET.Element('son',{'name':'儿1'})
  19. son1 = root.makeelement('son',{'name':'儿1'})
  20. #创建小儿子
  21. # son2 = ET.Element('son',{'name':'儿2'})
  22. son2 = root.makeelement('son',{'name':'儿2'})
  23.  
  24. #在儿子中创建2个孙子
  25. # grandson1 = ET.Element('grandson',{'name':'儿11'})
  26. grandson1 = root.makeelement('grandson',{'name':'儿11'})
  27. # grandson2 = ET.Element('grandon',{'name':'儿12'})
  28. grandson2 = root.makeelement('grandon',{'name':'儿12'})
  29.  
  30. son1.append(grandson1)
  31. son2.append(grandson2)
  32.  
  33. #把儿子添加到根节点
  34. root.append(son1)
  35. root.append(son2)
  36.  
  37. raw_str = prettify(root)
  38.  
  39. f = open('family.xml','w',encoding='utf-8')
  40. f.write(raw_str)
  41. f.close()

family.xml:

  1. <?xml version="1.0" ?>
  2. <family>
  3.    <son name="儿1">
  4.       <grandson name="儿11"/>
  5.    </son>
  6.    <son name="儿2">
  7.       <grandon name="儿12"/>
  8.    </son>
  9. </family>

shutil

高级的文件、文件夹、压缩包处理模块。

shutil.copyfileobj(ferc,fdst[,length])

将文件内容拷贝到另一个文件中

  1. import shutil
  2.  
  3. shutil.copyfileobj(open('old.xml','r'),open('new.xml','w'))

shuit.copyfile(src,dst)

拷贝文件

  1. shutil.copyfile('f1.log','f2.log')

zipfile、tarfile

zipfile创建压缩包

  1. import zipfile
  2.  
  3. #压缩
  4. z = zipfile.ZipFile('test.zip','a')
  5. z.write('new.xml')
  6. z.write('family.xml')
  7. z.close

zipfile解压压缩包

  1. #解压
  2. z = zipfile.ZipFile('test.zip','r')
  3.  
  4. #解压全部
  5. # z.extractall()
  6.  
  7. #解压单个文件
  8. z.extract("new.xml")
  9.  
  10. #获取压缩包的成员
  11. for item in z.namelist():
  12.     print(item)
  13.  
  14. z.close()

tarfile创建压缩包

  1. import tarfile
  2.  
  3. #压缩
  4. tar = tarfile.open("test.tar",'w')
  5. #arcname重命名
  6. tar.add('test.py',arcname='test_1.py')
  7. tar.add('xml_test.py',arcname='xml_test.py')
  8. tar.close()

tarfile解压压缩包

  1. tar = tarfile.open('test.tar','r')
  2.  
  3. #可设置解压路径
  4. # tar.extractall()
  5.  
  6. for item in tar.getmembers():
  7.     print(item,type(item))
  8.  
  9. obj = tar.getmember("test_1.py")
  10. print(obj,type(obj))
  11. tar.extract(obj)
  12.  
  13. tar.close()

系统命令

call

获取状态码,0正常。

  1. import subprocess
  2.  
  3. #shell=False命令传入方式为列表
  4. ret = subprocess.call(["ls","-l"],shell=False)
  5.  
  6. #shell=True命令传入方式为字符串
  7. ret = subprocess.call("ls -l",shell=True)

check_call

执行命令,如果执行状态码是0,则返回0,否则抛出异常。

  1. ret = subprocess.check_call("ls -l",shell=True)

check_output

执行命令,如果状态码是0,则返回执行结果,否则抛出异常。

  1. ret = subprocess.check_output("ls -l",shell=True)

函数式编程和面向对象编程实现发送邮件功能。

函数实现:

  1. def mail(email,message):
  2.     print("发送")
  3.    return True
  4.  
  5. mail("xxxx.@126.com","hello")

面向对象实现:

  1. class Foo:
  2.     #方法
  3.    def mail(self,email,message):
  4.        print("发送")
  5.       return True
  6.  
  7. #调用
  8. #1、创建对象,类名()
  9. obj = Foo()
  10. #2、通过对象去执行方法
  11. obj.mail("xxxx.@126.com","hello")

类和对象

1、创建类:

class 类名:

def 方法名(self,xxx):

pass

2、创建对象

对象 = 类名()

3、通过对象执行方法

对象.方法名(xxx)

函数式

def fetch(host,username,passwd,sql):

pass

def create(host,username,passwd,sql):

pass

def remove(host,username,passwd,sql):

pass

def modify(host,username,passwd,sql):

pass

fetch(…)

面向对象:

class SQLHelper:

def fetch(self,host,username,passwd,sql):

pass

def create(self,host,username,passwd,sql):

pass

def remove(self,host,username,passwd,nid):

pass

def modify(self,host,username,passwd,name):

pass

obj = SQLHelper()

obj.fetch(…)

面向对象优化:

class SQLHelper:

def fetch(self, sql):

pass

def create(self,sql):

pass

def remove(self,nid):

pass

def modify(self,name):

pass

obj = SQLHelper()

obj.hhost = "xxx.xxx.xxx"

obj.uusername = "xxxx"

obj.passwd = "xxxx"

obj.fetch("sql")

  1. class SQLHelper:
  2.     def fetch(self, sql):
  3.         #链接数据库
  4.         print(self.hhost)
  5.         print(self.uusername)
  6.         print(self.passwd)
  7.         print(sql)
  8.     def create(self,sql):
  9.         pass
  10.     def remove(self,nid):
  11.         pass
  12.     def modify(self,name):
  13.         pass
  14. obj = SQLHelper()
  15. obj.hhost = "xxx.xxx.xxx"
  16. obj.uusername = "xxxx"
  17. obj.passwd = "xxxx"
  18. obj.fetch("select * from A")
  19. 输出:
  20. xxx.xxx.xxx
  21. xxxx
  22. xxxx
  23. select * from A

什么时候使用面向对象?

当某些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性封装到对象中,方便以后去对象中取值。

self是什么?

self是一个python自动传值的参数,那个对象执行方法,self就是谁。

obj1 = SQLHelper()

obj1.hhost = "1xxx.xxx.xxx"

obj1.uusername = "xxxx"

obj1.passwd = "xxxx"

obj1.fetch("sql") #self==obj1

 

obj2 = SQLHelper()

obj2.hhost = "2xxx.xxx.xxx"

obj2.uusername = "xxxx"

obj2.passwd = "xxxx"

obj2.fetch("sql") #self==obj2

构造方法

类中有一个特殊的方法__init__,类()自动被执行。

  1. class SQLHelper:
  2.     def __init__(self,a1,a2,a3):
  3.         self.hhost = a1
  4.         self.uusername = a2
  5.         self.passwd = a3
  6.         print("自动执行init")
  7.     def fetch(self, sql):
  8.         #链接数据库
  9.         print(self.hhost)
  10.         print(self.uusername)
  11.         print(self.passwd)
  12.         print(sql)
  13.     def create(self,sql):
  14.         pass
  15.     def remove(self,nid):
  16.         pass
  17.     def modify(self,name):
  18.         pass
  19. obj1 = SQLHelper("1xxx.xxx.xxx","xxxx","xxxx")
  20. obj1.fetch("select * from A")
  21. obj2 = SQLHelper("2xxx.xxx.xxx","xxxx","xxxx")
  22. obj2.fetch("select * form A")
  23. 输出:
  24. 自动执行init
  25. 1xxx.xxx.xxx
  26. xxxx
  27. xxxx
  28. select * from A
  29. 自动执行init
  30. 2xxx.xxx.xxx
  31. xxxx
  32. xxxx
  33. select * form A

面向对象三大特性:封装、继承、多态。

封装

面向对象的程序设计中,某个类把所需要的数据(类的属性)和对数据的操作(类的行为)全部都封装在类中,分别称为类的成员变量和方法(成员函数)。这种把成员变量和成员函数封装在一起的编程特性称为封装。

  1. class c1:
  2.     def __init__(self,name,obj):
  3.         self.name = name
  4.         self.obj = obj
  5.  
  6. class c2:
  7.     def __init__(self,name,age):
  8.         self.name = name
  9.         self.age = age
  10.  
  11.     def show(self):
  12.         print(self.name)
  13.  
  14. class c3:
  15.     def __init__(self,a1):
  16.         self.money = 123
  17.         self.aaa = a1
  18.  
  19.  
  20. c2_obj = c2('aa',11)
  21.  
  22. c1_obj = c1("alex",c2_obj)
  23. print(c1_obj.obj.age)
  24.  
  25. c3_obj = c3(c1_obj)
  26. print(c3_obj.aaa.obj.age)
  27. 输出:
  28. 11
  29. 11

继承

继承是两个类或多个类之间的父子关系,子进程继承父进程的所有公有实例变量和方法。继承实现了代码的重用。重用已经存在的数据和行为,减少代码的重新编写,python在类名后用一对圆括号表示继承关系,括号中的类表示父类,如果父类定义了__init__方法,则子类必须显示地调用父类的__init__方法,如果子类需要扩展父类的行为,可以添加__init__方法的参数。

单继承

  1. class F1:#父类、基类
  2.     def show(self):
  3.         print('show')
  4.  
  5. #F2继承F1
  6. class F2(F1):#子类、派生类
  7.     def bar(self):
  8.         print('bar')
  9.  
  10. obj = F2()
  11. obj.bar()
  12. obj.show()
  13. 输出:
  14. bar
  15. show

 

  1. class F1:#父类、基类
  2.     def show(self):
  3.         print('show')
  4.  
  5.     def foo(self):
  6.         print(self.name)
  7.  
  8. #F2继承F1
  9. class F2(F1):#子类、派生类
  10.     def __init__(self,name):
  11.         self.name = name
  12.  
  13.     def bar(self):
  14.         print('bar')
  15.  
  16.     def show(self):#自己的优先级更高
  17.         print('F2 show')
  18.  
  19. obj = F2('alex')
  20. obj.bar()
  21. obj.show()
  22. obj.foo()
  23. 输出:
  24. bar
  25. F2 show
  26. alex

 

  1. class S1:
  2.     def F1(self):
  3.         self.F2()
  4.  
  5.     def F2(self):
  6.         print('S1.F2()')
  7.  
  8. class S2(S1):
  9.     def F3(self):
  10.         self.F1()
  11.  
  12.     def F2(self):
  13.         print('S2.F2()')
  14.  
  15. obj = S2()
  16. obj.F3()
  17. 输出:
  18. S2.F2()

多继承

  1. class C1:
  2.     def f1(self):
  3.         pass
  4.  
  5. class C2:
  6.     def f2(self):
  7.         pass
  8.  
  9. class C3(C2,C1):
  10.     pass
  11.  
  12. obj = C3()

 

  1. class C1:
  2.     def f1(self):
  3.         print("C1.f1()")
  4.  
  5. class C2:
  6.     def f1(self):
  7.         print("C2.f1()")
  8.  
  9. class C3(C2,C1):
  10.     pass
  11.  
  12. obj = C3()
  13. obj.f1()
  14. 输出:
  15. C2.f1()

 

  1. class C0:
  2.     def f1(self):
  3.         print("C0.f1()")
  4.  
  5. class C1():
  6.     def f1(self):
  7.         print("C1.f1()")
  8.  
  9. class C2(C0):
  10.     def f2(self):
  11.         print("C2.f1()")
  12.  
  13. class C3(C2,C1):
  14.     pass
  15.  
  16. obj = C3()
  17. obj.f1()
  18. 输出:
  19. C0.f1()

 

  1. class C_2:
  2.     def f1(self):
  3.         print("C_2.f1()")
  4.  
  5. class C_1(C_2):
  6.     def f1(self):
  7.         print("C_1.f1()")
  8.  
  9. class C0(C_2):
  10.     def f2(self):
  11.         print("C0.f1()")
  12.  
  13. class C1(C_1):
  14.     def f1(self):
  15.         print("C1.f1()")
  16.  
  17. class C2(C0):
  18.     def f2(self):
  19.         print("C2.f1()")
  20.  
  21. class C3(C2,C1):
  22.     pass
  23.  
  24. obj = C3()
  25. obj.f1()
  26. 输出:
  27. C1.f1()

继承总结

对于继承其方法(属性)可能定义在当前类,也可能来自于基类,所以在方法调用时就需要对当前类和基类进行搜索以确定方法所在的位置。而搜索的顺序就是所谓的方法解析顺序(MRO、Method Resolution Order)。对于单继承来说,MRO一般比较简单,而对于多继承来说,MRO比较复杂。下面就二种基本的继承模式解析。

原文地址:https://www.cnblogs.com/yinshoucheng-golden/p/6323504.html