显示标签为“python”的博文。显示所有博文
显示标签为“python”的博文。显示所有博文

2009-04-27

使用pdb调试python程序

python程序也可以实现类似于c/c++的单步调试功能,而python中的调试模块pdb类似于c中的gdb(常用命令等),可以进行方便的调试。
下面是一个例子(debug_demo.py):

#!/usr/bin/env python
_debug = True

def debug_demo(val):
if _debug:
import pdb
pdb.set_trace()           #引入相关的pdb模块
if val < 10:
print "less than 10"
elif val<20:
print "less than 20, but big than 10"
else:
print "other condition"

在命令行中按如下方法进行调试:

python debug_demo.py
这时会进入类似于gdb的界面,启动相关的调试

> /home/tower/test/python/debug.py(9)debug_demo()
-> if val <= 10:
(Pdb)

可以输入h来查看可用的命令集(很类似于gdb)。
单步调试对于某些情况下的调试是非常有帮助的。

更多参考:

2009-04-20

Python的敏捷工具箱[转]

Python的敏捷工具箱[转]

 转处http://billlangjun.blogspot.com/2008/01/python.html

Pycon2006的有一篇Paper是介绍如果使用敏捷的开发和测试方法进行Python开发。其中,包括的内容是我到目前为止看到的最全面的Agile Toolkits介绍:
1. Source control management ( subversion) and issue tracking/project management ( Trac)
2. TDD/unit testing ( py.test,  doctest,  nose,  TestOOB)
3.  unit tests as documentation ( doctest and epydoc)
4. code coverage (the  coverage module)
5. source code analysis ( pylint,  pyflakes, pychecker)
6. acceptance/functional testing ( PyFit/FitNesse,  TextTest)
7. performance testing ( FunkLoad,  twill)
8. Web application testing with  Selenium and twill
9. Scripting Selenium tests using a  Twisted-based server
10. Python package management ( distutils,  setuptools,  Cheesecake index)
11. Continuous integration and 'smoke test' ( buildbot )

2009-04-16

django相关资源整理

  1. 编码风格 http://www.python.org/dev/peps/pep-0008/
  2. django相关资源 http://vik.haoluobo.com/trac/wiki/Django
2里面有较丰富的资源网址。

2009-04-07

[ZZ]Zen of Python

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2009-04-03

django+tiny_mce使用到的html转换

这里主要涉及2个方面的问题:
  1. tiny_mce生成的HTML是以html escape的形式存在的,即把<存为&lt, 把空格存为&nbsp;等
  2. 在显示时,通常分为两种显示:一种是去除格式的摘要显示,这时就要将&nbsp;这样的字符转换为空格重新显示,而django中没有相应的filter;第二种是全格式的显示,即WYSIWYG,显示所有的HTML实体
在开发过程中,2问题中的第二点是比较容易解决的,本文主要介绍第一点的解决,思路有2个,一个是在view中进行转换,另一种是在template中转换,相比而言,后者则通用性、简洁性方便好了很多,也符合DRY原则,因为只需要一次建立自己的filter而若干次调用即可。

具体的解决方法为:
  1. 在相应的应用下建立templatetags文件夹,其下建立__init__.py(表明此为一module)和mytags.py
  2. 在mytags.py文件中写下如下代码,生成自己的filter
  3. 最后在template中{% load mytags %} {content|escape}
代码如下:

@register.filter
def unescape(value):
from django.utils.text import unescape_entities
return unescape_entities(value)