python基础教程第2版 20 项目1:即时标记
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
- python基础教程第2版 20 项目1:即时标记
- 主要使用正则
- 将一段文本转换成有格式的
-
- '''
- import sys,re
- #找出块
- #收集所有行,直到空行,然后返回已经收集的行.
- 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 = []
-
-
- print(' 测试 ')
- title=True
- for block in blocks(sys.stdin):
- block = re.sub(r'\*(.+?)\*',r'\1',block)
- if title:
- print('
')
- print(block)
- print('')
- title=False
- else:
- print('
')
- print(block)
- print('
- print('')
html.py(主要执行程序)
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
-
- '''
- import sys,re
- from util import *
- from handler import *
- from rules import *
-
- class Parser:
- def __init__(self,handler):
- self.handler=handler
- self.rules=[]
- def addRule(self,rule):
- self.rules.append(rule)
- def parse(self,file):
- self.handler.start('document')
- for block in blocks(file): #应用规则
- # print(self.rules)
- 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())
-
- handler=HTMLRenderer()
- parser=BasicTextParser(handler)
- #parser.parse(sys.stdin)
- parser.parse(sys.stdin)
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
- 这个模块主要实现把文本分成区块.
- '''
- def lines(file):
- for line in file: #读取文件,生成每一行的迭代器
- yield line
- yield '\n'
- def blocks(file):
- block=[]
- for line in lines(file):#从迭代器中取出每一行
- if line.strip(): #如果为空,退出;有内容,添加block列表中
- block.append(line)
- elif block: #如果上面有空行,再次判断,如果block列表中有内容,生成block列表块;没有内容说明是多行空行,忽略.
- yield ''.join(block).strip()
- block=[]
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
-
- '''
- from util import *
- class Rule:
- """
- Base class for all rules.
- """
- def action(self, block, handler):
- handler.start(self.type)
- handler.feed(block)
- handler.end(self.type)
- return True
-
- class HeadingRule(Rule):
- """
- A heading is a single line that is at most 70 characters and
- that doesn't end with a colon.
- """
- type = 'heading'
- def condition(self, block):
- return not '\n' in block and len(block) = 70 and not block[-1] == ':'
-
- class TitleRule(HeadingRule):
- """
- The title is the first block in the document, provided that it is
- a heading.
- """
- 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):
- """
- A list item is a paragraph that begins with a hyphen. As part of
- the formatting, the hyphen is removed.
- """
- 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):
- """
- A list begins between a block that is not a list item and a
- subsequent list item. It ends after the last consecutive list
- item.
- """
- 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):
- """
- A paragraph is simply a block that isn't covered by any of the
- other rules.
- """
- type = 'paragraph'
- def condition(self, block):
- return True
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
-
- '''
- class Handler():
- def callback(self,prefix,name,*args):
- mothed=getattr(self,prefix+name,None)
- if callable(mothed): #加一层判断,如果对象是可调用的才返回
- return mothed(*args)
- def start(self,name):
- self.callback('start_',name)
- def end(self,name):
- self.callback('end_',name)
-
- class HTMLRenderer(Handler):
- '''
- 用于生成HTML的具体处理程序
- HTMLRender内的方法都可以通过超类处理程序的start(),end(),sub()方法来访问,它们实现了用于HTML文档的基本标签.
-
- '''
-
- def start_document(self):
- print(' html ')
- def end_document(self):
- print('')
-
- def start_paragraph(self):
- print('
')
- def end_paragraph(self):
- print('
-
- def start_heading(self):
- print('
')
- def end_heading(self):
- print('')
-
- def start_list(self):
- print('
- '
- def end_list(self):
- 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
点击(此处)折叠或打开
- Required Entitlements and Repositories
-
- 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*.
-
- Red Hat Enterprise Virtualization Manager
-
- 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.
-
- The following Red Hat Enterprise Virtualization features are not supported:
- - Hot-plug CPU
- - SPICE display
- - SmartCard
- - Sound device
- - Watchdog
-
- ntegration with OpenStack Networking (Neutron), *OpenStack* Image (Glance), and OpenStack Volume (Cinder)
点击(此处)折叠或打开
- [t@bjb0541 untitled]$ ./B20util.py test_input.txt |tee > 2.html
- html>head>title>测试/title>body>
- h1>
- Required Entitlements and Repositories
- /h1>
- p>
- 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>.
- /p>
- p>
- Red Hat Enterprise Virtualization Manager
- /p>
- p>
- 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.
- /p>
- p>
- The following Red Hat Enterprise Virtualization features are not supported:
- - Hot-plug CPU
- - SPICE display
- - SmartCard
- - Sound device
- - Watchdog
- /p>
- p>
- ntegration with OpenStack Networking (Neutron), em>OpenStack/em> Image (Glance), and OpenStack Volume (Cinder)
- /p>
- /body>/html>
进一步版本执行测试:
[t@bjb0541 makeuphtml]$ python html.py 1.html
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
动手实践虚拟网络 - 每天5分钟玩转 OpenStack(10)
本节将演示如何在实验环境中实现下图所示的虚拟网络 配置 Linux Bridge br0 编辑 /etc/network/interfaces,配置 br0。 下面用 vimdiff 展示了对 /etc/network/interfaces 的修改 有两点需要注意: 1. 之前宿主机的 IP 是通过 dhcp 配置在 eth0 上的;创建 Linux Bridge 之后,IP 就必须放到 br0 上了 2. 在 br0 的配置信息中请注意最后一行 “bridge_ports eth0”,其作用就是将 eth0 挂到 br0 上 重启宿主机,查看 IP 配置,可以看到 IP 已经放到 br0 上了 # ifconfig br0 Link encap:Ethernet HWaddr 00:0c:29:8d:ec:be inet addr:192.168.111.107 Bcast:192.168.111.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe8d:ecbe/64 Scope:Link UP BR...
- 下一篇
Alluxio简介
一、Alluxio是什么? Alluxio是一个基于内存的分布式文件系统,它是架构在底层分布式文件系统和上层分布式计算框架之间的一个中间件,主要职责是以文件形式在内存或其它存储设施中提供数据的存取服务。 Alluxio的前身为Tachyon。 二、Alluxio应用场景 Alluxio居于传统大数据存储(如:Amazon S3,Apache HDFS和OpenStack Swift等)和大数据计算框架(如Spark,Hadoop Mapreduce)之间,如下图所示: 在大数据领域,最底层的是分布式文件系统,如Amazon S3、Apache HDFS等,而较高层的应用则是一些分布式计算框架,如Spark、MapReduce、HBase、Flink等,这些分布式框架,往往都是直接从分布式文件系统中读写数据,效率比较低,性能消耗比较大。而如果我们将其架构与底层分布式文件系统与上层分布式计算框架之间,以文件的形式在内存中对外提供读写访问服务的话,那么Alluxio可以为那些大数据应用提供一个数量级的加速,而且它只要提供通用的数据访问接口,就能很方便的切换底...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7设置SWAP分区,小内存服务器的救世主