CPyton

1.简介

    可以执行C语言版的Python,通过http://bytedeco.org/的cpython库执行。

2.设计思想

     作为动作,实现run()方法,在run()方法里执行模型code属性所写的代码。

     run方法代码如下:

public static Object run(ActionContext actionContext) throws IOException {
        Thing self = actionContext.getObject("self");

        Pointer program = Py_DecodeLocale(CPythonAction.class.getSimpleName(), null);
        if (program == null) {
            Executor.warn(TAG, "Fatal error: cannot decode class name");
        }
        try {
            Py_SetProgramName(program);  /* optional but recommended */
            Py_Initialize(cachePackages());
            String code = self.getStringBlankAsNull("code");
            if (code != null) {
                PyRun_SimpleString(code);
            }

            return Py_FinalizeEx();
        }finally {
            PyMem_RawFree(program);
        }
    }

3.示例

<?xml version='1.0' encoding='utf-8'?>

<CPythonAction name="CPythonAction" descriptors="xworker.cpython.CPythonAction">
    <code><![CDATA[from time import time,ctime
print('Today is', ctime(time()))]]></code>
</CPythonAction>

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号