Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active June 16, 2017 04:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/fba8c63ea7e737046034 to your computer and use it in GitHub Desktop.
Save kjunichi/fba8c63ea7e737046034 to your computer and use it in GitHub Desktop.
mruby-python

mruby-python

Juliaとmrubyの連携

Tensorflow

Juliaも使えるように欲張ってやってみた

この下のメモの構想をmruby-juliaというモジュールでやり始めた

目的

numpyとかPythonのすごいライブラリをmrubyから扱えるようにすることでmrubyの科学技術計算関連への 応用を探る。

使えそうなAPI

Py_SetProgramName(argv[0]);  /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
Py_Finalize();
Py_Initialize();
pName = PyString_FromString(argv[1]);
pModule = PyImport_Import(pName);
Py_DECREF(pName);
pFunc = PyObject_GetAttrString(pModule, argv[2]);
if (pFunc && PyCallable_Check(pFunc)) {
  pArgs = PyTuple_New(argc - 3);
  /* pValue reference stolen here: */
  PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
  printf("Result of call: %ld\n", PyInt_AsLong(pValue));
  Py_DECREF(pValue);
}

jsonでmrubyとpython間でやり取りする。

作業で必要なコマンドなど

python2.7-config --cflags
python2.7-config --ldflags

windows版どうしよう。

楽して作りたい

JulialangのPyCallが参考にならないか

Julialangオンリーで記述されてる可能性がある。

# evaluate a python string, returning PyObject, given a dictionary
# (string/symbol => value) of local variables to use in the expression
function pyeval_(s::AbstractString, locals::PyDict, input_type)
    sb = bytestring(s) # use temp var to prevent gc before we are done with o
    sigatomic_begin()
    try
        o = PyObject(@pycheckn ccall((@pysym :Py_CompileString), PyPtr,
                                     (Cstring, Cstring, Cint),
                                     sb, pyeval_fname, input_type))
        main = @pycheckn ccall((@pysym :PyImport_AddModule),
                                PyPtr, (Cstring,), "__main__")
        maindict = @pycheckn ccall((@pysym :PyModule_GetDict), PyPtr,
                                    (PyPtr,), main)
        return PyObject(@pycheckn ccall((@pysym :PyEval_EvalCode),
                                         PyPtr, (PyPtr, PyPtr, PyPtr),
                                         o, maindict, locals))
    finally
        sigatomic_end()
    end
end

PyObjectとは

#########################################################################
# Wrapper around Python's C PyObject* type, with hooks to Python reference
# counting and conversion routines to/from C and Julia types.
"""
    PyObject(juliavar)
This converts a julia variable to a PyObject, which is a reference to a Python object.
You can convert back to native julia types using `convert(T, o::PyObject)`, or using `PyAny(o)`.
Given `o::PyObject`, `o[:attribute]` is equivalent to `o.attribute` in Python, with automatic type conversion.
Given `o::PyObject`, `get(o, key)` is equivalent to `o[key]` in Python, with automatic type conversion.
"""
type PyObject
    o::PyPtr # the actual PyObject*
    function PyObject(o::PyPtr)
        po = new(o)
        finalizer(po, pydecref)
        return po
    end
end

juliaからlibmruby.soを呼び出せば、Cフリーでjulia<->python<->mrubyが行き来できないかなぁ

Julialangを組み込むに当たっての個人メモ

関連記事

関連

アクセス解析タグ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment