Created
October 31, 2010 14:48
-
-
Save t9md/656667 to your computer and use it in GitHub Desktop.
vim_bridge example try
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "-----------------------------------------------" | |
echo "Self training for 'vim_vridge' example" | |
echo " http://github.com/nvie/vim_bridge/tree/master/examples/" | |
echo "-----------------------------------------------" | |
echo "Example 1" | |
echo "-----------------------------------------------" | |
python << ENDPYTHON | |
from vim_bridge import bridged | |
@bridged | |
def say_hello(first, last): | |
return "Hello, %s %s!" % (first, last) | |
ENDPYTHON | |
echo SayHello("t9", "md") | |
" [Result] | |
" Hello, t9 md! | |
echo "-----------------------------------------------" | |
echo "Example 2" | |
echo "-----------------------------------------------" | |
python << ENDPYTHON | |
@bridged | |
def get_longest(list): | |
return max(map(lambda s: len(s), list)) | |
ENDPYTHON | |
echo GetLongest(['one', 'two', 'three', 'four']) | |
" returns 5 (because "three" is 5 chars long) | |
" [Result] | |
" 5 | |
echo "-----------------------------------------------" | |
echo "Example 3" | |
echo "-----------------------------------------------" | |
python << ENDPYTHON | |
@bridged | |
def will_cause_exception(): | |
raise Exception("Oops") | |
ENDPYTHON | |
" This will throw an error to the user... | |
try | |
echo WillCauseException() | |
catch /.*python.*/ | |
echo "Error occured" | |
finally | |
echo "Clean up" | |
endtry | |
" [Result] | |
" Error occured | |
" Clean up | |
echo "-----------------------------------------------" | |
echo "Example 4" | |
echo "-----------------------------------------------" | |
python << ENDPYTHON | |
import os.path | |
@bridged | |
def normalize_path(path): | |
return os.path.realpath(path) | |
ENDPYTHON | |
echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name") | |
echo NormalizePath("..") | |
" [Result] | |
" /that/is/a/path/name | |
" /Users | |
echo "-----------------------------------------------" | |
echo "Example 5" | |
echo "-----------------------------------------------" | |
echo "I skipped, because I couldn't understand the difference from example-4" | |
echo "-----------------------------------------------" | |
echo "Example 6" | |
echo "-----------------------------------------------" | |
python << ENDPYTHON | |
import os | |
@bridged | |
def public(): | |
return "I am public." | |
@bridged | |
def _private(): | |
return "I am private (available in the current script only)." | |
@bridged | |
def my_name_is_auto_converted(): | |
return "In Python, I'm called my_name_is_auto_converted, but in Vim, I'm called MyNameIsAutoConverted :)" | |
@bridged | |
def _long_private_name(): | |
return "I'm private, and my case is converted automatically." | |
ENDPYTHON | |
echo Public() | |
echo s:Private() | |
" [Result] | |
" I am public. | |
" I am private (available in the current script only). | |
echo MyNameIsAutoConverted() | |
echo s:LongPrivateName() | |
" [Result] | |
" In Python, I'm called my_name_is_auto_converted, but in Vim, I'm called MyNameIsAutoConverted :) | |
" I'm private, and my case is converted automatically. | |
echo "-----------------------------------------------" | |
echo "Example 7" | |
echo "-----------------------------------------------" | |
python << ENDPYTHON | |
# import os.path | |
@bridged | |
def is_cool(name): | |
"""I need to fix to convert True/False => 1/0 """ | |
let = 1 if name == 'Mr. Freeze' else 0 | |
return let | |
@bridged | |
def dont_return_anything(): | |
"""I need to fix to return 0 explicitly""" | |
print "I'm a function without return value." | |
return 0 | |
ENDPYTHON | |
echo IsCool("Mr. Freeze") | " => 1 | |
echo IsCool("Mr. t9md") | " => 0 | |
echo DontReturnAnything() | |
" [Result] | |
" I'm a function without return value. | |
" 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment