前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python - 调用Lua

Python - 调用Lua

作者头像
AIHGF
发布2019-02-18 10:52:06
4.1K0
发布2019-02-18 10:52:06
举报
文章被收录于专栏:AIUAI

Python 调用Lua

lupaLuaLuaJIT2集成进CPython,可以在Python中执行Lua代码.

Lupa的主要特点: - separate Lua runtime states through a LuaRuntime class - Python coroutine wrapper for Lua coroutines - iteration support for Python objects in Lua and Lua objects in Python - proper encoding and decoding of strings (configurable per runtime, UTF-8 by default) - frees the GIL and supports threading in separate runtimes when calling into Lua解决了GIL问题,支持多线程 - tested with Python 2.6/3.2 and later 适用于Python 2.6/3.2以后的版本 - written for LuaJIT2 (tested with LuaJIT 2.0.2), but also works with the normal Lua interpreter (5.1 and 5.2) - easy to hack on and extend as it is written in Cython, not C 扩展性好

1. lupa安装

这里基于Ubuntu14.04. lupa官网提供的教程:

代码语言:javascript
复制
# ForDebian/Ubuntu + Lua 5.2
sudo apt-get install liblua5.2-dev  # Install Lua 5.2 development package
sudo pip install lupa  # Install lupa

# For Debian/Ubuntu + LuaJIT2
sudo apt-get install libluajit-5.1-dev  # Install LuaJIT2 development package
sudo pip install lupa  # Install lupa

源码安装方式如下:

1.1 安装lua环境

代码语言:javascript
复制
curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
tar zxf lua-5.3.4.tar.gz
cd lua-5.3.4
make linux test
make install

注:如果遇到错误:fatal error: readline/readline.h: No such file or directory,则需要安装readline-dev

代码语言:javascript
复制
sudo apt-get install libreadline-dev

测试是否安装成功: 新建 hellolua.lua脚本,代码如下:

代码语言:javascript
复制
print("Hello Lua!")

执行命令:

代码语言:javascript
复制
lua hellolua.lua

安装成功,则输出:

代码语言:javascript
复制
Hello Lua!

1.2 安装LuaJIT

代码语言:javascript
复制
wget -c http://luajit.org/download/LuaJIT-2.0.5.tar.gz

tar zxf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
sudo make install PREFIX=/usr/local/luajit
echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_luajit_lib.conf
sudo ldconfig

export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

1.3 lupa安装

代码语言:javascript
复制
sudo pip install lupa

2. 实例

基本用法

代码语言:javascript
复制
import lupa
from lupa import LuaRuntime

lua = LuaRuntime(unpack_returned_tuples=True)
lua.eval('1+1') # output: 2

lua_func = lua.eval('function(f, n) return f(n) end')

def py_add1(n): 
    return n+1

lua_func(py_add1, 2) # output: 3

lua.eval('python.eval(" 2 ** 2 ")') == 4 # output: True

lua.eval('python.builtins.str(4)') == '4' # output: True

# lua_type(obj) 函数用于输出 type of a wrapped Lua object
lupa.lua_type(lua_func) # output: 'function'
lupa.lua_type(lua.eval('{}')) # output: 'table'
lupa.lua_type(123) is None # output: True
lupa.lua_type('abc') is None # output: True
lupa.lua_type({}) is None # output: True

# flag unpack_returned_tuples=True that is passed to create the Lua runtime
lua.execute('a,b,c = python.eval("(1,2)")')
g = lua.globals()
g.a   # output: 1
g.b   # output: 2
g.c is None   # output: True

# flag unpack_returned_tuples=False, functions that return a tuple pass it through to the Lua code
non_explode_lua = lupa.LuaRuntime(unpack_returned_tuples=False)
non_explode_lua.execute('a,b,c = python.eval("(1,2)")')
g = non_explode_lua.globals()
g.a   # output: (1, 2)
g.b is None  # output: True
g.c is None  # output: True

更多用法参考lupa官网.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年05月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python 调用Lua
    • 1. lupa安装
      • 1.1 安装lua环境
      • 1.2 安装LuaJIT
      • 1.3 lupa安装
    • 2. 实例
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档