Lupa - Python中调用Lua(收藏)
来源:互联网 发布:网络教育的考试难吗 编辑:程序博客网 时间:2024/06/03 20:42
Lupa将LuaJIT集成到了Python模块中,可以在Python中执行Lua代码。 比较有意思,也许以后用的着,记录一下。
基本用法:
>>> import lupa
>>> from lupa import LuaRuntime
>>> lua = LuaRuntime()
>>> lua.eval('1+1')
2
>>> lua_func = lua.eval('function(f, n) return f(n) end')
>>> def py_add1(n): return n+1
>>> lua_func(py_add1, 2)
3
>>> lua.eval('python.eval(" 2 ** 2 ")') == 4
True
>>> lua.eval('python.builtins.str(4)') == '4'
True
>>> from lupa import LuaRuntime
>>> lua = LuaRuntime()
>>> lua.eval('1+1')
2
>>> lua_func = lua.eval('function(f, n) return f(n) end')
>>> def py_add1(n): return n+1
>>> lua_func(py_add1, 2)
3
>>> lua.eval('python.eval(" 2 ** 2 ")') == 4
True
>>> lua.eval('python.builtins.str(4)') == '4'
True
Lua中的Python对象
>>> lua_func = lua.eval('function(obj) return obj["get"] end')
>>> d = {'get' : 'got'}
>>> value = lua_func(d)
>>> value == 'got'
True
>>> dict_get = lua_func( lupa.as_attrgetter(d) )
>>> dict_get('get') == 'got'
True
>>> lua_func = lua.eval(
... 'function(obj) return python.as_attrgetter(obj)["get"] end')
>>> dict_get = lua_func(d)
>>> dict_get('get') == 'got'
True
>>> d = {'get' : 'got'}
>>> value = lua_func(d)
>>> value == 'got'
True
>>> dict_get = lua_func( lupa.as_attrgetter(d) )
>>> dict_get('get') == 'got'
True
>>> lua_func = lua.eval(
... 'function(obj) return python.as_attrgetter(obj)["get"] end')
>>> dict_get = lua_func(d)
>>> dict_get('get') == 'got'
True
Lua中的迭代循环:
>>> lua_copy = lua.eval('''
... function(L)
... local t, i = {}, 1
... for item in python.iter(L) do
... t[i] = item
... i = i + 1
... end
... return t
... end
... ''')
>>> table = lua_copy([1,2,3,4])
>>> len(table)
4
>>> table[1] # Lua indexing
1
... function(L)
... local t, i = {}, 1
... for item in python.iter(L) do
... t[i] = item
... i = i + 1
... end
... return t
... end
... ''')
>>> table = lua_copy([1,2,3,4])
>>> len(table)
4
>>> table[1] # Lua indexing
1
Lua中的Table:
>>> table = lua.eval('{10,20,30,40}')
>>> table[1]
10
>>> table[4]
40
>>> list(table)
[1, 2, 3, 4]
>>> list(table.values())
[10, 20, 30, 40]
>>> len(table)
4
>>> mapping = lua.eval('{ [1] = -1 }')
>>> list(mapping)
[1]
>>> mapping = lua.eval('{ [20] = -20; [3] = -3 }')
>>> mapping[20]
-20
>>> mapping[3]
-3
>>> sorted(mapping.values())
[-20, -3]
>>> sorted(mapping.items())
[(3, -3), (20, -20)]
>>> mapping[-3] = 3 # -3 used as key, not index!
>>> mapping[-3]
3
>>> sorted(mapping)
[-3, 3, 20]
>>> sorted(mapping.items())
[(-3, 3), (3, -3), (20, -20)]
>>> table[1]
10
>>> table[4]
40
>>> list(table)
[1, 2, 3, 4]
>>> list(table.values())
[10, 20, 30, 40]
>>> len(table)
4
>>> mapping = lua.eval('{ [1] = -1 }')
>>> list(mapping)
[1]
>>> mapping = lua.eval('{ [20] = -20; [3] = -3 }')
>>> mapping[20]
-20
>>> mapping[3]
-3
>>> sorted(mapping.values())
[-20, -3]
>>> sorted(mapping.items())
[(3, -3), (20, -20)]
>>> mapping[-3] = 3 # -3 used as key, not index!
>>> mapping[-3]
3
>>> sorted(mapping)
[-3, 3, 20]
>>> sorted(mapping.items())
[(-3, 3), (3, -3), (20, -20)]
(等等……)
参考:
1. http://pypi.python.org/pypi/lupa/0.18
2. http://androguard.blogspot.com/2010/11/lupa-lua-from-python.html
作者:CoderZh(CoderZh的技术博客 - 博客园)
微博:http://t.sina.com.cn/coderzh
出处:http://coderzh.cnblogs.com
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
- Lupa - Python中调用Lua(收藏)
- Lupa - Python中调用Lua
- python中调用lua——(lupa)
- [python][lua]在python里调用Lua
- 使用C++调用 LUA 函数收藏
- C++中调用Lua
- lua中调用C
- C#中调用lua
- lua中调用C#
- LUA中调用C
- Lua 在Lua中调用C函数
- C#中调用dll收藏
- lua 中 调用 union 对象
- lua 中 调用 union 对象
- lua中调用c++函数
- C/C++中调用Lua
- 在lua中调用DLL
- 在lua中调用C++
- java编程练习50题(含答案)
- STL-学习笔记:关联容器
- 项目报警机制
- 该用户与可信SQL server 连接无关联 18452
- 动态规划之01背包问题(最易理解的讲解)
- Lupa - Python中调用Lua(收藏)
- dpkg error trying to overwrite
- Havok动画渲染Demo(使用Ogre)
- SQL Server2005 查询表列名 表说明
- 如何让div显示在最上层(页面含Flash)
- 3D视频揭秘
- Havok Physics物理引擎的学习入门
- C#操作word
- 如何快速查找某个存储过程执行的状况