码迷,mamicode.com
首页 > 编程语言 > 详细

1、python 函数式+面向对象

时间:2016-06-20 18:54:41      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:









from xml.etree import ElementTree as ET

tree = ET.parse(‘xo.xml‘)
root = tree.getroot()

for child in root:
print(child.tag,child.attrib)
for gradechild in child:
print(gradechild.tag, gradechild.text)

 

 打开文件,读取XML内容
from xml.etree import ElementTree as ET
str_xml = open(‘xo.xml‘, ‘r‘).read()

# 将字符串解析成xml特殊对象,root代指xml文件的根节点
root = ET.XML(str_xml)

############ 操作 ############

# 顶层标签
print(root.tag)

# 循环所有的year节点
for node in root.iter(‘year‘):
# 将year节点中的内容自增一
new_year = int(node.text) + 1
node.text = str(new_year)

# 设置属性
node.set(‘name‘, ‘alex‘)
node.set(‘age‘, ‘18‘)
# 删除属性
del node.attrib[‘name‘]

############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding=‘utf-8‘)




from xml.etree import ElementTree as ET

tree = ET.parse(‘xo.xml‘)
root = tree.getroot()

ele = ET.Element(‘Alex‘, {‘k1‘: ‘v1‘})
ele.text = "我是内容"
root.append(ele)

tree.write("new.xml", encoding="utf-8")

# 列表
# root_list = [
# []
# ]
#
# list ==> class 类名
# 类名() > 创建了一个对象
# ele = list()
# e = dict()
# el = Element()
# root_list.append(ele)


from xml.etree import ElementTree as ET


# 创建根节点
root = ET.Element("famliy")


# 创建节点大儿子
son1 = ET.Element(‘son‘, {‘name‘: ‘儿1‘})
# 创建小儿子
son2 = ET.Element(‘son‘, {"name": ‘儿2‘})

# 在大儿子中创建两个孙子
grandson1 = ET.Element(‘grandson‘, {‘name‘: ‘儿11‘})
grandson2 = ET.Element(‘grandson‘, {‘name‘: ‘儿12‘})
son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son1)

tree = ET.ElementTree(root)
tree.write(‘oooo.xml‘,encoding=‘utf-8‘, xml_declaration=True, short_empty_elements=False)





def prettify(elem):
"""将节点转换成字符串,并添加缩进。
"""
rough_string = ET.tostring(elem, ‘utf-8‘)
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent="\t")

# 创建根节点
root = ET.Element("famliy")


# 创建大儿子
# son1 = ET.Element(‘son‘, {‘name‘: ‘儿1‘})
son1 = root.makeelement(‘son‘, {‘name‘: ‘儿1‘})
# 创建小儿子
# son2 = ET.Element(‘son‘, {"name": ‘儿2‘})
son2 = root.makeelement(‘son‘, {"name": ‘儿2‘})

# 在大儿子中创建两个孙子
# grandson1 = ET.Element(‘grandson‘, {‘name‘: ‘儿11‘})
grandson1 = son1.makeelement(‘grandson‘, {‘name‘: ‘儿11‘})
# grandson2 = ET.Element(‘grandson‘, {‘name‘: ‘儿12‘})
grandson2 = son1.makeelement(‘grandson‘, {‘name‘: ‘儿12‘})

son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son1)


raw_str = prettify(root)

f = open("xxxoo.xml",‘w‘,encoding=‘utf-8‘)
f.write(raw_str)
f.close()



import zipfile

# 压缩
# z = zipfile.ZipFile(‘laxi.zip‘, ‘a‘)
# z.write(‘newnew.xml‘)
# z.write(‘xo.xml‘)
# z.close()

# 解压
z = zipfile.ZipFile(‘laxi.zip‘, ‘r‘)
# z.extractall()

# for item in z.namelist():
# print(item,type(item))
z.extract("new.xml")

z.close()



import tarfile

# 压缩
# tar = tarfile.open(‘your.tar‘,‘w‘)
# tar.add(‘s1.py‘, arcname=‘ssss.py‘)
# tar.add(‘s2.py‘, arcname=‘cmdb.py‘)
# tar.close()

# 解压
tar = tarfile.open(‘your.tar‘,‘r‘)
# tar.extractall() # 可设置解压地址

# for item in tar.getmembers():
# print(item,type(item))
obj = tar.getmember("ssss.py")
tar.extract(obj)

