Giter VIP home page Giter VIP logo

node-cpython's Introduction

node-cpython

Native bindings to run python in its native interpreter.

This Library is in alpha status. Do not use for fun or profit

Build Status Build status js-standard-style

NPMNPM

TL;DR

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.

Implementation Status

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.

Introduction

The following shall give background information and explain why you want to use this.

Motivation

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.

Overview

Technical Overview

Rquirements:

  • Node 4.0.0+

Platform

This module is currently tested on:

Platform Node IO.js
Mac OS X - yes
Linux - yes
Windows - -

Roadmap

Please see list of the implemented methods for now.

API

CPython

Kind: global class

new CPython()

Implements the CPython Python interpreter

cPython.init(arguments) ⇒ Object

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)

cPython.run(glob, [cb])

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) })

cPython.anyFile()

Kind: instance method of CPython

cPython.runString()

Kind: instance method of CPython

cPython.repl()

Kind: instance method of CPython

cPython.simpleString(str, [flags], [cb])

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')

cPython.simpleFile(filepath, filename, [flags], [cb])

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

cPython.callForeignFunction(file, functioname) ⇒ function

[callForeignFunction description]

Kind: instance method of CPython
Returns: function - [description]

Param Type Description
file string [description]
functioname string [description]

cPython.ffi(file, functionname) ⇒ function

[ffi description]

Kind: instance method of CPython
Returns: function - [description]

Param Type Description
file string [description]
functionname string [description]

cPython.interactiveOne()

Kind: instance method of CPython

cPython.interactiveLoop()

Kind: instance method of CPython

cPython.simpleParseString()

Kind: instance method of CPython

cPython.simpleParseFile()

Kind: instance method of CPython

cPython.string()

Kind: instance method of CPython

cPython.file()

Kind: instance method of CPython

cPython.compileString()

Kind: instance method of CPython

cPython.evalCode()

Kind: instance method of CPython

cPython.evalFrame()

Kind: instance method of CPython

cPython.initialize()

initialize python context, reserve memory.

Kind: instance method of CPython

cPython.finalize(callback)

Finalize python context, clear memory.

Kind: instance method of CPython

Param Type Description
callback callback for completion of py context

cPython.setProgramName(Program)

set low level python program name (optional)

Kind: instance method of CPython

Param Type Description
Program string name.

cPython.setArgv(string)

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

cPython.pyCreateContext(python)

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

License

MIT

node-cpython's People

Contributors

eljefedelrodeodeljefe avatar greenkeeperio-bot avatar gulpgitcentric avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.