By minitia, on 十月 29th, 2009, Category: Python
这里主要是用sort方法对list中的数据进行排序。如果list中元素是tuple这样的类型,则按照tuple里面元素的位置的先后来排序。 代码如下:
Continue reading Python list的排序 - 全文阅读
By minitia, on 十月 28th, 2009, Category: Python
wxpython事件处理evt列表
python中evt处理,可以使用bind将事件触发,绑定至控件,并提供事件处理函数 其evt列表可参考/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode下的*.py文件。*.py文件内有相关的控件列表。
例如: listctrl控件列表事件处理 选中listctrl一行,触发listctrl控件事件wx.EVT_LIST_ITEM_ACTIVATED。
这时,可以使用bind,将listctrl控件事件,与事件处理函数绑定,就可以实现当鼠标,或则光标选中listctrl行时,处理相关的事件。
By minitia, on 十月 28th, 2009, Category: Python
django中model类都是ModelBase的子类。ModelBase是在django.db.models.base中定义的。 如果一个mdoel类是ModelBase的子类,他的属性有: ['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__unicode__', '__weakref__', '_base_manager', '_collect_sub_objects', '_default_manager', '_deferred', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_meta', '_set_pk_val', 'delete', 'objects', 'permissions', 'pk', 'prepare_database_save', 'save', 'save_base', 'serializable_value', 'user_set']
Continue reading django中ModelBase的属性 - 全文阅读
By minitia, on 十月 19th, 2009, Category: Python
先来看个例子: def foo(*args, **kwargs): print ‘args = ‘, args print ‘kwargs = ‘, kwargs print ‘—————————————’ if __name__ == ‘__main__’: foo(1,2,3,4) foo(a=1,b=2,c=3) foo(1,2,3,4, a=1,b=2,c=3) foo(’a', 1, None, a=1, b=’2′, c=3)输出结果如下:
args = (1, 2, 3, 4) kwargs = {} ————————————— args = () kwargs = {’a': 1, ‘c’: 3, ‘b’: 2} ————————————— args = (1, 2, 3, 4) kwargs = {’a': 1, ‘c’: 3, ‘b’: 2} ————————————— args = (’a', 1, None) kwargs = {’a': 1, ‘c’: 3, ‘b’: ‘2′} —————————————
可以看到,这两个是python中的可变参数。*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前,像foo(a=1, b=’2′, c=3, a’, 1, None, )这样调用的话,会提示语法错误“SyntaxError: non-keyword arg after keyword arg”。
呵呵,知道*args和**kwargs是什么了吧。还有一个很漂亮的用法,就是创建字典: def kw_dict(**kwargs): return kwargs print kw_dict(a=1,b=2,c=3) == {’a':1, ‘b’:2, ‘c’:3}
其实python中就带有dict类,使用dict(a=1,b=2,c=3)即可创建一个字典了。
“人生苦短,我用python。”
By minitia, on 十月 16th, 2009, Category: Python
#!/USR/BIN/PYTHON import smtplib,string
class SendMail: def __init__(self,smtpServer,user="",passwd=""): self.smtpServer=smtpServer self.user=user self.passwd=passwd
def sendmail(self,fromAddr,toAddr,subject,msg): server=smtplib.SMTP(self.smtpServer)
if len(self.user)!=0 and len(self.passwd)!=0: server.login(self.user,self.passwd) body=string.join(( "FROM: %s" %fromAddr, "TO: %s" %toAddr, "Subject: %s" %subject, "", msg),"\r\n")
server.sendmail(fromAddr,toAddr,body) server.quit()
if __name__ == "__main__": smDemo=SendMail("192.168.1.100","wuweixin","wwx’s passwd") smDemo.sendmail("wuweixin@pconline.com.cn","121957514@qq.com","SendMail Test","Just Test")
By minitia, on 十月 12th, 2009, Category: Python
在做网络爬虫中很多人都会遇到,做得比较好的一些企业级网站,对于登陆或者其他操作都会对用户的输入做一些加密处理后再post到服务器上去,而加密这部分几乎都是通过JavaScript来完成的,所以要做爬虫,需要模拟 这些加密的处理。 我比较喜欢Python来做爬虫,urllib/urllib2库用起来真的是很方便,所以很多时候遇到需要在python中来模拟网站的JavaScript加密过程。暂时总结起来有两种方法:
Continue reading Python中模拟网页的javascript加密验证处理 - 全文阅读
By minitia, on 十月 12th, 2009, Category: Python
1.编译正则表达式 >>> import re >>> p = re.compile(’ab*’,re.IGNORECASE) >>> print p <_sre.SRE_Pattern object at 0×012FB3E0> 得到了一个正则表达式的对象!RegexObject` 实例,可以为不同的操作提供方法,如模式匹配搜索或字符串替换。 类似于JAVA里面的正则编译。得到了这个正则实例之后就能够拿它出来进行了匹配搜索或字符串处理了!
Continue reading Python正则表达式心得整理 - 全文阅读
By minitia, on 十月 12th, 2009, Category: Python
def createDaemon():
”’Funzione che crea un demone per eseguire un determinato programma…”’
import os
# create - fork 1
try:
if os.fork() > 0: os._exit(0) # exit father…
except OSError, error:
print ‘fork #1 failed: %d (%s)’ % (error.errno, error.strerror)
os._exit(1)
# it separates the son from the father
os.chdir(’/')
os.setsid()
os.umask(0)
# create - fork 2
try:
pid = os.fork()
if pid > 0:
print ‘Daemon PID %d’ % pid
os._exit(0)
except OSError, error:
print ‘fork #2 failed: %d (%s)’ % (error.errno, error.strerror)
os._exit(1)
funzioneDemo() # function demo
def funzioneDemo():
import time
fd = open(’/tmp/demone.log’, ‘w’)
while True:
fd.write(time.ctime()+’\n’)
fd.flush()
time.sleep(2)
fd.close()
if __name__ == ‘__main__’:
createDaemon()
By minitia, on 十月 12th, 2009, Category: Python
1.性能
Py3.0运行pystone benchmark的速度比Py2.5慢30%。Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可以取得很好的优化结果。
2.编码
Py3.0源码文件默认使用utf-8编码,这就使得以下代码是合法的:
>>>中国 = ‘china’
>>> print(中国)
china
Continue reading python3.0与2.x之间的区别 - 全文阅读
Page 4 of 13 « First ... « 2 3 4 5 6 » ... Last »
|
|
|