aardio扩展库:调用Python函数(例如itertools.permutations和itertools.combinations)
By
jerryxjr1220
at 2021-10-05 • 0人收藏 • 1915人看过
Python中有非常多比较好用的函数,比如排列函数itertools.permutations和itertools.combinations,对应高数中的P(m,n)和C(m,n)。
但是,由于python输出到aardio中的都是pyObject对象,无法直接被调用,因此做了一个扩展库,用web.json来解析(json中的列表和python的列表语法一致)
调用openpyxl直接读取excel(不用安装office),见2楼。
import console; console.open(); import python; console.dumpTable(python.permutations({"我","爱","你"},2)) console.dumpTable(python.combinations({"我","爱","你"},2)) console.pause();
//Python函数调用库 namespace python{ permutations = function(list, n){ import string.template; import web.json; import py3; strlist = web.json.stringifyArray(list); if !n {n=#list}; pycode = string.template(); pycode.template = /*** import itertools p = list(itertools.permutations(${list}, ${n})) ***/ pycode = pycode.format({list=strlist, n=tostring(n)}); py3.exec(pycode); return web.json.parse(tostring(py3.main.p)); }; combinations = function(list, n){ import string.template; import web.json; import py3; strlist = web.json.stringifyArray(list); if !n {n=#list}; pycode = string.template(); pycode.template = /*** import itertools c = list(itertools.combinations(${list}, ${n})) ***/ pycode = pycode.format({list=strlist, n=tostring(n)}); py3.exec(pycode); return web.json.parse(tostring(py3.main.c)); } }
14 个回复 | 最后更新于 2021-10-10
2021-10-05
#2
//调用openpyxl直接读取excel,不用安装office console.dump(python.readxls("C:\\Users\\Administrator\\Desktop\\aardio\\project\\test\\res\\test.xlsx"))
//Python函数调用库 namespace python{ permutations = function(list, n){ import string.template; import web.json; import py3; strlist = web.json.stringifyArray(list); if !n {n=#list}; pycode = string.template(); pycode.template = /*** import itertools p = list(itertools.permutations(${list}, ${n})) ***/ pycode = pycode.format({list=strlist, n=tostring(n)}); py3.exec(pycode); return web.json.parse(tostring(py3.main.p)); }; combinations = function(list, n){ import string.template; import web.json; import py3; strlist = web.json.stringifyArray(list); if !n {n=#list}; pycode = string.template(); pycode.template = /*** import itertools c = list(itertools.combinations(${list}, ${n})) ***/ pycode = pycode.format({list=strlist, n=tostring(n)}); py3.exec(pycode); return web.json.parse(tostring(py3.main.c)); } readxls = function(path, sheet_no=0){ import py3; import web.json; import string.template; //这里的路径改成你实际的openpyxl包的路径 py3.appendPath("C:\Users\Administrator\anaconda3\Lib\site-packages"); pycode = string.template(); pycode.template = /*** import openpyxl wb = openpyxl.open('${path}') ws = wb.worksheets[${sheet_no}] values = list(ws.values) ***/ pycode = pycode.format( path = path; sheet_no = tostring(sheet_no) ) py3.exec(pycode); return web.json.parse(tostring(py3.main.values)); } }
直接用就行吧,注意下参数类型就行
import console; import py3; var pyCode = /** def permutations(iterable,r=""): from itertools import permutations if r!="": return list(permutations(iterable,int(r))) else: return list(permutations(iterable)) **/ py3.exec( pyCode ) var pyList = py3.main.permutations("甲乙丙"); console.dump(pyList.parseValue()) pyList = py3.main.permutations("甲乙丙",2); console.dump(pyList.parseValue()) pyList = py3.main.permutations({"甲","乙","丙"}); console.dump(pyList.parseValue()) pyList = py3.main.permutations({"甲","乙","丙"},2); console.dump(pyList.parseValue()) console.pause()
v33.15.4 更新后更加简单了,直接在aardio调用就行了
import console; import py3; var itertools = py3.import("itertools"); var permutations = function(iterable,r){ r = r ? py3.int(r) : r; return itertools.permutations(iterable,r); } /* 转为list */ var list = py3.list(permutations("abc")); console.dump(list.parseValue()) /* 直接遍历 */ for( item in permutations("abc",2).each() ){ console.dump(item.parseValue()) } console.pause(true);
import console console.open() import py3 var n = 30; py3.execf(` import math print(math.factorial(%d)) `, n) console.log(py3.main.math.factorial(n).parseValue()) console.pause();
输出为
265252859812191058636308480000000 2.6525285981219e+032 请按任意键继续 ...
请教下, 有没有办法在 aardio 里面取得py的输出结果,而不是科学记数法的结果?
回复#9 @mouse :
在 \lib\py3\_.aardio 269行 int = builtin.int; 之后加个 str = builtin.str;
然后调用py3.str把返回值转str类型在输出就行了
import console console.open() import py3 var n = 30; py3.execf(` import math print(math.factorial(%d)) `, n) console.dump(py3.str(py3.main.math.factorial(n)).parseValue()) console.pause();
登录后方可回帖
抛砖引玉,其实可以用这个方法调用非常多的python函数,只要是python的输出字符串,数字,列表和字典,先全部转字符串,然后在aardio中用web.json就能解析过来。