文章关键字 ‘django’

Digg和Twitter都在迁移数据库到Cassandra

2009年12月11日,星期五

NoSQL运动越来越热烈了,掀起了Web 2.0技术从前端到后台一又一的高潮。。。。。

Twitter计划迁移数据库到Cassandra

Digg已经在转换数据到Cassandra

如果说我还说漏了一个Web 2.0网站,那么Cassandra就是Facebook开发的。。。

Cassandra是一个K/V数据库,但差不多也就是行/列混杂式数据库。。。。用它的设计者Jeff Hammerbacher的话说,Cassandra就是 a BigTable data model running on a Amazon Dynamo-like infrastructure.

Django 1.2也将支持Cassandra

想起来微软和Sun真囧。强推XML那么多年,结果文档式数据库最后还是被CouchDB抢了风头,JSON也成了AJAX那个X的de facto格式。结果最后大家都NoSQL不玩SQL了,大家都玩K/V了。

所以说云计算并不是炒作概念,还是很有一些新的革莫道不消魂命性的东西在悄悄进化的。

via

Matheatre - Unit Circle Trigonometry

2009年11月22日,星期日

很囧的一首歌。歌词官网有,

In the style of:
Gilbert & Sullivan - When I Was A Lad

Zero, π over 6 and more,
The next one to know is π over 4,
Then π over 3 and π over 2,
Are all of the radians I’ll tell to you!

r is 1, circumference is π times 2

O, thirty, forty-five, sixty, ninety!
That’s why I love unit circle Trigonometry.

O, thirty, forty-five, sixty, ninety!
That’s why I love unit circle Trigonometry.

The x’s are 1, square root of 3-
Over 2 horizontal they must be.
Square root of 2 over 2 and next are,
One half and zero the stately x.

Secretly they’re cosine but that’s coming next.

O, thirty, forty-five, sixty, ninety!
That’s why I love unit circle Trigonometry.

O, thirty, forty-five, sixty, ninety!
That’s why I love unit circle Trigonometry.

Zero, one half, and square-root of 2-
Over 2, that’s "y" I’m telling you,
Square-root of 3 over 2 and 1.
Sine is the height at any radian,

The square-root of 4 over 2 is 1!

O, thirty, forty-five, sixty, ninety!
That’s why I love unit circle Trigonometry.

O, thirty, forty-five, sixty, ninety!
That’s why I love unit circle Trigonometry.

Now there are three other quadrants to
The unit circle that is in your view,
I’m not going to sing them,
You can work them through,
And while you are doing that I’ll say to you:
r COSINE is X!…And r SINE is Y!
So we all love unit circle Trigonome - tri!

r COSINE is X!…And r SINE is Y!
So we all love unit circle Trigonome - tri!

btw, thesixtyone.com是Django做的网站哦。。。

Django里send_mail如何兼容GB2312的SMTP防止出现中文乱码

2008年08月21日,星期四

#urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
    (r'^test/$', 'Zhai.core.views.test'),
)


#views.py from django.http import HttpResponse def test(request): from django.core.mail import EmailMessage email = EmailMessage(request.GET['s'], request.GET['c'].encode('gbk'),'sender@example.com', ['address1@another.com']) email.encoding = 'gb2312' return HttpResponse(email.send(fail_silently=False))

django的编码是比较囧的
标准python shell里:

ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> u'我囧'
u'\u6211\u56e7'

对比下Django的shell:


Python 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> u'我囧'
u'\xce\xd2\x87\xe5'
>>>

想了一下,Guido规定Python的默认编码是ascii,但是这里的ascii是range(12 8) 的。这是最大的失误。

Apache2+mod_fastcgi+Django备忘

2008年08月19日,星期二

LoadModule rewrite_module modules/mod_rewrite.so

LoadModule fastcgi_module modules/mod_fastcgi.so
FastCGIExternalServer /home/django/mysite.fcgi -host 127.0.0.1:8882

<VirtualHost *:80>
    
#这里很重要。。。我配置了2天就是没有把这里的权限配置好
    
<Directory /home/django/#注意这里要chmod 777 -R
      Options FollowSymLinks
      AllowOverride All
      Order deny,allow
      Allow from all
    
</Directory>

    ServerAdmin foo@bar.com
    DocumentRoot /home/django/
    ServerName 域名
    Alias /assets /home/django/static/assets
    RewriteEngine On #注意这里重写规则的 / 很复杂,不能错一个。
    RewriteRule ^/(assets.*)$ /$1 [QSA,L,PT]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /mysite.fcgi/$1 [L]
    ErrorLog /var/log/web/error_log #文件要存在,要有读写权限
    CustomLog /var/log/web/access_log common
