SqlServer2012中First_Value函數簡略剖析。本站提示廣大學習愛好者:(SqlServer2012中First_Value函數簡略剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是SqlServer2012中First_Value函數簡略剖析正文
Vim 插件是一個 .vim 的劇本文件,界說了函數、映照、語律例則和敕令,可用於操作窗口、緩沖和行。普通一個插件包括了敕令界說和事宜鉤子。當應用 Python 編寫 vim 插件時,函數裡面是應用 VimL 編寫,雖然 VimL 學起來很快,但 Python 加倍靈巧,例如可以用 urllib/httplib/simplejson 來拜訪某些 Web 辦事,這也是為何許多須要拜訪 Web 辦事的插件都是應用 VimL + Python 編寫的緣由。
在開端編寫插件之前,你須要確認 Vim 支撐 Python,經由過程以下敕令來辨別:
vim --version | grep +python
接上去我們經由過程一個簡略的例子來進修用 Python 編寫 Vim 插件,該插件用來獲得 Reddit 首頁信息並顯示在以後緩沖區上。
起首在 Vim 新建 vimmit.vim 文件,我們起首須要斷定能否支撐 Python,假如不支撐給出提醒信息:
if !has('python') echo "Error: Required vim compiled with +python" finish endif
下面這段代碼就是用 VimL 編寫的,它將檢討 Vim 能否支撐 Python。
上面是用 Python 編寫的 Reddit() 主函數:
" Vim comments start with a double quote. " Function definition is VimL. We can mix VimL and Python in " function definition. function! Reddit() " We start the python code like the next line. python << EOF # the vim module contains everything we need to interface with vim from # python. We need urllib2 for the web service consumer. import vim, urllib2 # we need json for parsing the response import json # we define a timeout that we'll use in the API call. We don't want # users to wait much. TIMEOUT = 20 URL = "http://reddit.com/.json" try: # Get the posts and parse the json response response = urllib2.urlopen(URL, None, TIMEOUT).read() json_response = json.loads(response) posts = json_response.get("data", "").get("children", "") # vim.current.buffer is the current buffer. It's list-like object. # each line is an item in the list. We can loop through them delete # them, alter them etc. # Here we delete all lines in the current buffer del vim.current.buffer[:] # Here we append some lines above. Aesthetics. vim.current.buffer[0] = 80*"-" for post in posts: # In the next few lines, we get the post details post_data = post.get("data", {}) up = post_data.get("ups", 0) down = post_data.get("downs", 0) title = post_data.get("title", "NO TITLE").encode("utf-8") score = post_data.get("score", 0) permalink = post_data.get("permalink").encode("utf-8") url = post_data.get("url").encode("utf-8") comments = post_data.get("num_comments") # And here we append line by line to the buffer. # First the upvotes vim.current.buffer.append("↑ %s"%up) # Then the title and the url vim.current.buffer.append(" %s [%s]"%(title, url,)) # Then the downvotes and number of comments vim.current.buffer.append("↓ %s | comments: %s [%s]"%(down, comments, permalink,)) # And last we append some "-" for visual appeal. vim.current.buffer.append(80*"-") except Exception, e: print e EOF " Here the python code is closed. We can continue writing VimL or python again. endfunction
應用以下敕令保留文件
:source vimmit.vim
然後挪用該插件:
:call Reddit()
這個敕令用起來不那末便利,是以我們再界說一個敕令:
command! -nargs=0 Reddit call Reddit()
我們界說了敕令:Reddit來挪用這個函數。-nargs 參數聲明敕令行中有若干個參數。
關於函數參數的成績:
問:若何拜訪函數中的參數?
function! SomeName(arg1, arg2, arg3) " Get the first argument by name in VimL let firstarg=a:arg1 " Get the second argument by position in Viml let secondarg=a:1 " Get the arguments in python python << EOF import vim first_argument = vim.eval("a:arg1") #or vim.eval("a:0") second_argument = vim.eval("a:arg2") #or vim.eval("a:1")
你可使用 ... 來處置可變個數參數來調換特定的參數名,可經由過程地位或許定名參數來拜訪,如:(arg1, arg2, ...)
問:若何在 Python 中挪用 Vim 敕令?
vim.command("[vim-command-here]")
問:若何界說全局變量,並在 VimL 和 Python 中拜訪?
全局變量應用形如 g:. 的前綴,界說全局變量前應當檢討該變量能否已界說:
if !exists("g:reddit_apicall_timeout") let g:reddit_apicall_timeout=40 endif
然後你經由過程上面代碼在 Python 中拜訪這個變量:
TIMEOUT = vim.eval("g:reddit_apicall_timeout")
可經由過程上面的辦法來對全局變量停止從新賦值:
let g:reddit_apicall_timeout=60
備注:
一旦你用過VimL,就會發明它挺簡略的,你用python寫的代碼也能夠用它來完成。具體請參考vim python模塊文檔,這是一份主要的參考材料。
除上述文檔,你也能夠在IBM developerWorks網站找到一些有效的材料。