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('< > & "') '< > & "' ### 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