python基础教程第2版 20 项目1:即时标记

简介: 初始版本: 点击(此处)折叠或打开 #!/usr/bin/env python #-*- coding:utf-8 -*- ''' py...
初始版本:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. python基础教程第2版 20 项目1:即时标记
  5. 主要使用正则
  6. 将一段文本转换成有格式的

  7. '''
  8. import sys,re
  9. #找出块
  10. #收集所有行,直到空行,然后返回已经收集的行.
  11. def lines(file):
  12.     for line in file: yield line
  13.     yield '\n'
  14. def blocks(file):
  15.     block=[]
  16.     for line in lines(file):
  17.         #判断是否是空行
  18.         if line.strip():
  19.             block.append(line)
  20.         elif block:
  21.             yield ''.join(block).strip()
  22.             block = []


  23. print(' 测试 ')
  24. title=True
  25. for block in blocks(sys.stdin):
  26.     block = re.sub(r'\*(.+?)\*',r'\1',block)
  27.     if title:
  28.         print('

    ')

  29.         print(block)
  30.         print('')
  31.         title=False
  32.     else:
  33.         print('

    ')

  34.         print(block)
  35.         print('


    ')
  36. print('')
进一步处理:
html.py(主要执行程序)

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''

  4. '''
  5. import sys,re
  6. from util import *
  7. from handler import *
  8. from rules import *

  9. class Parser:
  10.     def __init__(self,handler):
  11.         self.handler=handler
  12.         self.rules=[]
  13.     def addRule(self,rule):
  14.         self.rules.append(rule)
  15.     def parse(self,file):
  16.         self.handler.start('document')
  17.         for block in blocks(file): #应用规则
  18. # print(self.rules)
  19.             for rule in self.rules:
  20.                 if rule.condition(block):
  21.                     last=rule.action(block,self.handler)
  22.                     if last:
  23.                         break
  24.         self.handler.end('document')
  25. class BasicTextParser(Parser):
  26.     #处理文本需要渲染器
  27.     def __init__(self,handler):
  28.         #调用父类的初始化
  29.         Parser.__init__(self,handler)
  30.         #调用父类的方法
  31.         self.addRule(ListRule()) #添加文章格式规则
  32.         self.addRule(ListItemRule())
  33.         self.addRule(TitleRule())
  34.         self.addRule(HeadingRule())
  35.         self.addRule(ParagraphRule())

  36. handler=HTMLRenderer()
  37. parser=BasicTextParser(handler)
  38. #parser.parse(sys.stdin)
  39. parser.parse(sys.stdin)
分割模块:util.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. 这个模块主要实现把文本分成区块.
  5. '''
  6. def lines(file):
  7.     for line in file: #读取文件,生成每一行的迭代器
  8.         yield line
  9.         yield '\n'
  10. def blocks(file):
  11.     block=[]
  12.     for line in lines(file):#从迭代器中取出每一行
  13.         if line.strip(): #如果为空,退出;有内容,添加block列表中
  14.             block.append(line)
  15.         elif block: #如果上面有空行,再次判断,如果block列表中有内容,生成block列表块;没有内容说明是多行空行,忽略.
  16.             yield ''.join(block).strip()
  17.             block=[]
规则模块:rules.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''

  4. '''
  5. from util import *
  6. class Rule:
  7.     """
  8.     Base class for all rules.
  9.     """
  10.     def action(self, block, handler):
  11.         handler.start(self.type)
  12.         handler.feed(block)
  13.         handler.end(self.type)
  14.         return True

  15. class HeadingRule(Rule):
  16.     """
  17.     A heading is a single line that is at most 70 characters and
  18.     that doesn't end with a colon.
  19.     """
  20.     type = 'heading'
  21.     def condition(self, block):
  22.         return not '\n' in block and len(block) = 70 and not block[-1] == ':'

  23. class TitleRule(HeadingRule):
  24.     """
  25.     The title is the first block in the document, provided that it is
  26.     a heading.
  27.     """
  28.     type = 'title'
  29.     first = True

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

  34. class ListItemRule(Rule):
  35.     """
  36.     A list item is a paragraph that begins with a hyphen. As part of
  37.     the formatting, the hyphen is removed.
  38.     """
  39.     type = 'listitem'
  40.     def condition(self, block):
  41.         return block[0] == '-'
  42.     def action(self, block, handler):
  43.         handler.start(self.type)
  44.         handler.feed(block[1:].strip())
  45.         handler.end(self.type)
  46.         return True

  47. class ListRule(ListItemRule):
  48.     """
  49.     A list begins between a block that is not a list item and a
  50.     subsequent list item. It ends after the last consecutive list
  51.     item.
  52.     """
  53.     type = 'list'
  54.     inside = False
  55.     def condition(self, block):
  56.         return True
  57.     def action(self, block, handler):
  58.         if not self.inside and ListItemRule.condition(self, block):
  59.             handler.start(self.type)
  60.             self.inside = True
  61.         elif self.inside and not ListItemRule.condition(self, block):
  62.             handler.end(self.type)
  63.             self.inside = False
  64.         return False

  65. class ParagraphRule(Rule):
  66.     """
  67.     A paragraph is simply a block that isn't covered by any of the
  68.     other rules.
  69.     """
  70.     type = 'paragraph'
  71.     def condition(self, block):
  72.         return True
