Python之靜態語法檢查


  Python是一門動態語言。在給python傳參數的時候並沒有嚴格的類型限制。寫python程序的時候,發現錯誤經常只能在執行的時候發現。有一些錯誤由於隱藏的比較深,只有特定邏輯才會觸發,往往導致需要花很多時間才能將語法錯誤慢慢排查出來。其實有一些錯誤是很明顯的,假如能在寫程序的時候發現這些錯誤,就能提高工作效率。最近我發現,可以用pyflakes這個程序對python語法進行語法檢查,這樣可以盡量早的發現錯誤。pyflakes有三種使用方法,第一種是當作命令行工具使用;第二種可以作為vim的插件,在vim編輯器中使用,實現所見即所得;第三種是可以在emacs中使用。

  Pyflakes的下載地址戳這里

  • 命令行用法:
    pyflakes *.py
  • vim編輯器的配置,首先vim需要支持python,vim是否支持python按照如下方式檢查:
    vim
    #進入vim編輯器界面之后,在命令行模式下輸入如下命令
    :version

    結果如下:

    VIM - Vi IMproved 7.3 (2010 Aug 15, compiled May  4 2012 04:09:27)
    Included patches: 1-429
    Modified by pkg-vim-maintainers@lists.alioth.debian.org
    Compiled by buildd@
    Huge version without GUI.  Features included (+) or not (-):
    +arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent
    -clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
    +conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs
    -dnd -ebcdic +emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path
    +find_in_path +float +folding -footer +fork() +gettext -hangul_input +iconv
    +insert_expand +jumplist +keymap +langmap +libcall +linebreak +lispindent
    +listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape
    +mouse_dec +mouse_gpm -mouse_jsbterm +mouse_netterm -mouse_sysmouse
    +mouse_xterm +mouse_urxvt +multi_byte +multi_lang -mzscheme +netbeans_intg
    +path_extra -perl +persistent_undo +postscript +printer +profile +python
    -python3 +quickfix +reltime +rightleft -ruby +scrollbind +signs +smartindent
    -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary
    +tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
     -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo
    +vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
     -xterm_clipboard -xterm_save

    其中,+號代表支持,我的vim支持python,不支持的小伙伴們升級你們的vim吧!好,繼續,下面我們對vim進行配置,使其支持pyflakes。首先,下載pyflakes.vim,將其解壓到vim配置文件夾,我是直接配置到/etc/vim/文件夾下面,修改vim的配置,打開vimrc,配置如下選項:

     40 if has("autocmd")
     41   filetype plugin indent on
     42 endif

    好了,萬事俱備,測試一下吧!測試結果如下:

  • 配置成功,emacs的配置不再詳細介紹,感興趣的小伙伴自己去google!感謝大家的閱讀,希望能幫到大家!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM