escape_html()的c扩展模块

Webext的Python项目详细描述


webext是一个提供非常快速的“escape_html()”函数的库。 它被实现为c扩展,运行速度比纯python代码快得多 例如“cgi.escape()”。

安装

$ tar xzf Webext-0.0.1.tar.gz
$ cd Webext-0.0.1
$ sudo python setup.py install

功能

webext提供以下功能:

  • webext.escape_html()
    • 转义HTML特殊字符(<;gt;&;)。
    • 将Unicode转换为具有“utf8”编码的str。
    • 将none转换为空字符串(='')。
  • webext.escape()
    • webext.escape_html()的别名
  • webext.to_str()
    • 将参数转换为str(与str()相同)
    • 将Unicode转换为具有“utf8”编码的str。
    • 将none转换为空字符串(='')。
  • webext.get_encoding()
    • 返回escape_html()和to_str()的默认编码
    • 默认值为“utf8”
  • webext.set_编码(arg)
    • 设置escape_html()和_str()的默认编码

示例

### import escape_html() and to_str()
>>> from webext import escape_html, to_str

### escape_html() escapes html special characters
>>> escape_html('< > & "')
'&lt; &gt; &amp; &quot;'

### to_str() and escape_html() convert unicode into str with 'utf8' encoding
>>> to_str(u'\u65e5\u672c\u8a9e')
'\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e'
>>> escape_html(u'\u65e5\u672c\u8a9e')
'\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e'

### to_str() and escape_html() convert None into empty string
### (this is more desirable than str() for web application)
>>> to_str(None)
''
>>> escape_html(None)
''

与Tenjin一起使用

Tenjin是一个非常快速的模板引擎 用纯python实现。使用webext可以使tenjin更快。

import tenjin
from tenjin.helpers import *
from webext import to_str, escape    # use webext's to_str() and escape() instead of tenjin's
engine = tenjin.Engine()
context = { 'items': ['<AAA>', 'B&B', '"CCC"'] }
print engine.render('example.pyhtml', context)

tenjin的基准现在支持webext。下面是一个例子 Mac OS X 10.6、Intel Core Duo 2GHz、Tenjin 0.9.0上的基准测试结果。 这表明webext促进了tenjin,特别是html转义。

## without html escaping
$ python bench.py -n 10000 tenjin tenjin-
webext
import tenjin ... done. (0.001740 sec)
import webext ... done. (0.000466 sec)
*** loading context data (file=bench_context.py)...
*** start benchmark
*** ntimes=10000
                                    utime     stime     total      real
tenjin                             3.8100    0.0400    3.8500    3.8584
tenjin-webext                      2.5000    0.0300    2.5300    2.5367

## with html escaping
$ python bench.py -e -n 10000 tenjin tenj
in-webext
import tenjin ... done. (0.001580 sec)
import webext ... done. (0.000459 sec)
*** loading context data (file=bench_context.py)...
*** start benchmark
*** ntimes=10000
                                    utime     stime     total      real
tenjin                             7.2100    0.0500    7.2600    7.2935
tenjin-webext                      2.9800    0.0400    3.0200    3.0476

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用户界面java,使用gui连接到另一台计算机/服务器的文件系统   运行sbt的ubuntu返回错误:“javahome需要<path>参数”   java如何在Android中处理许多ImageView而不出现内存问题?   查询中非法字符的java相同URL失败   安卓取消引用可能会产生“java”。lang.NullPointerException'   java中的indexoutofboundsexception“java.lang.ArrayIndexOutOfBoundsException”错误   xml Java将dom保存到文件>文件在程序结束后由另一个进程打开   Java的垃圾收集器是如何工作的?   Java如何筛选值(列表)   java处理字符串我怎样才能像在真实的书籍中一样在上面部分生成“小数字”呢?   java SonarQube是否有一个API来获取所有项目分析的一部分?   java startActivity(intent)什么都不做   JAVAutil。扫描器类Java   java如何从Firebase更新电子邮件?UpdateMail方法已被弃用   java Hibernate。如何正确组织带有注释的onetomany关系?   在java中获得卷标和驱动器号之间的映射(而不是FileSystemView)的解决方法是什么   java查找文件的路径