Call Lua in Lupa-Python

Source: Internet
Author: User

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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.