鱼C论坛

 找回密码
 立即注册
查看: 2486|回复: 3

python基础教程-项目1即时标记

[复制链接]
发表于 2015-1-8 17:28:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ~风介~ 于 2015-1-8 18:51 编辑

python报错.png 运行报错,求教大神这个问题怎么解决!
  1. class Handler:
  2.     def callback(self,prefix,name,*args):
  3.         method=getattr(self,prefix+name,None)
  4.         if callable(method):return method(*args)

  5.     def start(self,name):
  6.         self.callback('start_',name)

  7.     def end(self,name):
  8.         self.callback('end_',name)

  9.     def sub(self,name):
  10.         def substitution(match):
  11.             result=self.callback('sub_',name,match)
  12.             if result is None:match.group(0)
  13.             return result
  14.         return substitution
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-1-8 18:53:33 | 显示全部楼层
这边没报错啊!有完整的代码?另外,你要干什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-8 20:43:11 | 显示全部楼层
~风介~ 发表于 2015-1-8 18:53
这边没报错啊!有完整的代码?另外,你要干什么?

#handlers.pyclass Handler:
    def callback(self,prefix,name,*args):
        method=getattr(self,prefix+name,None)
        if callable(method): return method(*args)

    def start(self,name):
        self.callback('start_',name)

    def end(self,name):
        self.callback('end_',name)

    def sub(self,name):
        def substitution(match):
            result=self.callback('sub_',name,match)
            if result is None: match.group(0)
            return result
        return substitution

class HTMLRenderer(Handler):

    def start_document(self):
        print('<html><head><title>...</title></head><body>')

    def end_document(self):
        print('</body></html>')

    def start_paragraph(self):
        print('<p>')

    def end_paragraph(self):
        print('</p>')

    def start_heading(self):
        print('<h2>')

    def end_heading(self):
        print('</h2>')

    def start_list(self):
        print ('<ul>')

    def end_list(self):
        print('</ul>')

    def start_listitem(self):
        print('<li>')

    def end_listitem(self):
        print('</li>')

    def start_title(self):
        print('<h1>')

    def end_title(self):
        print('</h1>')

    def sub_emphasis(self,match):
        return '<em>%s</em>' % match.group(1)

    def sub_url(self,match):
        return '<a href="%s">%s</a>' % (match.group(1),match.group(1))

    def sub_mail(self,match):
        return '<a href="mailto:%s">%s</a>' %(match.group(1),match.group(1))

    def feed(self,data):
        print(data)

-------------------
#rules.py
class Rule:
    def action(self,block,handler):
        handler.start(self.type)
        handler.feed(block)
        handler.end(self.type)
        return True

class HeadingRule(Rule):
    type='heading'
    def condition(self,block):
        return not '\n' in block and len(block)<=70 and not block[-1]==':'

class TitleRule(HeadingRule):
    type='title'
    first=True

    def condition(self,block):
        if not self.first:return False
        self.first=False
        return HeadingRule.condition(self,block)

class ListItemRule(Rule):
    type='listitem'
    def condition(self,block):
        return block[0]=='-'
    def action(self,block,handler):
        handler.start(self.type)
        handler.feed(block[1:].strip())
        handler.end(self.type)
        return True
class ListRule(ListItemRule):
    type='list'
    inside=False
    def condition(self,block):
        return True
    def action(self,block,handler):
        if not self.inside and ListItemRule.condition(self,block):
            handler.start(self.type)
            self.inside=True
        elif self.inside and not ListItemRule.condition(self,block):
            handler.end(self.type)
            self.inside=False
        return False

class ParagraphRule(Rule):
    type='paragraph'
    def condition(self,block):
        return True

------------
#util.py
def lines(file):
    for line in file:yield line
    yield '\n'

def blocks(file):
    block=[]
    for line in lines(file):
        if line.strip():
            block.append(line)
        elif block:
            yield ''.join(block).strip()
            block=[]

--------------------
#makeup.py
import re,sys
from handlers import *
from util import *
from rules import *

class Parser:
    def __init__(self,handler):
        self.handler=handler
        self.rules=[]
        self.filters=[]

    def addRule(self,rule):
        self.rules.append(rule)

    def addFilter(self,pattern,name):
        def filter(block,handler):
            return re.sub(pattern,handler.sub(name),block)
        self.filters.append(filter)

    def parse(self,file):
        self.handler.start('document')
        for block in blocks(file):
            for filter in self.filters:
                block=filter(block,self.handler)
            for rule in self.rules:
                if rule.condition(block):
                    last=rule.action(block,self.handler)
                    if last:break
        self.handler.end('document')


class BasicTextParser(Parser):
    def __init__(self,handler):
        Parser.__init__(self,handler)
        self.addRule(ListRule())
        self.addRule(ListItemRule())
        self.addRule(TitleRule())
        self.addRule(HeadingRule())
        self.addRule(ParagraphRule())

        self.addFilter(r'\*(.+?)\*', 'emphasis')
        self.addFilter(r'(http://[\.a-z0-9A-Z/]+)', 'url')
        self.addFilter(r'([\.a-zA-Z]+@[\.a-zA-Z]+[a-zA-Z]+)','mail')

handler=HTMLRenderer()
parser=BasicTextParser(handler)
parser.parse(sys.stdin)

---------------
#test_input.txt
Welcome to World Wide Spam. Inc.

These are the corporate web pages of *World Wide Spam*, Inc. We hope you find your stay enjoyable, and that you will sample many of our products.

A short history of the company

World Wide Spam was started in the summer of 2000.The business concept was to ride the dot-com wave and to make money both through bulk email and by selling canned meat online.

After receiving several complaints from customers who weren't satisfied by their bulk email,World Wide Spam altered their profile, and focused 100% on canned goods.Today, they rank as the world's 13,892nd online supplier of SPAM.

Destinations

From this page you may visit several of our interesting web pages:

        -What is SPAM? (http://wwspam.fu/whatisspam)
       
        -How do they make it? (http://wwspam.fu/howtomakeit)
       
        -Why should I eat it? (http://wwspam.fu/whyeatit)

How to get in touch with us

You can get in touch with us in *many* ways: By phone(555-1234), by email(wwspam@wwspam.fu) or by visiting our customer feedback page (http://wwspam.fu/feedback).


---------------------------
这是完整的代码,在公司弄得报错来着,我到家又执行了次好了。。。。。!不过还是谢谢你!~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-11 15:45:41 | 显示全部楼层
:cry:cry:cry:cry
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-14 08:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表