Archive

Archive for the ‘Developer’ Category

How to use google form as a simple survey system

July 20th, 2009

Preview

  • A google form is a simple generally form system. You can use it to generate a form to ask your user some questions and collect the answers into a google spreadsheet automatically. And then you can do farther data analytics with that spreadsheet.
  • Actually, the google form can be used directly. But in some cases, the user want to change the google form’s default behavior to meet their requirements, such as they don’t want it to be redirected to google’s response page which have google’s logo there, they want to customize the design of the google form, etc
  • This scripts is used to focus on these special cases, also it can’t cover all of these cases, but it’s a beginning.

Design

  • I used a iframe to handle the google form’s default response.
  •   <form target="gsurvey_result"></form>
      <iframe name="gsurvey_result" height="1" style="display:none;"></iframe>
  • I used javascript to replace all of the radio button with a image, when the user clicks the image, the value of the radio will be synced directly. The image can be changed with css.
  • I used the javascript to check if the required field has a value. But now there’s nothing to do with the required field without a value.
  • User can define the response text by themselves.

An example

  • Generate a google form with your own google account.
  • Create a new html file, load the script gsurvey.js, gsurvey.css in the html file.
  • <html>
    <head>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
      <link rel="stylesheet" media="screen" href="/static/css/gsurvey.css" />
      <script type="text/javascript" src="/static/js/jquery.gsurvey.js"></script>
    </head>
    <body>
    </body>
    </html>
  • Insert the html code of the google form. You can also define a element holds response text. You can check the code in the attachments.

javascript

Performance of str+str and ‘‘.join()

April 28th, 2009
>>> a = 'a' * 100000
>>> def t(a):
>>>     b = ''
>>>     for i in a:
>>>         b = b + '|' + i
>>>     return b
>>> 
>>> time c = t(a)
>>> time c = '|'.join(a)

In my notebook ( Intel(R) Core(TM)2 CPU T5200 @ 1.60GHz, 1.5G Mem):

The result of time c=t(a):
CPU times: user 2.90 s, sys: 0.00 s, total: 2.90 s
Wall time: 3.07 s

The result of time c = ‘|’.join(a):
CPU times: user 0.01 s, sys: 0.00 s, total: 0.01 s
Wall time: 0.01 s

If I changed a from 100000 to 1000000, c=t(a) can’t give me a result in more than 5 minutes while the other one give me the result in 0.09s.

join is much faster than str + str. str + str need to create new strings all the time, but join doesn’t. I guess “join” maybe do something on the memory level.

Developer, Python

来自 “The Federated States of Micronesia” 的顾客

April 1st, 2009

今天我们站点上出现了个跟国名有关的错误。一个来自”The Federated States of Micronesia”的老兄试图给我们站点留言。我们的IP2Location解析了他的ip并且得到了正确的国家名称”MICRONESIA, FEDERATED STATES OF”。可是我们的数据库country字段的限制是20,最终还是把这位老兄的留言拒之门外了。。。(小国家真不容易,这样也能被拒绝,我估计这老兄一定这么想。)

这让我想起了个问题,不知道世界上最长的国家名称能有多少个字符?
写个blog纪念一下这位老兄的国家吧,也算长见识了。

http://en.wikipedia.org/wiki/Federated_States_of_Micronesia

Python , ,

分析日志常用到的几个shell工具

March 31st, 2009

一个不错的例子: http://nunojob.wordpress.com/2008/04/12/history-awk-print-2-sort-uniq-c-sort-rn-head/

httpd.log | awk '{print $2}' \  | sort | uniq -c | sort -rn | head

解释:
httpd.log : 要分析的日志
awk : 用来取出某一特定的列。简单的也可以用cut来代替
sort : 用来排序(第一次排序是用来为后面的uniq服务的。uniq对于没有排序的内容工作不正确)
uniq : 用来uniq有序的内容,-c参数会把重复次数带上
sort -rn : 用来安重复次数倒序排列
head : 用来去前几条数据,默认是10.

Python ,

Satchmo 新体验

March 30th, 2009

今天checkout最新的satchmo看了一下,发现变化还是很大的。看样子我们的BE是没法升级到最新的satchmo了…

还没有时间仔细研究,不过发现有几个satchmo用到的第三方的app值得注意一下:
1. django-registration

http://code.google.com/p/django-registration/

这个已经用过几次,感觉不错,而且感觉它的生命力很强。
2. django-values

http://code.google.com/p/django-values/

satchmo用这个替换了原来自己写的configuration模块。我一直认为satchmo的configuration模块有设计上的缺陷,结果这次被彻底换掉了,不错。有时间研究一下这个。
3. threaded_multihost

http://gosatchmo.com/apps/django-threaded-multihost/

multi-site aware features, 不是很懂,还得看看代码。

Python ,

[八卦] google music, 爽!

March 30th, 2009

