日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

my python FAQ

發布時間:2025/3/13 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 my python FAQ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • python編碼規范
  • http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
  • 判斷對象是否含有某屬性?
    if hasattr(object, 'attribute')
  • 反射獲取類實例
    globals()['ClassName']()
  • 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)
  • 查找列表中的指定值
    guids = []
    guids.append(1)
    guids.append(3)
    guidTofind = 4
    guidin = filter(lambda g: g==guidTofind, guids)
    #不在列表中
    if len(guidin)==0:
  • python三元表達式
    s = ('negitive', 'positive')[n >= 0]
  • python靜態方法
    注解式:
    @staticmethod
    def bar():
    print Foo.str
    other:
    def bar():
    print Foo.str
    ?bar = staticmethod(bar)
  • 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
  • 檢測文件是否存在
    import os.path
    os.path.isfile(fname)
  • 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()
  • cherrypy 訪問靜態資源
    html css js 圖片等資源的訪問方法:
    cherryd -i cpapp -c prod.conf
    cherrypy.quickstart(Root(), '/', config=conf)
    詳見: http://www.cherrypy.org/wiki/StaticContent
  • 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
  • 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 中的其它東西一樣都是大小寫敏感的。
  • 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
    函數內部不捕獲 由外圍捕獲 也會造成進程終止
  • 假設當前項目有幾個文件夾(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
    不報錯說明安裝成功
  • 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
  • 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()]
  • 安裝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 //軟鏈接
  • 檢查變量是否defined
    a = 1
    if a in dir()
  • 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()
  • 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')

    ?
  • 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)




  • 轉載于:https://www.cnblogs.com/shenguanpu/archive/2011/12/21/2296092.html

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的my python FAQ的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 日韩一区二区三区在线观看 | 日韩精品短片 | 成人av免费网站 | 少妇一晚三次一区二区三区 | 在线你懂的视频 | 夜夜操网站| 亚洲一区黄色 | 激情另类视频 | 亚洲欧美另类日韩 | 成人av电影在线播放 | 久久久久久91亚洲精品中文字幕 | 四虎4hu永久免费网站影院 | 神马久久网 | 日本a√在线观看 | 日日躁夜夜躁白天躁晚上躁91 | 久久国产在线视频 | 日本精品在线观看视频 | 日韩精品你懂的 | 国产中文字幕视频 | 日韩一区二区三区在线看 | 精品国产免费人成在线观看 | 国产精品电影网 | 精品欧美色视频网站在线观看 | 国产精品亚洲视频 | av福利站| 国产视频一区二区视频 | av影院在线 | 亚洲欧美日韩精品在线观看 | 骚av在线| 国产精品国产三级国产三级人妇 | 二级黄色大片 | 日韩久久一区二区三区 | 日韩黄色一区 | 97国产精品视频 | 秋霞av一区二区三区 | 色综合中文网 | 国产高清在线精品 | 超碰在线免费看 | 久草视频这里只有精品 | 中文字幕+乱码+中文字幕一区 | 激情综合五月婷婷 | 亚洲精品国产精品乱码不66 | 日韩精品免费播放 | 毛片毛片毛片毛片毛片毛片毛片毛片 | 国产黄片一区二区三区 | 农村偷人一级超爽毛片 | 亚洲欧美乱日韩乱国产 | 最色成人网 | 一区二区三区四区免费 | 翔田千里一区二区 | 欧美一区二区三区在线 | 欧美日韩成人一区二区在线观看 | 日韩一级在线播放 | 中文字幕无码av波多野吉衣 | 污污视频网站在线免费观看 | 日本精品视频在线播放 | 欧美成人aa| 免费黄色激情视频 | 日本黄色片. | av片在线免费观看 | 日韩在线观看免费全 | 亚欧美视频| 精品国产亚洲av麻豆 | 91久久久久久久久 | 欧美一卡二卡三卡四卡 | 国产精品久久久久99 | 日本人的性生活视频 | 亚洲经典一区 | 无码人妻一区二区三区免费 | 探花视频在线观看 | 老牛影视少妇在线观看 | 久久久资源 | 九九99视频 | 久久h| 欧美大片黄 | 偷拍一区二区三区 | 日本精品人妻无码免费大全 | 暖暖视频日本 | 久久精品这里只有精品 | 国产视频网站在线观看 | 一区二区三区日韩在线 | 日韩欧美国产一区二区三区在线观看 | 亚洲国产成人精品女人 | 免费av视屏| 欧美国产乱视频 | 国产成人短视频在线观看 | 51国产偷自视频区视频 | av高清在线 | 亚洲精品一区二区三区四区乱码 | 亚洲先锋影音 | 特一级黄色大片 | 黑人大群体交免费视频 | 91视频网址 | 一区二区网 | 一级特黄aa | 国产三级aaa | 熟妇人妻久久中文字幕 | jizzjizz中国精品麻豆 | 日韩激情影院 |