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. 脉脉的一道网红Java面试题

    题目如下: public class Test { public static void main(String[] args) { int a = 10; int b = 10; // 需要在met ...

  2. 2,electron 打包

    1,全局安装electron-packager npm install electron-packager -g 2,打包,进入要打包的文件夹 electron-packager . app --pl ...

  3. uboot 代码执行顺序

    ref:http://blog.chinaunix.net/uid-30352139-id-5128405.html uboot: 2014.07 1.1    U-boot相关文件 boards.c ...

  4. docker部署mysql,nginx,php,并上传镜像到私有仓库

    前言 最近公司准备把现有环境全部搞成容器化,所以笔者就先了解了一下docker,并搞了一搞,并把自己搞的过程记录下来.话不多说直接开干 环境说明 Centos7 Docker version 18.0 ...

  5. 怎样理解构造函数中的return语句

    因为构造函数也是一个函数, 自然也可以有return语句, 不过和一般函数不太一样的是, 在构造函数中如果return的是一个对象, 则会直接返回这个对象, 如果return 的不是一个对象, 那在n ...

  6. 数据库及MySQL基础(1)

    1.数据库概述 关系型数据库:面对关系,Java面向对象. ·常见数据库 Oracle(神喻):甲骨文 DB2:IBM SQL Server:微软 Sybase:赛尔斯 MySQL:甲骨文,最早是开源 ...

  7. 什么是实体关系图(ERD)? 转

    https://www.visual-paradigm.com/cn/guide/data-modeling/what-is-entity-relationship-diagram/#erd-data ...

  8. win10下PLSQL Developer 连接ubuntu上安装的oracle 11g

    说明:过程记录的不是很相信,只记录基本步骤.并不适合想一步一步照做的同学. win10下需要的操作 1.微软官网下载instantclient,然后接到到本地一个文件夹,注意路径不要又空格,中文和括号 ...

  9. nodejs入门API之fs模块

    fs模块下的类与FS常量 fs模块下的主要方法 fs的Promise API与FileHandle类 一.fs模块下的类 1.1 fs.Dir:表示目录流的类,由 fs.opendir().fs.op ...

  10. List · leetcode-24. 交换相邻节点

    题面 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the va ...