Thursday, February 21, 2013

Lua inside Python, Python inside Lua - Lunatic Python


(Fixing the no newlines issues in the post, with this edit. If any reader still notices any issue with formatting, please leave a comment and I'll check it again - thanks.)

Lunatic Python - Labix

As the title of this post says, Lunatic Python (nice name - see below for why :-), is a software bridge, that enables two-way communication between Python and Lua.

From the site: "Being two-way means that it allows Lua inside Python, Python inside Lua, Lua inside Python inside Lua, Python inside Lua inside Python, and so on."

I only saw it today, so have not tried it out much yet, but from reading some of the code examples, it looks pretty interesting. Here are a few examples from the site (pasted from mobile, some characters may get changed, check site for exact code):

[
Lua inside Python

A basic example.

>>> import lua
>>> lg = lua.globals()
>>> lg.string

>>> lg.string.lower

>>> lg.string.lower("Hello world!")
'hello world!'
Now, let's put a local object into Lua space.
>>> d = {}
>>> lg.d = d
>>> lua.execute("d['key'] = 'value'")
>>> d {'key': 'value'}
Can we get the reference back from Lua space?
>>> d2 = lua.eval("d")
>>> d is d2
True
Good! Is the python interface available inside the Lua interpreter?
>>> lua.eval("python") 
Yes, it looks so. Let's nest some evaluations and see a local reference passing through.
>>> class MyClass: pass
...
>>> obj = MyClass()
>>> obj <__main__.MyClass instance at 0x403ccb4c>
>>> lua.eval(r"python.eval('lua.eval(\"python.eval (\'obj\')\")')")
<__main__.MyClass instance at 0x403ccb4c>
Are you still following me? Good. Then you've probably noticed that the Python interpreter state inside the Lua interpreter state is the same as the outside Python we're running. Let's see that in a more comfortable way.
>>> lua.execute("pg = python.globals()")
>>> lua.eval("pg.obj")
<__main__.MyClass instance at 0x403ccb4c>

]

Though it is an experimental project, the author of Lunatic Python, Gustavo Niemeyer, says that it is being used in some real world applications.

Lua.

Lua on Wikipedia.

Lua is a (buzzwords ahead :) cross-platform (*), extensible, multi-paradigm, lightweight scripting language.

It is used a lot in real world applications, including games, an Adobe product, and other areas.

Lua means moon in Portuguese (it was developed at an institute in Brazil), that's why the name Lunatic Python is a good one, since lunatics are supposed to be influenced by the moon ...

I had first come across Lua some years ago when checking out various new or less known programming languages. Tried it out a bit at the basic level, not the extensibility aspects.

(*) It seems to have developed a lot since then, at least in platform support; the Lua home page says it runs on OS's from many mobile ones up to mainframes. When I first saw it, it only worked on Linux and Windows, IIRC.

- Vasudev Ram

2 comments:

Anonymous said...

the text and code is wrapping, at least in Chrome.

Vasudev Ram said...

I guess you didn't read the first few lines (where I said there are formatting issues due it initially being posted from mobile, and that I will fix that later from a PC).

Fixed now and removed that message.
Check.