Mix-in:混入类是一种Python程序设计中的技术,作用是在运行期间动态改变类的基类或类的方法,从而使得类的表现可以发生变化。可以用在一个通用类接口中。

在实践一个 解析XML文件的实践中,体会动态改变的格式。

格式一般是:

定义一个基类:

class base:

def startElement(self,prefix,name,*args):

self.callback('Start',name,*args)

def callback(self,prefx,name,*args):

mname = prefix + name

method = getattr(self,mname,None)

if callbale(method): method(*args)

然后定义一个子类,在里面实现prefix+name的方法。

处理xml的内置库:

from   xml.sax.hander import ContentHandler

from xml.sax import parse

parse('xmlfile', instanceofContentHandler)

from os import path
from xml.sax.handler import ContentHandler
from xml.sax import parse

__metaclass__ = type

class Dispatcher:
def __init__(self):
pass
def startElement(self,name,attrs):
self.dispatch('start',name,attrs)
def endElement(self,name):
self.dispatch('end',name)

def dispatch(self,prefix,name,attrs=None):
propName = prefix + name.capitalize()
dname = 'default' + prefix.capitalize()
method = getattr(self,propName,None)
if callable(method):
if prefix == 'start':
method(attrs)
else:
method()
else:
method = getattr(self,dname,None)
if callable(method):
if prefix == 'start':
method(name,attrs)
else:
method(name)

class Sub_Dispatcher(Dispatcher,ContentHandler):
in_dir = False
in_page = False
outer = None
files = []
def characters(self,content):
print content
if self.in_page: self.outer.write(content)
def startPage(self,attrs):
self.in_page = True
fname = attrs['name'] + '.html'
self.outer = open(fname,'w')
if self.in_dir:
self.files.append(fname)
document ='''
<html>
<head>
<title>%s</title>
</head>
<body>
'''
self.outer.write(document % attrs['title'])

def endPage(self):
document = '</body></html>'
self.outer.write(document)
self.outer.close()
self.in_page = False
def startDirectory(self,attrs):
self.in_dir = True
self.fullpath = path.join(path.curdir,attrs['name'])
if not path.exists(self.fullpath):
from os import mkdir
mkdir(self.fullpath)
def endDirectory(self):
from distutils.file_util import move_file
for f in self.files:
move_file(f,path.join(self.fullpath,f))
def defaultStart(self,name,attrs):
self.outer.write('<' + name + ' ')
for key,value in attrs.items():
self.outer.write('%s="%s" '%(key,value))
self.outer.write('>')
def defaultEnd(self,name):
self.outer.write('</' + name + '>')
def startWebsite(self,attrs):
pass
def endWebsite(self):
pass
parse('website.xml',Sub_Dispatcher())

Python 实践--混入类的更多相关文章

  1. Python编程从入门到实践笔记——类

    Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...

  2. python 混入类MixIn

    写在前面 能把一件事情说的那么清楚明白,感谢廖雪峰的官方网站. 1.为什么要用混入类?(小白入门) 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. step1: 回忆一下 ...

  3. Python实践之(七)逻辑回归(Logistic Regression)

    机器学习算法与Python实践之(七)逻辑回归(Logistic Regression) zouxy09@qq.com http://blog.csdn.net/zouxy09 机器学习算法与Pyth ...

  4. 机器学习算法与Python实践之(四)支持向量机(SVM)实现

    机器学习算法与Python实践之(四)支持向量机(SVM)实现 机器学习算法与Python实践之(四)支持向量机(SVM)实现 zouxy09@qq.com http://blog.csdn.net/ ...

  5. 机器学习算法与Python实践之(三)支持向量机(SVM)进阶

    机器学习算法与Python实践之(三)支持向量机(SVM)进阶 机器学习算法与Python实践之(三)支持向量机(SVM)进阶 zouxy09@qq.com http://blog.csdn.net/ ...

  6. 机器学习算法与Python实践之(二)支持向量机(SVM)初级

    机器学习算法与Python实践之(二)支持向量机(SVM)初级 机器学习算法与Python实践之(二)支持向量机(SVM)初级 zouxy09@qq.com http://blog.csdn.net/ ...

  7. 机器学习算法与Python实践之(五)k均值聚类(k-means)

    机器学习算法与Python实践这个系列主要是参考<机器学习实战>这本书.因为自己想学习Python,然后也想对一些机器学习算法加深下了解,所以就想通过Python来实现几个比较常用的机器学 ...

  8. (转) K-Means聚类的Python实践

    本文转自: http://python.jobbole.com/87343/ K-Means聚类的Python实践 2017/02/11 · 实践项目 · K-means, 机器学习 分享到:1 原文 ...

  9. 机器学习算法与Python实践之(六)二分k均值聚类

    http://blog.csdn.net/zouxy09/article/details/17590137 机器学习算法与Python实践之(六)二分k均值聚类 zouxy09@qq.com http ...

随机推荐

  1. MySQL忘记密码解决

    1.设置管理员root密码为123 开启MySQL服务后 PS C:\WINDOWS\system32> mysqladmin -uroot -p password "123" ...

  2. Python习题005

    作业一 :任意一个数字列表,然后进行排序(冒泡排序) 方法一: def test1(): list1 = [1,23,4,6,8,55,2,9,90,35] list1.sort() # sort() ...

  3. Itellij IDEA下Maven的配置

    maven基本配置 配置阿里云镜像 打开settings.xml,添加 <mirrors> <mirror> <id>alimaven</id> < ...

  4. go hello world第一个程序

    main 函数所在的包名必须使用main import "fmt"  导入包fmt  fmt包包含了Println方法的定义 func main() 程序运行入口方法和c语言相似 ...

  5. Django 中 app_name (应用命名空间) 和 namespace (实例命名空间) 的区别

    转自:https://www.jianshu.com/p/404500a0408a 补充理解: 先把官网上对应用命名空间(app_name)和实例命名空间(namespace)的解释贴上: app_n ...

  6. Angular 调试

    我们新建一个项目.执行 ng server 会启动一个网站. 1. 执行 where ng .看看ng 是什么. D:\Abp学习\angular\Mytest>where ng C:\User ...

  7. .net Core如何对静态文件的访问进行鉴权操作?

    之前给公司开发了一个文件管理服务,最基本的功能就是文件的上传下载,以及更新删除.预览:负责公司各个子系统的相关附件的管理,所有的接口都通过AOP来进行身份拦截认证了,但是在进行预览的时候,因为采用的是 ...

  8. Project Oberon

    Project Oberon Project Oberon  http://www.projectoberon.com/ Project Oberon 28.11.2018 / 11.12.2018 ...

  9. [转载]Java中继承、装饰者模式和代理模式的区别

    [转载]Java中继承.装饰者模式和代理模式的区别 这是我在学Java Web时穿插学习Java设计模式的笔记 我就不转载原文了,直接指路好了: 装饰者模式和继承的区别: https://blog.c ...

  10. restTemplate源码解析(一)构造restTemplate的Bean实例

    所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 构造一个restTemplate的Bean实例很容易,只需这样配置 @Bean publ ...