tar.close()


class SQLHelper:

def __init__(self, a1, a2, a3):
print(‘自动执行init‘)
self.hhost = a1
self.uuserane = a2
self.pwd = a3
self.create(‘sss‘)
def fetch(self, sql):
# 连接数据
print(self.hhost)
print(self.uuserane)
print(self.pwd)
print(sql)
def create(self, sql):
pass
def remove(self, nid):
pass
def modify(self, name):
pass

 obj1 = SQLHelper(‘c1.salt.com‘, ‘alex‘, 123)
# obj2 = SQLHelper(‘c2.salt.com‘, ‘alex1‘, 12333)
# obj1.create()


class c1:

def __init__(self,name,obj):
self.name = name
self.obj = obj

class c2:

def __init__(self,name,age):
self.name = name
self.age = age

def show(self):
print(self.name)
return 123

class c3:
def __init__(self, a1):
self.money = 123
self.aaa = a1


c2_obj = c2(‘aa‘, 11)
# c2_obj是c2类型
# - name = "aa"
# - age = 11
c1_obj = c1("alex", c2_obj)
# c1_obj 是c1 类型
# - name = "alex"
# - obj = c2_obj
c3_obj = c3(c1_obj)
# 使用c3_obj执行show方法
ret = c3_obj.aaa.obj.show()
print(ret)



"""
class F1: # 父类,基类
def show(self):
print(‘show‘)

def foo(self):
print(self.name)

class F2(F1): # 子类,派生类
def __init__(self, name):
self.name = name

def bar(self):
print(‘bar‘)
def show(self):
print(‘F2.show‘)

obj = F2(‘alex‘)
# obj.show()
obj.foo()
"""

# obj = F2()
# obj.bar()
# obj.show()
# obj = F1()
# obj.bar()





# class S1:
#
# def F1(self):
# self.F2()
#
# def F2(self):
# pass
#
# class S2(S1):
#
# def F3(self):
# self.F1()
#
# def F2(self):
# pass
#
# obj = S2()
# obj.F3()
# obj = S1()
# obj.F1()
class C_2:
def f2(self):
print(‘C-2‘)

class C_1(C_2):
def f2(self):
print(‘C-1‘)

class C0(C_2):
def f1(self):
print(‘C0‘)

class C1(C0):

def f1(self):
print(‘C1‘)

class C2(C_1):
def f2(self):
print(‘C2‘)

class C3(C1,C2):
def f3(self):
pass

obj = C3()
obj.f2()





 

1、python 函数式+面向对象

2、函数式编程,面向对象编程实现:发送邮件的功能
# 函数
def mail(email, message):
print("去发吧")
return True

mail("alex3714@126.com", "好人")

面向对象:类,对象

class Foo:

# 方法
def mail(self, email, message):
print(‘去发吧‘)
return True

# 调用
1、创建对象,类名()
obj = Foo()
2、通过对象去执行方法
obj.mail("alex3714@126.com", "好人")

3、类和对象

a. 创建类
class 类名:

def 方法名(self,xxxx):
pass

b. 创建对象
对象 = 类名()

c. 通过对象执行方法
对象.方法名(123)

4、
函数式:

def fetch(host, username, password, sql):
pass
def create(host, username, password, sql):
pass
def remove(host, username, password, nid):
pass
def modify(host, username, password, name):
pass
...

fetch(....)
面向对象

class SQLHelper:

def fetch(self, sql):
pass

def create(self, sql):
pass
def remove(self, nid):
pass
def modify(self, name):
pass

obj1 = SQLHelper()
obj1.hhost = "c1.salt.com"
obj1.uuserane = "alex"
obj1.pwd = "123"

obj1.fetch("select * from A")


obj2 = SQLHelper()
obj2.hhost = "c2.salt.com"
obj2.uuserane = "alex"
obj2.pwd = "123"

obj2.fetch("select * from A")

 



========> 什么时候用面向对象?当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可

5、self是什么鬼?
self是一个python自动会给传值的参数

那个对象执行方法,self就是谁。

obj1.fetch(‘selec...‘) self=obj1

obj2.fetch(‘selec...‘) self=obj2

6、构造方法

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


7、面向对象

三大特性:封装、继承、多态
















1、python 函数式+面向对象

标签:

原文地址:http://www.cnblogs.com/liushubao/p/5601341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!