Native bindings to run python in its native interpreter.
This Library is in alpha status. Do not use for fun or profit
Sometimes you want to use Python scripts or even whole libraries, but you don't want to rely on child_process.exec()
or child_process.spawn()
. This module initializes the standard Python interpreter and passes Py code to it.
Method | implemented |
---|---|
.anyFile() | - |
.simpleString(str, [flags], [cb]) | yes * |
.simpleFile() | - |
.repl() | yes * |
.simpleParseString() | - |
.simpleParseFile() | - |
.string() | - |
.file() | - |
.compileString() | - |
.evalCode() | - |
.evalFrame() | - |
* Will be extended, but not directly in JS user space. It will be optional to pass more than the required params so the functionality of the CPython-API will be implicitly usable. This includes closeits and compiler flags.
The following shall give background information and explain why you want to use this.
In order to compile C code with Emscripten you'd have to run the Python script, which utilizes LLVM. However working with child_process.exec()
or chold_process.spawn()
seems odd, since it neither safe nor does it handle errors nor is it platform independent.
So in order to run scripts programmatically it seemed a good idea to use the the perfect legit Python.h
C-header in the standard implementation of Python.
Rquirements:
- Node 4.0.0+
This module is currently tested on:
Platform | Node | IO.js |
---|---|---|
Mac OS X | - | yes |
Linux | - | yes |
Windows | - | - |
Please see list of the implemented methods for now.
Kind: global class
- CPython
- new CPython()
- .init(arguments) ⇒
Object
- .run(glob, [cb])
- .anyFile()
- .runString()
- .repl()
- .simpleString(str, [flags], [cb])
- .simpleFile(filepath, filename, [flags], [cb])
- .callForeignFunction(file, functioname) ⇒
function
- .ffi(file, functionname) ⇒
function
- .interactiveOne()
- .interactiveLoop()
- .simpleParseString()
- .simpleParseFile()
- .string()
- .file()
- .compileString()
- .evalCode()
- .evalFrame()
- .initialize()
- .finalize(callback)
- .setProgramName(Program)
- .setArgv(string)
- .pyCreateContext(python)
Implements the CPython Python interpreter
intitialze this module from init function rather than over constructor
Kind: instance method of CPython
Returns: Object
- returns itself is chainable
Param | Type | Description |
---|---|---|
arguments | Object |
object where keys represent toggles of individual features or point to files |
Example
const cpython = require('node-cpython')
let options = {
\/\* Options go in here \*\/
}
cpython.init(options)
\/\/ available options [here](https://github.com/eljefedelrodeodeljefe/node-cpython#options)
Executes any number of Python source code files. This is JS userland API and automates and abstracts many choices of the below C-API. If you want to have more control, please use the below methods.
Kind: instance method of CPython
Param | Type | Description |
---|---|---|
glob | string | Array.<string> |
a glob of valid .py files |
[cb] | callback |
Optional callback |
Example
'use strict'
let cpython = require('cpython')
cpython.on('error', function(err) {console.log(err)})
cpython.run('[example/**\/*.py', function(result) { console.log(result) })
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Executes the Python source code from command. See also Python docs for Reference
Kind: instance method of CPython
Param | Type | Default | Description |
---|---|---|---|
str | string |
String of python denoted code | |
[flags] | string | Array.<string> |
null |
Compiler flag or array of flags for CPython |
[cb] | callback |
Optional callback |
Example
'use strict'
let cpython = require('cpython')
cpython.on('error', function(err) {console.log(err)})
cpython.simpleString('from time import time,ctime\nprint 'Today is',ctime(time())\n')
Executes the Python source code from file. Similar to simpleString(), but the Python source code is read from a file instead of an in-memory string. filename should be the name of the file. See also Python docs for Reference
Kind: instance method of CPython
Param | Type | Default | Description |
---|---|---|---|
filepath | string |
String of filepath | |
filename | string |
String of filename | |
[flags] | string | Array.<string> |
null |
Compiler flag or array of flags for CPython |
[cb] | callback |
Optional callback |
Example
'use strict'
let cpython = require('cpython')
cpython.on('error', function(err) {console.log(err)})
cpython.simpleFile('example/multiply.py', 'multiply.py')
// passing the filename seems to be a necessity of the C-API
// TODO: this will only last very shortly and be made optional
[callForeignFunction description]
Kind: instance method of CPython
Returns: function
- [description]
Param | Type | Description |
---|---|---|
file | string |
[description] |
functioname | string |
[description] |
[ffi description]
Kind: instance method of CPython
Returns: function
- [description]
Param | Type | Description |
---|---|---|
file | string |
[description] |
functionname | string |
[description] |
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
Kind: instance method of CPython
initialize python context, reserve memory.
Kind: instance method of CPython
Finalize python context, clear memory.
Kind: instance method of CPython
Param | Type | Description |
---|---|---|
callback | callback |
for completion of py context |
set low level python program name (optional)
Kind: instance method of CPython
Param | Type | Description |
---|---|---|
Program | string |
name. |
set low level python argv
Kind: instance method of CPython
Param | Type | Description |
---|---|---|
string | string | Array.<string> |
or an array of strings as argv argc is auto computed by the arrays length |
Create a context in memory to run the python script and injects a python function to run in.
Kind: instance method of CPython
Param | Type | Description |
---|---|---|
python | callback |
function to run in the memory |
MIT