Eshkation
Eshkation

Reputation: 147

Create a lua api with python

I was looking for ways to make python read lua scripts and execute functions, like this:

function foo()
*call python "test" def*
end
function bar()
print('honk')
end

on python:

def test():
    print('tonk')
    *call lua function "bar"

is there a way to make this?

Upvotes: 0

Views: 769

Answers (2)

Dev
Dev

Reputation: 277

You can do it using lunatic library which can called from either side. https://labix.org/lunatic-python

Upvotes: 1

Motine
Motine

Reputation: 1872

There is a package called lupa. It seems like it does exactly what you want (taken from their example):

>>> import lupa
>>> from lupa import LuaRuntime
>>> lua = LuaRuntime(unpack_returned_tuples=True)

>>> 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

Upvotes: 3

Related Questions