今天看新闻发现google出了music搜索,于是试用了一把。 (http://www.google.cn/music/homepage)

搜歌,听歌,下载,一应俱全。
尤其是它的web播放器,用起来很舒服。

对于google的music搜索,有几个疑问:
1. 歌曲的下载地址不是链接到其他网站,而是统一链接到top100.cn
2. 上面可以下载的歌曲很新,不知道版权问题是如何解决的?

Python

Normal method, @staticmethod, @classmethod of python

March 18th, 2009

Reference:
http://snippets.dzone.com/posts/show/1679
http://www.geocities.com/foetsch/python/new_style_classes.htm

Check the doctest in these code bottom.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test the variable methods in class
"""
 
class K(object):
    """
    Test the variable methods in class
    >>> k_obj = K()
    >>> k_obj.method1()
    Normal method: obj.method1() becomes method1(obj)
    >>> K.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.K'>)
    >>> k_obj.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.K'>)
    >>> K.method3()
    staticmethod: K.method3() become just method3(None)
    >>> k_obj.method3()
    staticmethod: K.method3() become just method3(None)
    """
    # normal method (instance method)
    def method1(self):
        print 'Normal method: obj.method1() becomes method1(obj)'
 
    # class method
    @classmethod
    def method2(cls):
        print 'classmethod: K.method2() becomes method2(klass: %s)' % cls
 
    # static method
    @staticmethod
    def method3():
        print 'staticmethod: K.method3() become just method3(None)'
 
class KK(K):
    """
    Test the variable methods' behavior in the subclass.
    >>> kk_obj = KK()
    >>> kk_obj.method1()
    Normal method: obj.method1() becomes method1(obj)
    >>> KK.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.KK'>)
    >>> kk_obj.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.KK'>)
    >>> KK.method3()
    staticmethod: K.method3() become just method3(None)
    >>> kk_obj.method3()
    staticmethod: K.method3() become just method3(None)
    """
    pass 
 
if __name__ == "__main__":
    import doctest
    doctest.testmod()

Python

A good example to explain *args and **kwargs in python

March 10th, 2009

I read this wonderful example here: http://www.megasolutions.net/python/-args-and—kwargs-78766.aspx

>>> def a(*stuff):
                 print repr(stuff)
>>> def b(**stuff):
                 print repr(stuff)
>>> def c(*args, **kwargs):
                 print 'args', repr(args)
                 print 'kwargs', repr(kwargs)
>>> a(1,2,3)
(1, 2, 3)
>>> b(hello='world', lingo='python')
{'hello': 'world', 'lingo': 'python'}
>>> c(13,14,thenext=16,afterthat=17)
args (13, 14)
kwargs {'afterthat': 17, 'thenext': 16}
>>> args = [1,2,3,4]
>>> kwargs = {'no-way': 23, 'yet-anotherInvalid.name': 24}
>>> c(*args, **kwargs)
args (1, 2, 3, 4)
kwargs {'no-way': 23, 'yet-anotherInvalid.name': 24}

Here is some simple explaination about *args and **kwargs

Basically ‘args’ is a tuple with all the positional arguments, kwargs is a dictionary with all the named arguments.
Likewise you can pass a tuple to a function like func(*tuple), or a dict like func(**dictionary) or both, where the zuple has to come first.

Python

A project list I want to do.

January 8th, 2009

1. Write a script to sync the tickets from trac to platform(Platform is a internal project.). I don’t need to create the ticket in the trac and copy it to the platform. It’s a waste.

2. Improve google code: getsong project. I want to get the top 500 song list from http://www.9sky.com/ and download the song with getsong script.

Developer

A good structure practice of Django project

December 18th, 2008

I want to share a directory structure comes from my Django project.

Here is basic structure:

/PROJECT/manage.py (under svn)
/PROJECT/__init__.py (under svn)
/PROJECT/urls.py (under svn)
/PROJECT/settings.py (under svn)
/PROJECT/local_settings.py (not under svn)
/PROJECT/env.sh (not under svn)
/PROJECT/run (not under svn)
/PROJECT/apps (under svn)
/PROJECT/docs (under svn)
/PROJECT/static (under svn)
/PROJECT/templates (under svn)
/PROJECT/tests (under svn)
/PROJECT/scripts (under svn)
/PROJECT/initial_data (under svn)
/PROJECT/middlewares (under svn)
/PROJECT/patches (under svn)
/PROJECT/logs (not under svn)
/PROJECT/tmp (not under svn)

Here is the detail for each item.

  1. manage.py and __init__.py are default, you don’t need to change them.
  2. urls.py. Actually, we didn’t not write the url patterns in this file, we just include the url patterns from apps.urls in this file
  3. settings.py and local_settings.py. You must import local_settings.py from settings.py. In the settings.py, you have generic configuration. So you can add this file in the subversion. For in local_settings.py, we will put some particular settings based different environment. You don’t need to worry about breaking other’s environment by checking in the settings.py file any more.
  4. env.sh. This is a key file in this structure. With my experience, some rookies are always bothered by kinds of module not found issues. In this file you need at least add these two environment variable. They’re PYTHONPATH and DJANGO_SETTINGS_MODULE. I always add a PROJECT_HOME variable, and then I can use this later in my shell scripts. That’s very very useful.
  5. run. This a shell wrapping of manage.py.  That’s because you always need some environment setup before you run the manage.py. Here is a example
  6. PYTHONPATH=$PROJECT_HOME:$PYTHONPATH
    DJANGO_SETTINGS_MODULE=settings
    python2.5 manage.py $*
  7. apps directory, it’s the directory hold all of the apps.
  8. docs, we always put kinds of document about the project here, such as deploy document
  9. static directory, the static pages are always located here.
  10. templates, we put template here.
  11. tests, we plan to put the QA’s test cases there. We also will put some unit test there.
  12. scripts, some dba scripts located here.
  13. initial_data: The basic database of the site.
  14. patches and tmp directory is rarely used.

Python ,

FireStats icon Powered by FireStats