Python function calls from the bash shell or dos prompt
2006-12-20 11:02
603 查看
From DANSE
Beyond the standard way to call python from a shell (like bash)
[1, 2]
If one adds a bit of code to any existing code, some nice things happen...
then from a shell, we can test everything:
testing x...
[1, 2]
['a','b']
[[1,2,3],[4,5,6]]
testing add...
3
ab
[1, 2, 3, 4, 5, 6]
we can call a function incorrectly, and get documentation:
Error: incorrect syntax 'x(4)'
x(a,b) --> [a,b]
and we can call a function correctly, and get the desired response
[4, 5]
this is really cool because if the file "example.py" lives on a remote computer, we can still call it from our local computer:
[4, 5]
now we say if we have an implimentation of the shell commands
"which" or "find" (gsl.infect does), then we can get useful information
like:
LOCALLY
/usr/local/bin/zip
REMOTELY
/usr/bin/zip
Beyond the standard way to call python from a shell (like bash)
>$ python -c "x = [1,2];print '%s' % x"
[1, 2]
If one adds a bit of code to any existing code, some nice things happen...
#file="example.py" def x(a,b): '''x(a,b) --> [a,b]''' return [a,b] def add(a,b): '''add(a,b) --> a+b''' return a+b def test(): '''test code for all modules''' print "testing x..." print x(1,2) print x('a','b') print x([1,2,3],[4,5,6]) print testing add..." print add(1,2) print add('a','b') print add([1,2,3],[4,5,6]) return
if __name__=='__main__': import sys try: func = sys.argv[1] except: func = None if func: try: exec 'print %s' % func except: print "Error: incorrect syntax '%s'" % func exec 'print %s.__doc__' % func.split('(')[0] else: test()
then from a shell, we can test everything:
>$ python example.py
testing x...
[1, 2]
['a','b']
[[1,2,3],[4,5,6]]
testing add...
3
ab
[1, 2, 3, 4, 5, 6]
we can call a function incorrectly, and get documentation:
>$ python example.py "x(4)"
Error: incorrect syntax 'x(4)'
x(a,b) --> [a,b]
and we can call a function correctly, and get the desired response
>$ python example.py "x(4,5)"
[4, 5]
this is really cool because if the file "example.py" lives on a remote computer, we can still call it from our local computer:
>$ ssh remote.caltech.edu 'cd ~/dev/test; python example.py "x(4,5)"'
[4, 5]
now we say if we have an implimentation of the shell commands
"which" or "find" (gsl.infect does), then we can get useful information
like:
LOCALLY
>$ cd ~/dev/gsl/infect; python shutils.py "which('zip')"
/usr/local/bin/zip
REMOTELY
>$ ssh remote.caltech.edu "cd ~/dev/gsl/infect; python shutils.py which\(\'zip\'\)"
/usr/bin/zip
相关文章推荐
- BASH Shell change the color of my shell prompt under Linux or UNIX
- ATL项目编译注册dll的时候报权限错误:error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions.
- Django: show/log ORM sql calls from python shell - Stack Overflow
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.
- How to: Create Files in Linux from a Bash Shell Prompt
- drp错误集锦---“Cannot return from outside a function or method”
- Myeclipse2016 部署webapp 至 tomcat 上出现 “There are no resources that can be added or removed from the server”
- swagger在本地没问题可是上传至服务器报错解决办法Can't read from server. It may not have the appropriate access-control-or
- RHCE笔记整理-RH033.Unit4-The Bash shell
- 关于MyEclipse较检异常 Invalid result location value/parameter 及Cannot return from outside a function or
- Dynamics CRM 本地插件注册器连CRMAn unsecured or incorrectly secured fault was received from the other party
- shell脚本报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory --引用自http://blog.csdn.net/xiaaiwu/article/details/49126777
- “Cannot return from outside a function or method”
- (转)one or more files from the emulator for windows ce installation is missing 解决方法
- Linux and C[2]: What is the function of inet_aton or inet ntoa ?
- python - Prompt the user to input something else if the first input is invalid - Stack Overflow
- Cannot return from outside a function or method
- shell脚本报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
- 有趣的文章:Will Intel be forced to remove the "cripple AMD" function from their compiler?
- eclise 部署web工程报 There are no resources that can be added or removed from the server. 解决办法