从python运行javascript代码
PyJSCaller的Python项目详细描述
PyjsCaller
从python运行javascript代码。
pyjscaller是介于python和javascript之间的一个代理,它使javascript更像python语言。
一个简短的例子:
example.js
functionadd(a,b){returna+b;}
使用量
>>>importjscaller>>>jscaller.eval("'Hello World!'.toUpperCase()")'HELLO WORLD!'>>>withjscaller.Session('example.js',timeout=3)assess:...add=sess.get('add')...retval=add(add(1,2),2)...sess.call(retval)>>>retval.getValue()5
安装
$ pip install PyJSCaller
更多示例
另一个简短的例子
>>>fromjscaller.collectimportnew,String>>>withjscaller.Session()assess:...string=new(String("Hello JavaScript!"))...string.replace('JavaScript','Python')...sess.call(string)>>>string.getValue()"Hello Python!"
使用jscaller.make()装备其他jsengine:
>>>fromjscaller.engineimportNodeJS,PhantomJS>>>PhantomJS.environ(shell=True,timeout=5)>>>jscaller.make(PhantomJS)>>>jscaller.eval('1+1*2/4')1.5
您可以使用PhantomJS.test()检查引擎是否工作正常。
>>>PhantomJS.test()# return the version number of PhantomJS2.1.1
许可证
麻省理工学院许可证
更改日志
0.1.1
- 全部重建。
- 支持Linux。
- 支持Python3.7.x。
- 支持Python2.7.x。
0.0.1
- 已上载代码。