>>> 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
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
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.
- manage.py and __init__.py are default, you don’t need to change them.
- 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
- 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.
- 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.
- 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
PYTHONPATH=$PROJECT_HOME:$PYTHONPATH
DJANGO_SETTINGS_MODULE=settings
python2.5 manage.py $*
- apps directory, it’s the directory hold all of the apps.
- docs, we always put kinds of document about the project here, such as deploy document
- static directory, the static pages are always located here.
- templates, we put template here.
- tests, we plan to put the QA’s test cases there. We also will put some unit test there.
- scripts, some dba scripts located here.
- initial_data: The basic database of the site.
- patches and tmp directory is rarely used.
Python
Django, Python
Recent Comments