随笔分类 - python
摘要:关于decorator的官方描述如下:(http://docs.python.org/glossary.html)decoratorA function returning another function, usually applied as a function transformation using the@wrappersyntax. Common examples fordecorators areclassmethod()andstaticmethod().Thedecoratorsyntax is merely syntactic sugar, the following t
阅读全文
摘要:有关python的__call__在官方文档上有这么一句解释 (http://docs.python.org/reference/datamodel.html?highlight=__call__#object.__call__)object.__call__(self[,args...])Called when the instance is “called” as a function; if this method is defined,x(arg1,arg2,...)is a shorthand forx.__call__(arg1,arg2,...).当把一个实例当作方法来调用的时候
阅读全文
摘要:判断一个callable对象是否是函数>>> import os>>> import types>>> >>> for item in dir(os): if isinstance(eval('.'.join(['os', item])), types.FunctionType): print item _execvpe_exists_get_exports_list_make_stat_result_make_statvfs_result_pickle_stat_result_pickle
阅读全文
摘要:对于三目运算符(ternary operator),python可以用conditional expressions来替代如对于x<5?1:0可以用下面的方式来实现1 if x<5 else 0注: conditional expressions是在python 2.5之前引入的,所以以上代码仅适用于2.5以及之后的版本对于2.5之前的版本,可以用下面这种形式X<5 and 1 or 0对于switch,我们完全可以用dictionary来实现,看下面的例子>>> def switch(choice): return dict(enumerate(range
阅读全文
摘要:通过os.access(filename,mode):s.access(path, mode)¶Use the real uid/gid to test for access to path. Note that most operationswill use the effective uid/gid, therefore this routine can be used in asuid/sgid environment to test if the invoking user has the specified access topath. mode should be F_O
阅读全文
摘要:http://www.lfd.uci.edu/~gohlke/pythonlibs/
阅读全文
摘要:>>> l=range(3)>>> t=(3,4,5)>>> l[0, 1, 2]>>> t(3, 4, 5)>>> l.extend(t)>>> l[0, 1, 2, 3, 4, 5]>>> l+=t>>> l[0, 1, 2, 3, 4, 5, 3, 4, 5]看来list对象的+=操作和extend方法有异曲同工之处.如果我们直接l+t,就会报错,因为+不会对不同类型的object进行操作.>>> l+tTraceback (m
阅读全文
摘要:最近在找工作,为了让用人单位可以及时的看到我的简历,以及状态,决定每天刷新简历,不过如果天天登录智联,手动的操作点击一下刷新也挺麻烦的,干脆用python来写一个试试吧,这里使用了cPAMIE模块,代码如下:代码代码仅仅为了实现功能,没有做异常处理等,等以后有时间了,再慢慢实现吧
阅读全文
摘要:1.这里介绍的python的单例模式有两种方式,一种方式是win32event的CreateMutex来实现,另外就是定义一个全局变量第一种实现方式可以参考这里http://code.activestate.com/recipes/474070-creating-a-single-instance-application/2.对于方法2,网上的方法很多,这里就简单的摘抄一个,http://dev....
阅读全文
摘要:用python下载有两种方法,使用urllib,urllib2两个类1.使用urllib的urlretrieve方法:代码如下[代码]这样就会在c:\下有我们下载的urllib.mp3文件。2。使用urllib2[代码]
阅读全文
摘要:1.安装trac 0.12后无法支持中文。babel已经安装,但就是在preference(个人设置)选项中没有“language(语言)”选项卡。并且在主页还有报错信息:"Trac detected an internal error: KeyError: 'trac/locale' This is probably a local installation issue....
阅读全文
摘要:近日在看trac 0.12,现将配置mod_wsgi和apache过程,以及遇到的一些问题,记录下来,以便以后查看1.首先安装apache2.22.下载mod_wsgi.so,我这里用的是python2.5,apache2.2。我从mod_wsgi官网没有找到相应的版本,官网上多是python2.6,2.7,3.1和apache2.2的版本。从源码编译需要mod_wsgi 3.0以及以上的版本,就...
阅读全文
摘要:最近在用subprocess调用命令行的时候经常会遇到弹出黑框的问题,很令人讨厌。现记录下用去除黑框的方法:
阅读全文
摘要:这篇文章接第一部分,原文地址http://docs.djangoproject.com/en/1.2/intro/tutorial02/#intro-tutorial021.激活admin站点。要激活admin站点需要做三件事情1>在settings.py的INSTALLED_APPS里面添加"django.contrib.admin"2>运行manage.py syncdb命令3&g...
阅读全文
摘要:原文http://docs.djangoproject.com/en/1.2/intro/tutorial01/#intro-tutorial011.使用django-admin.py startproject project创建一个projectdjango-admin.py startproject mysite.在当前目录下创建了一个mysite目录 mysite目录包含下面四个文件 1&g...
阅读全文
摘要:1.django下载,这里用的版本是1.2.1,下载地址http://www.djangoproject.com/download/1.2.1/tarball/下载解压,运行python setup.py install安装完毕后,在shell下面做尝试[代码]如果运行结果和上面类似,说明安装成功。说明:django目前不支持3.0,官方支持的python版本为2.4--2.7,但推荐使用2.5和...
阅读全文
摘要:参考http://docs.python.org/howto/regex,http://docs.python.org/library/re.html,以下内容仅限于个人理解,如有不当之处,欢迎指出。 正则表达式(可以称为REs,regex,regex pattens)是一个小巧的,高度专业化的编程语言,它内嵌于python开发语言中,可通过re模块使用。正则表达式的pattern可以被编译成一系...
阅读全文
摘要:以前断断续续的看python,由于应用少,总是看了就忘,所以这里记录下来,已备以后查看。这次主要是把python里面的常用模块简单的做下记录.记录的部分仅仅是自己的理解。这里的python版本是2.5.2>>> import sys>>> sys.version'2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1...
阅读全文
浙公网安备 33010602011771号