渲染模块:handler.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''

  4. '''
  5. class Handler():
  6.     def callback(self,prefix,name,*args):
  7.         mothed=getattr(self,prefix+name,None)
  8.         if callable(mothed): #加一层判断,如果对象是可调用的才返回
  9.             return mothed(*args)
  10.     def start(self,name):
  11.         self.callback('start_',name)
  12.     def end(self,name):
  13.         self.callback('end_',name)

  14. class HTMLRenderer(Handler):
  15.     '''
  16.     用于生成HTML的具体处理程序
  17.     HTMLRender内的方法都可以通过超类处理程序的start(),end(),sub()方法来访问,它们实现了用于HTML文档的基本标签.

  18.     '''

  19.     def start_document(self):
  20.         print(' html ')
  21.     def end_document(self):
  22.         print('')

  23.     def start_paragraph(self):
  24.         print('

    ')

  25.     def end_paragraph(self):
  26.         print('


    ')

  27.     def start_heading(self):
  28.         print('

    ')

  29.     def end_heading(self):
  30.         print('')

  31.     def start_list(self):
  32.         print('
    • '
    )
  33.     def end_list(self):
  34.         print(' '
)

     def start_listitem (self ) :
         print ( ' ' )
     def end_listitem (self ) :
         print ( ' ' )

     def start_title (self ) :
         print ( '

')

     def end_title (self ) :
         print ( '' )

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

'' '
getattr(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar ') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
callable(object)
Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a __call__() method.
' ''

#下面是给XML渲染








test_input.txt

点击(此处)折叠或打开

  1. Required Entitlements and Repositories

  2. The packages provided in the following repositories are required to install and configure a functioning Red Hat Enterprise Virtualization environment. When one of these repositories is required to install a package, the steps required to enable the repository are provided in the appropriate location in the Installation Guide or *Self-Hosted Engine Guide*.

  3. Red Hat Enterprise Virtualization Manager

  4. Red Hat Enterprise Virtualization Hypervisor 6 hosts are supported in Red Hat Enterprise Virtualization 3.6 only in clusters with 3.5 compatibility. Clusters with 3.6 compatibility support Red Hat Enterprise Virtualization Hypervisor 7 hosts and Red Hat Enterprise Linux 7 hosts only.

  5. The following Red Hat Enterprise Virtualization features are not supported:
  6. - Hot-plug CPU
  7. - SPICE display
  8. - SmartCard
  9. - Sound device
  10. - Watchdog

  11. ntegration with OpenStack Networking (Neutron), *OpenStack* Image (Glance), and OpenStack Volume (Cinder)
执行测试:

点击(此处)折叠或打开

  1. [t@bjb0541 untitled]$ ./B20util.py test_input.txt |tee > 2.html
  2. html>head>title>测试/title>body>
  3. h1>
  4. Required Entitlements and Repositories
  5. /h1>
  6. p>
  7. The packages provided in the following repositories are required to install and configure a functioning Red Hat Enterprise Virtualization environment. When one of these repositories is required to install a package, the steps required to enable the repository are provided in the appropriate location in the Installation Guide or em>Self-Hosted Engine Guide/em>.
  8. /p>
  9. p>
  10. Red Hat Enterprise Virtualization Manager
  11. /p>
  12. p>
  13. Red Hat Enterprise Virtualization Hypervisor 6 hosts are supported in Red Hat Enterprise Virtualization 3.6 only in clusters with 3.5 compatibility. Clusters with 3.6 compatibility support Red Hat Enterprise Virtualization Hypervisor 7 hosts and Red Hat Enterprise Linux 7 hosts only.
  14. /p>
  15. p>
  16. The following Red Hat Enterprise Virtualization features are not supported:
  17.   - Hot-plug CPU
  18.   - SPICE display
  19.   - SmartCard
  20.   - Sound device
  21.   - Watchdog
  22. /p>
  23. p>
  24. ntegration with OpenStack Networking (Neutron), em>OpenStack/em> Image (Glance), and OpenStack Volume (Cinder)
  25. /p>
  26. /body>/html>



进一步版本执行测试:
[t@bjb0541 makeuphtml]$ python html.py 1.html





目录
相关文章
|
16天前
|
存储 数据可视化 数据挖掘
Python数据分析项目:抖音短视频达人粉丝增长趋势
Python数据分析项目:抖音短视频达人粉丝增长趋势
|
20天前
|
监控 安全 测试技术
如何在实际项目中应用Python Web开发的安全测试知识?
如何在实际项目中应用Python Web开发的安全测试知识?
25 4
|
21天前
|
Python
SciPy 教程 之 Scipy 显著性检验 9
SciPy 教程之 Scipy 显著性检验第9部分,介绍了显著性检验的基本概念、作用及原理,通过样本信息判断假设是否成立。着重讲解了使用scipy.stats模块进行显著性检验的方法,包括正态性检验中的偏度和峰度计算,以及如何利用normaltest()函数评估数据是否符合正态分布。示例代码展示了如何计算一组随机数的偏度和峰度。
22 1
|
21天前
|
BI Python
SciPy 教程 之 Scipy 显著性检验 8
本教程介绍SciPy中显著性检验的应用,包括如何利用scipy.stats模块进行显著性检验,以判断样本与总体假设间的差异是否显著。通过示例代码展示了如何使用describe()函数获取数组的统计描述信息,如观测次数、最小最大值、均值、方差等。
25 1
|
22天前
|
数据采集 数据可视化 数据挖掘
深入浅出:使用Python进行数据分析的基础教程
【10月更文挑战第41天】本文旨在为初学者提供一个关于如何使用Python语言进行数据分析的入门指南。我们将通过实际案例,了解数据处理的基本步骤,包括数据的导入、清洗、处理、分析和可视化。文章将用浅显易懂的语言,带领读者一步步掌握数据分析师的基本功,并在文末附上完整的代码示例供参考和实践。
|
22天前
|
Python
SciPy 教程 之 Scipy 显著性检验 6
显著性检验是统计学中用于判断样本与总体假设间是否存在显著差异的方法。SciPy的scipy.stats模块提供了执行显著性检验的工具,如T检验,用于比较两组数据的均值是否来自同一分布。通过ttest_ind()函数,可以获取两样本的t统计量和p值,进而判断差异是否显著。示例代码展示了如何使用该函数进行T检验并输出结果。
22 1
|
24天前
|
Python
SciPy 教程 之 Scipy 显著性检验 3
本教程介绍Scipy显著性检验,包括其基本概念、原理及应用。显著性检验用于判断样本与总体假设间的差异是否显著,是统计学中的重要工具。Scipy通过`scipy.stats`模块提供了相关功能,支持双边检验等方法。
26 1
|
23天前
|
Python
SciPy 教程 之 Scipy 显著性检验 5
显著性检验用于判断样本与总体假设间的差异是否由随机变异引起,或是假设与真实情况不符所致。SciPy通过scipy.stats模块提供显著性检验功能,P值用于衡量数据接近极端程度,与alpha值对比以决定统计显著性。
23 0
|
25天前
|
机器学习/深度学习 数据处理 Python
SciPy 教程 之 SciPy 插值 3
本教程介绍了SciPy中的插值方法,包括什么是插值及其在数据处理和机器学习中的应用。通过 `scipy.interpolate` 模块,特别是 `Rbf()` 函数,展示了如何实现径向基函数插值,以平滑数据集中的离散点。示例代码演示了如何使用 `Rbf()` 函数进行插值计算。
23 0
|
7天前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!