前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >my python FAQ

my python FAQ

作者头像
架构师刀哥
发布2018-03-20 17:15:46
9710
发布2018-03-20 17:15:46
举报
文章被收录于专栏:坚毅的PHP坚毅的PHP
  1. python编码规范
  2. http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
  3. 判断对象是否含有某属性  if hasattr(object, 'attribute')
  4. 反射获取类实例 globals()['ClassName']()
  5. python日期转换  字符串到日期: import time timeInDate = time.strptime(timeInStr,"%Y-%m-%d %H:%M:%S") 日期到字符串: timeInStr = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) timeInStr = time.strftime("%Y/%m/%d %H:%M:%S", timeInDate)
  6. 查找列表中的指定值 guids = [] guids.append(1) guids.append(3) guidTofind = 4 guidin = filter(lambda g: g==guidTofind, guids) #不在列表中 if len(guidin)==0:
  7. python三元表达式 s = ('negitive', 'positive')[n >= 0]
  8. python静态方法 注解式: @staticmethod def bar(): print Foo.str other: def bar(): print Foo.str  bar = staticmethod(bar)
  9. pylint代码扫描规范工具Windows下安装 pylint用于代码自动分析,配置后eclipse设置build Automatically,每次保存后生成报告,说明你的代码是否符合编程规范,并给你打分(我的一份可以跑通的代码是 -13/10 ,负13,汗颜。。。) 参考http://www.logilab.org/card/pylint_manual 但是结果仍然提示: can't open file 'D:\Python26\Scripts\pylint': [Errno 2] No such file or directory 需要打开 D:\pylint-0.22.0\bin 目录,然后把那里的所有文件拷贝到 Python 的 Scripts 目录下( 如:D:\Python26\Scripts) 在命令行尝试执行 pylint,如果输出帮助,则表示已经安装成功 pylint默认的规范不符合驼峰方式的变量和方法命名方式 可视需要创建一份公用conf文件 确定变量和方法的正则表达式 配置草案: pylint.conf 可添加到eclipse=>Window=>preferences=>Pydev=>Pylint use Pylint勾上,location of pylint填入下载包pylint的本地路径 D:\develop\pylint\pylint-0.22.0\lint.py arguments框里填入: --rcfile=C:\Python26\Scripts\pylint.conf
  10. 检测文件是否存在 import os.path os.path.isfile(fname)
  11. post xml by http request
代码语言:javascript
复制
import httplib, urllib
params = urllib.urlencode( \
{'parameter': pValue, "p":valuep})
headers = { "Content-type": "text/xml,charset=utf-8"}
conn = httplib.HTTPConnection(self.webHost+":"+self.webPort)
conn.request("POST", "/"+self.webPath+"/?"+params, content , headers)
response = conn.getresponse()
print response.status, response.reason 
print response.read()
conn.close()
  1. cherrypy 访问静态资源 html css js 图片等资源的访问方法: cherryd -i cpapp -c prod.conf cherrypy.quickstart(Root(), '/', config=conf) 详见: http://www.cherrypy.org/wiki/StaticContent
  2. python binding cpp boost方式: 新建hello.cpp binding用的cpp hello_wrap.cpp #include #include using namespace boost::python; char const* greet(unsigned x); BOOST_PYTHON_MODULE(hello) { def("greet", greet, "return one of 3 parts of a greeting"); } 编译: sudo g++ -lpython2.5 -lboost_python -I/usr/include/python2.5 hello.cpp hello_wrap.cpp -shared -o hello.so 在当前目录下生成hello.so文件 python 命令行 : >>>import hello >>> print hello.greet(1) Boost.Python >>> python ctypes方式: http://blogold.chinaunix.net/u/21908/showart_2225882.html
