'Python' Category

  • "Leave password blank if dont want to change" in a django admin field

    April 27, 2012

    Django is a complicated but useful python web-framework, comparing to light weight frameworks like Bottle, web.py. I recently switch from Bottle, making use of it's powerful admin site to build a Email Account Management system. An AdminSite offer interfaces to manage databases. It includes traditional authentications, permissions, data display and POST saving mechanisms, etc. , [...]

  • uWSGI的几个使用技巧

    November 24, 2011

    uWSGI基本上是python做web服务的不二选择了,但似乎项目的开发者比较热衷于其新功能开发,对其使用文档还是相当缺乏的。 安装: Ubuntu 添加ppa,安装。 目前只有Lucid, Maverick, Natty几个版本还需要添加,以后版本都在官方原,直接apt-get install即可。 1 2 3 add-apt-repository ppa:uwsgi/release apt-get update apt-get install uwsgi-python Debian 目前Debian sid已经收录了uwsgi,一般服务器安装的testing/stable,只能自己修改apt的配置,从sid里面port过来。 注意:以下操作可能毁掉您的整个debian包系统。以下命令仅在Debian wheezy,即目前的testing运行测试安装过,如果你的是squeeze,自己修改01all那里 Pin: release a=squeeze 一行,但不保证能够正常运行。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo "deb http://ftp.tw.debian.org/debian sid main non-free contrib" >> /etc/apt/sources.list cat > /etc/apt/preferences.d/01all << [...]

  • Python多重继承的异构构造器

    November 23, 2011

    What 在Python里面,如果你使用上Qt,SQLAlchemy,Twisted之类各种大型类库时候,有时候多重继承Multiple Inheritance是个简单的解决方法,但是多重继承的复杂性总容易造成误解和疑惑。 一般“常识”说,使用super访问父类的属性/方法,这种说法在多重继承里面是不成立的,多重继承的类并没有父类的概念(There is no superclass in a MI world)。类似的博客在过去几年被人写了无数遍了,因为过去版本里面python官方文档对super的解释非常有限而且有误导解释,直到2.6以后的文档,才详细说明了super在单继承和多继承的两种不同工作方式。当时苦逼的程序员甚至不得不去翻看Python源码才搞清楚是什么回事。以致几年来很多人对python的多重继承保持怀疑态度。 Python多重继承使用Method Resolution Order的动态算法来解决一个方法名的调用顺序,mro其实说来简单,就是一个深度优先的继承列表,很易理解,但随之来的是遇到互不相同的构造器__init__参数的问题。 Codepad运行结果:http://codepad.org/qQNiMzBl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class A(object): def __init__(self, arg1): print "init func in A, with arg1 '%s'" % arg1 super(A, self).__init__()   class B(object): def __init__(self, arg1, [...]

  • 以Python+Bottle框架作为jQuery.Uploadify控件的后端

    September 5, 2011

    jQuery.Uploadify是个功能强大的文件上传控件,基于Flash(3.0兼容HTML5),作为前端使用是个不错的选择。 目前的前端技术中,能够实现跨域POST的成熟方案一般只有借助flash,这也是Uploadify的特点吧。 Uploadify的官方提供的是一个php的后端,后端需要做的事情相当简单,就是把POST的临时数据存放到指定路径。 有不少中文资料是把uploadify作为.net程序的上传控件,原理其实一样,所以用Python实现也是很简单的。 其实那些实现的后端(包括官方实现的php)都有安全性问题,比如限制文件类型、指定上传路径都可以在远端控制,上传个木马之类会导致服务器被入侵。当然这是因为他们提供的例子是半成品,实际使用起码应该加上session id的验证之类,因为上传时候数据是由Flash发出的,和浏览器的Session无关。 下面的Python例子中,则就如还在beta阶段的uploadify 3的开发日志讨论的,暂时忽略掉folder参数。 完整的例子代码可在我的仓库下载。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import bottle bottle.debug(True) [...]

  • Trac 运营环境最简易配置(uwsgi+nginx)

    August 13, 2011

    @felixonmars 点名要一份trac的最简易配置步骤,不过其实trac这样一个geek东西,即使按本文配置好跑起来后也依然是一套半成品,还有很多东西需要去看文档去配置,就像awesome, vim等,在一段陡峭的学习曲线之前,都不怎么好用。 所以其实本文也不会降低多少门槛,大概比直接看官方的guide安装的时候少点压力,但不一定是好事。 此前有在公司技术部内推广使用trac做项目管理的想法,做过了解,不过因为各种原因还是用不下来。赖勇浩的博客有一系列文章介绍trac,也包括了在公司环境使用的经验。 Trac作为一套web应用,其实部署步骤和此前介绍的MoinMoin, Mercurial很类似的。 以下步骤介绍安装包含多国语言的基本trac环境。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 [...]

  • Vimrepress Having Big Changes (Updated Beta Version)

    May 13, 2011

    Update Vimpress 已经升级到2.x版本,使用、配置都有改进,请关注在vim.org的插件页面: Vimpress had been updated to 2.x, usage and configurations are now different, read the officle page in vim.org: http://www.vim.org/scripts/script.php?script_id=3510 Hi guys, glad to know that many of you are having vimpress forked to improved it's power, while i'm also doing the same thing. I'm working on a version that had been refactored [...]

  • nginx+uwsgi架设自用Mercurial hg仓库:hgwebdir

    March 11, 2011

    Mercurial/hg是分布式版本管理工具之一,如google code等公共代码服务都直接支持。不过公共服务通常都有如公开开源、空间大小等等的限制,如果特殊情况,需要自己在VPS上假设一套,也不太难。 如果单独一个hg仓库,直接运行hg serve就已经是在线仓库,用nginx做的http_proxy反向代理就够了,各种功能齐全。但是要host多个hg仓库,需要用hgwebdir。 Ubuntu当中安装的mercurial已经包含了架设仓库所用的程序,包括cgi/fcgi/wcgi的,主接口在/usr/share/doc/mercurial-common/examples下的hgwebdir.(ws|(f)c)gi。虽然其中的wsgi有效的只有三行。 Mercurial官方Wiki上主要介绍了用apache2来跑cgi方式的仓库,但是作为python的程序,wsgi才是最原生的嘛。用Nginx+uwsgi的方式,资源占用、响应效率等,都要比apache + cgi靠谱。 Nginx+uwsgi的方式很普遍,参见本博客之前的MoinMoin 与 Nginx, fastcgi 与 uwsgi 的配置一文。 但是因为uwsgi处于快速开发过程当中,ppa的打包也还不十分成熟,里面的路径跟以前有些不一样了。 add-apt-repository ppa:nginx/stable add-apt-repository ppa:uwsgi/release apt-get update apt-get install nginx uwsgi-python python-virtualenv mercurial   mkdir -p /var/www/hgwebdir virtualenv /var/www/hgwebdir/python-home   cat >/var/www/hgwebdir/hgwebdir_wsgi.py << EOF from mercurial import demandimport; demandimport.enable() from mercurial.hgweb.hgwebdir_mod import hgwebdir   class hgwebdir_with_fixed_staticfile(hgwebdir): def __call__(self, env, respond): [...]

  • Manage multiple wordpresses with new Vimpress

    March 10, 2011

    Update Vimpress 已经升级到2.x版本,使用、配置都有改进,请关注在vim.org的插件页面: Vimpress had been updated to 2.x, usage and configurations are now different, read the officle page in vim.org: http://www.vim.org/scripts/script.php?script_id=3510 If you noticed Vimpress before and got more than one wordpress to write, must glad now to know this VimRePressed support multiple configs and swith freely. The new VimRePressed (Version: 1.2.0) Changes: Add: [...]

Page optimized by WP Minify WordPress Plugin

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org