• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 319, comments - 22, trackbacks - 0, articles - 11
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            Lupa - Python中調用Lua

            Posted on 2011-09-15 06:52 RTY 閱讀(1430) 評論(0)  編輯 收藏 引用 所屬分類: Python轉載隨筆Lua專題
            http://www.cnblogs.com/coderzh/archive/2010/11/29/lupa.html

            Lupa - Python中調用Lua

            Lupa將LuaJIT集成到了Python模塊中,可以在Python中執行Lua代碼。 比較有意思,也許以后用的著,記錄一下。

            基本用法:

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

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


            Lua中的Python對象 

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


            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


            Lua中的Table: 

            >>> table = lua.eval('{10,20,30,40}')
            >>> table[1]
            10
            >>> table[4]
            40
            >>> list(table)
            [
            1234]
            >>> list(table.values())
            [
            10203040]
            >>> 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)
            [
            -3320]
            >>> sorted(mapping.items())
            [(
            -33), (3-3), (20-20)]


            (等等……)

             

            參考:

            1. http://pypi.python.org/pypi/lupa/0.18

            2. http://androguard.blogspot.com/2010/11/lupa-lua-from-python.html

            久久精品国产WWW456C0M| 久久久久久亚洲Av无码精品专口 | 国产精品一久久香蕉产线看| 狼狼综合久久久久综合网| 国产一级做a爰片久久毛片| 99久久99久久精品国产片果冻| 国产伊人久久| 久久久久亚洲Av无码专| 久久久久无码中| 99久久无色码中文字幕| 亚洲日本va午夜中文字幕久久 | 亚洲欧洲久久久精品| 色综合久久综合中文综合网| 国产精品内射久久久久欢欢| 777午夜精品久久av蜜臀| 久久久久无码精品| 久久精品国产99国产精品澳门| 久久笫一福利免费导航| 成人精品一区二区久久| avtt天堂网久久精品| 国色天香久久久久久久小说| 久久久国产精华液| 一本大道加勒比久久综合| 亚洲国产精品久久久天堂| 一级做a爰片久久毛片免费陪| 亚洲国产成人久久综合碰碰动漫3d| 精品久久人人爽天天玩人人妻| 久久99精品免费一区二区| 色综合合久久天天综合绕视看| 亚洲av成人无码久久精品| 囯产极品美女高潮无套久久久 | 久久久综合香蕉尹人综合网| 精品人妻久久久久久888| 久久久久亚洲AV无码网站| A级毛片无码久久精品免费| 国产精品99久久久精品无码| 亚洲国产精品高清久久久| 天天躁日日躁狠狠久久| 久久国产精品99精品国产| 狠狠狠色丁香婷婷综合久久五月| 777米奇久久最新地址|