代码语言:javascript
复制
char const* greet(unsigned x)
{
static char const* const msgs[] = { "hello", "Boost.Python", "world!" };
if (x > 2)
return "nothing";
return msgs[x];
} 
  1. Python 中的 True 在 2.2.1 版本之前,Python 没有单独的布尔数据类型。为了弥补这个缺陷,Python 在布尔环境 (如 if 语句) 中几乎接受所有东西,遵循下面的规则: • 0 为 false; 其它所有数值皆为 true。 • 空串 ("") 为 false; 其它所有字符串皆为 true。 • 空 list ([]) 为 false; 其它所有 list 皆为 true。 • 空 tuple (()) 为 false; 其它所有 tuple 皆为 true。 • 空 dictionary ({}) 为 false; 其它所有 dictionary 皆为 true。  这些规则仍然适用于 Python 2.2.1 及其后续版本,但现在您也可以使用真正的布尔值,它的值或者为 True 或者为 False。请注意第一个字母是大写的;这些值如同在 Python 中的其它东西一样都是大小写敏感的。
  2. python进程异常终止问题 可能原因:cmd调用出错 内存块读取错误 程序错误 项目中遇到是程序错误 没有进行except获取引起 例如 i = 1 while True: i = i+1 if i==100: i/0 出现除0错误 则进程终止 def test(): i = 1 while True: i = i+1 print [c.name for c in messages.columns] if i==100: i/0 try: test() except Exception: print Exception 函数内部不捕获 由外围捕获 也会造成进程终止
  3. 假设当前项目有几个文件夹(core,domain,util)的类需要安装到python中  建立setup.py from setuptools import setup setup(name='scservice', version='0.1.0', description="Easy python framework for sc service", packages = ['core','domain','util'], platforms = 'any', keywords='framework for sc service', author='shen guanpu', author_email='shenguanpu@netqin.com', url='www.netqin.com', license='(c) 2010 NetQin', include_package_data=True, zip_safe=False, ) 正常情况下packages应该写项目名称 或者仅安装项目的几个模块 则 packages = ['scservice.core','scservice.domain','scservice.util'], sudo python setup.py develop 可建立链接文件,如果svn有更新 可update下来 不需要再运行安装即可使用 sudo python setup.py install 则直接安装几个文件夹到 /python/lib/site-packages之下 测试: 进入python 命令行: from core.rule import Rule 不报错说明安装成功
  4. str 为何可用for遍历?(from python mail list) str 没有像list一样提供__iter__()方法,不能产生迭代对象,但却可用for 去遍历 原因是: Python's for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. for针对的是sequence,只要是sequence,它都能处理。 sequence protocol在这里有定义: http://docs.python.org/library/functions.html#iter the __getitem__() method with integer arguments starting at 0 也就是说,只要有__getitem__方法,都算是sequence,for都能处理。 验证一下: class A(object): def __getitem__(self, item): print 'getitem:', item if item == 5: raise IndexError() return item for i in A():  print i
  5. dict list转换 构建空值的dict dict.fromkeys([1,2,3]) => {1:None,2:None,3:None} 构建dict dict([(1,2),(2,3)]) => {1:2, 2:3} 从dict的key 构建list list( {1:2, 2:3}) => [1,2] or {1:2, 2:3}.keys() 从dict的value构建list [j for i,j in {1:2, 2:3}.iteritems()]
  6. 安装python wget http://www.python.org/ftp/python/2.6.5/Python-2.6.5.tar.bz2 解压: $bzip2 -d Python-2.5.2.tar.bz2 $ tar -xvf Python-2.5.2.tar 转移位置: $ mv Python-2.6.5 /usr/local/ l 安装 $ cd Python-2.6.5 $ ./configure $ make $ make install l 如果默认版本没有被替换 则需建立软链接 $cd /usr/bin $ll |grep python //查看该目录下python $rm -rf python $ln -s /usr/local/Python-2.6.5/python ./python //软链接
  7. 检查变量是否defined a = 1 if a in dir()
  8. Pycurl 绑定到特定的IP地址
代码语言:javascript
复制
def iptest(ip) :
    c = pycurl.Curl()
    c.setopt(c.URL, "http://www.ip138.com/ip2city.asp")
#   绑定到特定的IP地址
    c.setopt(pycurl.INTERFACE,ip)
    c.perform()
    c.fp = StringIO.StringIO()
    print c.fp.getvalue()
    c.close()
  1. python 多进程中使用random随机数 Linux的fork是写复制的,即fork出来的进程只有修改了的部分才另外开辟内存;而随机数是根据 种子数值得出的伪随机数,fork出来的进程的种子相同,所以数值相同。因此每次做完random后, 需要random.seed(),这样能生成新的随机数 uuid方式: >>> import uuid # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
代码语言:javascript
复制
def executeChangeable():
    pid = os.getpid()
    random.seed()
    randpart = random.randint(10000000, 99999999)
    return pid, randpart
  1. python抓取http代理
代码语言:javascript
复制
def getproxycn():

    portDict = {"R":"8","D":"0","C":"1","M":"4","Z":"3","K":"2","I":"7","L":"9","B":"5","W":"6"};

    pagenum = 0

    num = 0;

    proxyfile = "proxys.txt"

    file = open(proxyfile,"w+");

    while pagenum <= 9:

        url='http://www.cnproxy.com/proxy'+str(pagenum + 1)+'.html'

        html=urllib2.urlopen(url)

        for line in html:

                if "HTTP" in line:

                    arra = line.upper().split("<TR><TD>")

                    arrb = arra[1].split("<SCRIPT TYPE=TEXT/JAVASCRIPT>")

                    ip =  arrb[0]

                    port =  arrb[1].split(")</SCRIPT>")[0].split("DOCUMENT.WRITE(\":\"")[1]

                    port = port.replace("+","");

                    p = "";

                    for i in range(len(port)):

                        alph = port[i:i+1]

                        p += portDict[alph];

                    print ip + ":" + p

                    file.write(ip+":"+p+"\n")

                    num += 1;

        pagenum += 1;

    file.close();

    print "已处理代理总条数 : " + str(num)

 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档