html tool

显示标签为“lua”的博文。显示所有博文
显示标签为“lua”的博文。显示所有博文

2014年6月24日星期二

lua试用log I

--add:http://coolshell.cn/articles/10739.html
--[[ print("hello,world")
     print("hi,here")--]]

    num=314.16e-2
    print(num)
    a=[[alo
    123"]]    --两个中括号可以用于定义有换行的字符串
    print(a)

-- while
sum=0
num=1
while num<= 100 do
sum =sum+num
num=num+1
end
print("sum=",sum)

-- if-else
-- local age=io.read()    -- io库的分别从stdin和stdout读写的read和write函数
-- [?]popexizhi:io.read()输入内容如何定义为数据类型呢?这个导致age>40不能使用
-- local sex=io.read()
age="40"
sex="Male"
print("your age is"..age.."\rsex is"..sex) --字符串的拼接操作符“..”
if age=="40" and "Male"==sex then
print "hi 40:)"
elseif age==60 and sex~="Female" then --“~=”是不等于,而不是!=
io.write("too old")
else
io.write("too young")
end

-- function
function myPower(x)
    return function(y) return y^x end
end

power2 = myPower(2)
power3 = myPower(3)

print(power2(4)) --4的2次方
print(power3(5)) --5的3次方

-- 函数返回多个值
function getUserInfo(id)
    print(id)
    return "haoel", 37, "haoel@hotmail.com", "http://coolshell.cn"
end

name, age, email, website, bGay = getUserInfo("2")
print(name.."\t"..age.."\t"..email.."\t"..website)
--[?]popexizhi:这里的bGay为nil ,但是引用时没有提示err,why,lua支持这样的语法吗?/ 25/6/14

2014年5月29日星期四

Lupa - Python中调用Lua


地址:http://www.cnblogs.com/coderzh/archive/2010/11/29/lupa.html
PS:刚刚才发现Arduino中可以直接运行lua脚本,嘻嘻这个是个更好的进一步学习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)
[
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)]
复制代码

(等等……)

参考: