Lupa integrates luajit into the python module and runs Lua in Python.Code. It's interesting. Maybe it will be used later. record it.
Basic usage: >>> Import Lupa
>>> From Lupa Import Luaruntime
>>> Lua = Luaruntime ()
>>>Lua. eval ('1 + 1')
2
>>>Lua_func=Lua. eval ('Function (F, n) return F (n) End')
>>> DefPy_add1 (n ):ReturnN+1
>>>Lua_func (py_add1,2)
3
>>> Lua. eval ( ' Python. eval ("2 ** 2 ") ' ) = 4
True
>>> Lua. eval ( ' Python. builtins. STR (4) ' ) = ' 4 '
True
Python objects in Lua >>> 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
Iteration loop in 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
Table in Lua:
>>> 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 )]
(Wait ......)
Refer:
1. http://pypi.python.org/pypi/lupa/0.18
2. http://androguard.blogspot.com/2010/11/lupa-lua-from-python.html