</VirtualHost>

一些细节弄了我很久。。。备忘。。。万恶的Apache配置文件语法。其中mod_rewrite.somod_fastcgi.so难得重新编译了。。。直接去deb包里提取一个现成的复制过去。。。当然你的Apache要支持DSO才行。

I am preparing to code a long-running project

2008年05月4日,星期日

上次说了要写一个社论坛,要做有什么新的特色和独特的功能呢?

  • 采用django ORM/MVC开发,extra部件WSGI兼容
  • 系统在空间和时间上都是scalable的
  • 不是平面/列表的信息堆积,而是结构化、流程化的
  • 承上条,避免环路
  • 多种Web I/O接口
  • 对一个具体的东西支持metadata
  • 界面接单化,输出模板化、操作傻瓜化、用户体念一定要门槛低、0时间上手,不需要manual

形而上的东西YY完了,YY下具体timeline和技术细节

参照LividProject Babel,和KillkeeperflatGrid

  • SEO
  • 热插拔模块
  • 和n多东西mash-up
  • 不仅仅支持文本、还支持文件、图片、视频。帖子也支持交易、投票、辩论、versioning、concurrent authoring、collaborative creation等等。如果等等太多就用widget实现
  • 傻瓜化的安装过程(easy_install
  • 基于comet的事件流(events flow)和 real-time life-streaming
  • 分离的authentication和用户系统,和XMPP结合的contact list。其实所有模块都是分离的。所谓分离,就是一个模块可以无需任何附加配置就可以移植到不同地方的同一系统;一个模块挂了不影响另外一个模块
  • 一个online IDE,在线 contribute code & debugging。也就是pastebin的一个进化
  • 基于fastcgi的架构,支持apache, lighttpd和IIS
  • 一个支持terminal转义的console,基于comet与服务器IO,甚至实现Web p2p。。。。。囧
  • 完美的access-key支持~~堪比GMail和GReader,一个类似launchy的启动条,整合搜索和命令行功能(例如awk, sort, more, find, 管道等等)。也算是WebOS一个prototype的尝试吧~~再加上一个AI自动对话?自然语言处理?图像拖拽?Voice Command?摄像头互动????囧囧囧~~~

伟大的构想需要伟大的Doer去实现。那么shutup, shutdown Internet & begin CODING!

预计五月份把metadata实现,做一个简陋的 Project Babel Clone出来再说~~~

django做的forums搜集

2008年04月19日,星期六

http://code.google.com/p/beydj

http://dev.pocoo.org/

http://code.google.com/p/pforum/

http://sct.sphene.net/wiki/show/Community/

http://code.djangoproject.com/wiki/ForumAppsComparison

最近闲得蛋痛,想凭本人一人之力做一个new fansion、cutting-edge、full-stack的forum,基于django。除了传统加入很多新鲜元素例如OpenID, OAuth, pluggable over Web Service, metadata, tagging, groups, 乃至绑定域名。

不知道有没有人出来阻止我?

借用django.utils.autoreload实现python程序模块热插拔

2008年03月23日,星期日

现代Web服务器应用一个重要的指标是“Uptime”,也就是在线时间。例如一个大型论坛,升级一个功能,如果需要更新配置而重启Web服务器,可能服务就暂停几秒种,几秒种的损失对于Web来说是相当大的,所以我们需要实现模块热插拔。升级一个模块之后,系统自动把内存里的代码更新为最新文件系统上的版本。再举一个例子,假如一个关键业务是一个IM聊天机器人,那么升级一个机器人的功能就不可能让机器人下线再上线。软件系统模块“热插拔”就像硬件的plug 'n' play 一样,方便了开发流程,把文件上传到服务器FTP,网站就自动更新。

实现思路很简单,犯不着去调用文件系统的消息和事件,我们用最原始最简单的办法:用一个单独的线程,隔一段时间遍历当前目录下的 .py 和 .pyc 文件,搜集文件时间属性,如果文件时间属性变化了,则 reload(module)

其实django内置的开发服务器已经实现了这个功能,你保存一个工程的代码,服务器在不重启的情况下自动刷新,那么秉着“不要重复造轮子”的理念,我们直接用django现有的模块。网上了一下,发现是这个模块在起作用: django.utils.autoreload 。其实retoreload.py也是源自于CherryPy Project的一段代码。

调用就很简单了

#!/usr/bin/env python
#coding:utf-8

from django.utils import autoreload

def main():
    
print 1

#必须从命令行启动
if "__main__" == __name__:
    
import autoreload
    autoreload.main(main)

保存为 test.py ,命令行运行 python test.py ,显示 1,说明代码没有问题。好,打开 test.py 修改main()函数为 print 2,保存,发现命令行自动刷新了,显示2。

如果上面的代码在工程中应用,可以在main()里写不能down的监护进程,然后所有功能通过 from module import class 来实现,要更新一个功能,只需要更新 module 文件,整个服务在不中断的情况下就会自动升级,我们就成功实现了模块热插拔。

脚本语言的确更加适合Web,如果是C语言开发的系统,每更新一个功能模块都需要编译一次,开发效率真是底下(当然用 .so/ .dll / COM 等技术就比较高级一点了)

[python]解决django使用MySQLdb连接数据库中文出现InterfaceError at (0, '')问题

2008年03月16日,星期日

新建一个django工程

设置数据库为一个普通的MySQL

新建URL映射
from django.conf.urls.defaults import *

urlpatterns = patterns('',
(r'^(?P<sql>[^/]*)','views.show_sql'),
)

写一个简单的view:
from django.http import HttpResponse
from django.db import connection

cursor=connection.cursor()

def show_sql(request, sql):
print sql
global cursor
cursor.execute(sql)
r=cursor.fetchall()
print r
return HttpResponse(str(r))

测试页面 http://127.0.0.1:8000/select 1这个简单页面,得到的返回是 ((1L,),) ,完全正常

刷新一次,或则多次刷新,都要出错:
InterfaceError at /select 1/
(0, '')
Request Method: GET
Request URL: http://127.0.0.1:8000/select 1/
Exception Type: InterfaceError
Exception Value: (0, '')
Exception Location: C:\Python25\Lib\site-packages\MySQLdb\cursors.py
in execute, line 147
Python Executable: C:\Python25\python.exe
Python Version: 2.5.1

Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\django\core\handlers\base.py" in
_real_get_response
81. response = callback(request, *callback_args, **callback_kwargs)
File "E:\Python\exercise\1\views.py" in show_sql
12. cursor.execute(sql)
File "C:\Python25\Lib\site-packages\django\db\backends\util.py" in
execute
19. return self.cursor.execute(sql, params)
File "C:\Python25\Lib\site-packages\MySQLdb\cursors.py" in execute
147. charset = db.character_set_name()

InterfaceError at /select 1/
(0, '')

折腾了几天,发现这样写view就不会出错了,而且中文也不会出错

from django.http import HttpResponse
from django.db import connection

def show_sql(request, sql):
print sql
cursor=connection.cursor()
cursor.execute(sql)
r=cursor.fetchall()
print r
return HttpResponse(str(r))

也就是说把cursor的获得放在view内部就不会出错了,很奇怪。

MySQLdb以前有不能正确中文等编码的bug,不过现在修复了

解决Google Calendar导入URL里icalendar(.ics)的中文乱码问题

2008年03月5日,星期三

这几天一直折腾和Google Calendar同步一个icalendar格式的日历,中文乱码问题多次掺和,非常犯人。最后终于解决了,写几点感受。

  1. 乱码问题要基于字节来研究,所以一个16进制编辑工具是必须的。例如UltraEdit。
  2. 乱码问题涉及多方面:网站源代码文件编码格式;动态页面后台解释器(asp, php, python)是MBCS还是SBCS;数据库查询结果返回编码;返回HTML的编码;返回HTML的BOM;返回HTTP头的Content-Type charset属性。下面一点一点的解说:

网站后台源代码文件:UTF8编码,不要BOM
动态页面后台解释器:最好是MBCS
数据库查询编码:UTF8
返回的HTML编码:UTF8,不要BOM
返回HTTP头:Content-Type: text/calendar; charset=utf-8 这里注意:大小写、连字符(utf-8而不是utf 8) 、空格。

这样可以保证Google Calendar解析含中文的icalendar日历。

还有一种非常郁闷的情况,就是动态页面后台解释器是SBCS的,例如python,那么有可能UTF8转换为GB2312输出文件的时候,每个字符中间多一个 \x00 。我也不知道这是为什么,但是用文本编辑器是没法察觉的,文本编辑器能正常显示中文,但是Google Calendar直接报告:invalid format。用十六进制编辑器才发现这个问题的。

[Python]Google出的Django视频教程

2008年01月19日,星期六

http://youtube.com/watch?v=p-WXiqrzAf8

Google TechTalks
April 26, 2006

Jacob Kaplan-Moss

ABSTRACT
Django is one of the premier web frameworks for Python, and is often compared to Ruby-on-Rails. Jacob is one of the lead developers on Django.