PyMiniRacer 是 Python 中的一个 V8 桥接器。
此包现已过时。请前往 https://github.com/bpcreech/PyMiniRacer 获取最新版本。
.. image:: https://img.shields.io/pypi/v/py_mini_racer.svg :target: https://pypi.python.org/pypi/py_mini_racer
from py_mini_racer import MiniRacer ctx = MiniRacer() ctx.eval("1+1") ctx.eval("var x = {company: 'Sqreen'}; x.company") 'Sqreen' print(ctx.eval("'\N{HEAVY BLACK HEART}'")) ❤ ctx.eval("var fun = () => ({ foo: 1 });")
变量保存在上下文中:
.. code-block:: Python
ctx.eval("x.company") 'Sqreen'
eval 只支持返回基本数据类型,如字符串,而 call 支持返回复合类型,如对象:
.. code-block:: Python
ctx.call("fun") {'foo': 1}
复合值使用 JSON 进行序列化。在发送非 JSON 可编码参数时,请使用自定义 JSON 编码器:
.. code-block:: Python import json from datetime import datetime class CustomEncoder(json.JSONEncoder): def default(self, obj): 如果 isinstance(obj, datetime): return obj.isoformat() return json.JSONEncoder.default(self, obj) .. code-block:: Python
ctx.eval("var f = function(args) { return args; }") ctx.call("f", datetime.now(), encoder=CustomEncoder) '2017-03-31T16:51:02.474118' MiniRacer 支持 ES6: .. code-block:: Python ctx.execute("[1,2,3].includes(5)" 错误 可以检索 V8 堆信息:
2
暂无开放 Issues,或尚未同步最近议题。