var arr = [1, 2, 3,];
alert(arr.length);
In firefox, the result is 3, but in IE, it is 4. It always takes some bugs which are hard to found. Right now, I don’t know which is the correct way, but I always remove the last comma safely.
javascript
javascript
>>> 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
Python
今天我们站点上出现了个跟国名有关的错误。一个来自”The Federated States of Micronesia”的老兄试图给我们站点留言。我们的IP2Location解析了他的ip并且得到了正确的国家名称”MICRONESIA, FEDERATED STATES OF”。可是我们的数据库country字段的限制是20,最终还是把这位老兄的留言拒之门外了。。。(小国家真不容易,这样也能被拒绝,我估计这老兄一定这么想。)
这让我想起了个问题,不知道世界上最长的国家名称能有多少个字符?
写个blog纪念一下这位老兄的国家吧,也算长见识了。
http://en.wikipedia.org/wiki/Federated_States_of_Micronesia
Python
Django, life, Python
一个不错的例子: 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
Linux, shell
今天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
Django, satchmo
今天看新闻发现google出了music搜索,于是试用了一把。 (http://www.google.cn/music/homepage)
搜歌,听歌,下载,一应俱全。
尤其是它的web播放器,用起来很舒服。
对于google的music搜索,有几个疑问:
1. 歌曲的下载地址不是链接到其他网站,而是统一链接到top100.cn
2. 上面可以下载的歌曲很新,不知道版权问题是如何解决的?
Python
tool
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
Python
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
Python
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
Recent Comments