• my python FAQ


    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
      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()
    12. cherrypy 访问静态资源
      html css js 图片等资源的访问方法:
      cherryd -i cpapp -c prod.conf
      cherrypy.quickstart(Root(), '/', config=conf)
      详见: http://www.cherrypy.org/wiki/StaticContent
    13. python binding cpp
      boost方式:
      新建hello.cpp
      char const* greet(unsigned x)
      {
      static char const* const msgs[] = { "hello", "Boost.Python", "world!" };
      if (x > 2)
      return "nothing";
      return msgs[x];
      }


      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
    14. 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 中的其它东西一样都是大小写敏感的。
    15. 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
      函数内部不捕获 由外围捕获 也会造成进程终止
    16. 假设当前项目有几个文件夹(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
      不报错说明安装成功
    17. 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
    18. 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()]
    19. 安装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 //软链接
    20. 检查变量是否defined
      a = 1
      if a in dir()
    21. Pycurl 绑定到特定的IP地址


      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()
    22. python 多进程中使用random随机数

      Linux的fork是写复制的,即fork出来的进程只有修改了的部分才另外开辟内存;而随机数是根据
      种子数值得出的伪随机数,fork出来的进程的种子相同,所以数值相同。因此每次做完random后,
      需要random.seed(),这样能生成新的随机数


      def executeChangeable():
      pid = os.getpid()
      random.seed()
      randpart = random.randint(10000000, 99999999)
      return pid, randpart
      uuid方式:
      >>> import uuid

      # make a random UUID
      >>> uuid.uuid4()
      UUID('16fd2706-8baf-433b-82eb-8c7fada847da')


       
    23. python抓取http代理
      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)




  • 相关阅读:
    SAP MM模块教程(1)— 概述
    CodeSmith Generator
    EasyUI 左侧 tree 右侧 DataGrid模板
    C# 数据库访问公共类
    Easyui datagrid toolbar 模板
    EasyUI combogrid 更新查询参数 queryParams 重新加载
    Jenkins的搭建与使用
    Idea2020激活,全网最新教程(各版本通用),亲测2089!!!
    阿里云盘开启“个人云种子用户报名
    尚硅谷Java互联网大厂面试题第三季,1024干货
  • 原文地址:https://www.cnblogs.com/shenguanpu/p/2296092.html
Copyright © 2020-2023  润新知