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. EasyUI 对话框弹出文件输入框

    目前用的EasyUI的dialog,要实现弹出文件输入框(或者其他输入框和对话框),我的实现方案是,首先写一个close的div,然后里面就是样式和输入框的一些代码和一个确定按钮,然后页面上一个按钮, ...

  2. Vue.js 父子组件相互传递数据

    父传子 : 子组件接收变量名=父组件传递的数据 如::f-cmsg="fmsg"  注意驼峰问题 子传父:@子组件关联的方法名 = 父组件接受的方法名 如:@func=" ...

  3. go hello world第一个程序

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

  4. 设置阿里云镜像仓库并安装Docker

    echo "设置阿里云镜像仓库" mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum ...

  5. 18-MySQL DBA笔记-MySQL Server调优

    第18章 MySQL Server调优 本章将为读者介绍针对MySQL Server的优化,这也是DBA最熟悉的领域之一.首先我们介绍MySQL的主要参数,然后,讲述常见硬件资源的优化.我们假设读者已 ...

  6. 奇妙的算法【6】-WY回文、树、最优化、卷积判断

    1,判断一个十进制正整数的二进制数是否为回文 package com.cnblogs.mufasa.answer1; import java.util.Scanner; public class Ma ...

  7. 如何在含有json类型的字段上建立多列索引

    废话不多,直接上图 如 : 表结构如图           那么我想在这三个字段上建立一个唯一索引,目的是为了防止重复插入数据, 1.首先,说明一下 data中的json中,key为 tagID 和 ...

  8. 0.a开始数据结构征程

    决定开始从mooc和ppt上学习数据结构,......书暂时不看.在进入数据结构之前,我首先将自己以一个还未进入大山但又向往山中美景的探险者身份对数据结构的几点疑问的答案的寻找和思考写在下面. 什么是 ...

  9. js中this关键字用法详解

    1.全局环境中的this 在全局环境中,this 指向全局对象Global,即 window 对象 如: alert(this); // 显示 [object Window] alert(this = ...

  10. charles 的安装和手机配置 (我用的win7系统 ,和 iphone8 的配置)

    2018/12/17 由于想抓一下某个手机上app的数据,然后就装了charles,纯记录一下,便于以后不用再查资料.个人参考的网址:https://blog.csdn.net/weixin_4233 ...