|
| 1 | +# python-bridge [](http://travis-ci.org/Submersible/node-python-bridge) [](https://ci.appveyor.com/project/munro/node-python-bridge/branch/master) |
| 2 | + |
| 3 | +Most robust and simple Python bridge. [Features](#features), and [comparisons](#comparisons) to other Python bridges below, supports Windows. |
| 4 | + |
| 5 | +# API for TypeScript |
| 6 | + |
| 7 | +[View documentation with TypeScript examples.](README.ts.md) |
| 8 | + |
| 9 | +``` |
| 10 | +npm install python-bridge |
| 11 | +``` |
| 12 | + |
| 13 | +```typescript |
| 14 | +import assert from 'assert'; |
| 15 | +import { pythonBridge } from 'python-bridge'; |
| 16 | + |
| 17 | +async function main() { |
| 18 | + const python = pythonBridge(); |
| 19 | + |
| 20 | + await python.ex`import math`; |
| 21 | + const x = await python`math.sqrt(9)`; |
| 22 | + assert.equal(x, 3); |
| 23 | + |
| 24 | + const list = [3, 4, 2, 1]; |
| 25 | + const sorted = await python`sorted(${list})`; |
| 26 | + assert.deepEqual(sorted, list.sort()); |
| 27 | + |
| 28 | + await python.end(); |
| 29 | +} |
| 30 | + |
| 31 | +main().catch(console.error); |
| 32 | +``` |
| 33 | + |
| 34 | +## var python = pythonBridge(options) |
| 35 | + |
| 36 | +Spawns a Python interpreter, exposing a bridge to the running processing. Configurable via `options`. |
| 37 | + |
| 38 | +* `options.python` - Python interpreter, defaults to `python` |
| 39 | + |
| 40 | +Also inherits the following from [`child_process.spawn([options])`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options). |
| 41 | + |
| 42 | +* `options.cwd` - String Current working directory of the child process |
| 43 | +* `options.env` - Object Environment key-value pairs |
| 44 | +* `options.stdio` - Array Child's stdio configuration. Defaults to `['pipe', process.stdout, process.stderr]` |
| 45 | +* `options.uid` - Number Sets the user identity of the process. |
| 46 | +* `options.gid` - Number Sets the group identity of the process. |
| 47 | + |
| 48 | +```javascript |
| 49 | +const python = pythonBridge({ |
| 50 | + python: 'python3', |
| 51 | + env: {PYTHONPATH: '/foo/bar'} |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +## python`` `expression(args...)` ``.then(...) |
| 56 | + |
| 57 | +Evaluates Python code, returning the value back to Node. |
| 58 | + |
| 59 | +```javascript |
| 60 | +// Interpolates arguments using JSON serialization. |
| 61 | +assert.deepEqual([1, 3, 4, 6], await python`sorted(${[6, 4, 1, 3]})`); |
| 62 | + |
| 63 | +// Passing key-value arguments |
| 64 | +const obj = {hello: 'world', foo: 'bar'}; |
| 65 | +assert.deepEqual( |
| 66 | + {baz: 123, hello: 'world', foo: 'bar'}, |
| 67 | + await python`dict(baz=123, **${obj})` |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +## python.ex`` `statement` ``.then(...) |
| 72 | + |
| 73 | +Execute Python statements. |
| 74 | + |
| 75 | +```javascript |
| 76 | +const a = 123, b = 321; |
| 77 | +python.ex` |
| 78 | + def hello(a, b): |
| 79 | + return a + b |
| 80 | +`; |
| 81 | +assert.equal(a + b, await python`hello(${a}, ${b})`); |
| 82 | +``` |
| 83 | + |
| 84 | +## python.lock(...).then(...) |
| 85 | + |
| 86 | +Locks access to the Python interpreter so code can be executed atomically. If possible, it's recommend to define a function in Python to handle atomicity. |
| 87 | + |
| 88 | +```javascript |
| 89 | +const x: number = await python.lock(async python =>{ |
| 90 | + await python.ex`hello = 123`; |
| 91 | + return await python`hello + 321`; |
| 92 | +}); |
| 93 | +assert.equal(x, 444); |
| 94 | + |
| 95 | +// Recommended to define function in Python |
| 96 | +await python.ex` |
| 97 | + def atomic(): |
| 98 | + hello = 123 |
| 99 | + return hello + 321 |
| 100 | +`; |
| 101 | +assert.equal(444, await python`atomic()`); |
| 102 | +``` |
| 103 | + |
| 104 | +## python.stdin, python.stdout, python.stderr |
| 105 | + |
| 106 | +Pipes going into the Python process, separate from execution & evaluation. This can be used to stream data between processes, without buffering. |
| 107 | + |
| 108 | +```javascript |
| 109 | +import { delay, promisifyAll } from 'bluebird'; |
| 110 | +const { createWriteStream, readFileAsync } = promisifyAll(require('fs')); |
| 111 | + |
| 112 | +const fileWriter = createWriteStream('hello.txt'); |
| 113 | +python.stdout.pipe(fileWriter); |
| 114 | + |
| 115 | +// listen on Python process's stdout |
| 116 | +const stdinToStdout = python.ex` |
| 117 | + import sys |
| 118 | + for line in sys.stdin: |
| 119 | + sys.stdout.write(line) |
| 120 | + sys.stdout.flush() |
| 121 | +`; |
| 122 | + |
| 123 | +// write to Python process's stdin |
| 124 | +python.stdin.write('hello\n'); |
| 125 | +await delay(10); |
| 126 | +python.stdin.write('world\n'); |
| 127 | + |
| 128 | +// close python's stdin, and wait for python to finish writing |
| 129 | +python.stdin.end(); |
| 130 | +await stdinToStdout; |
| 131 | + |
| 132 | +// assert file contents is the same as what was written |
| 133 | +const fileContents = await readFileAsync('hello.txt', {encoding: 'utf8'}); |
| 134 | +assert.equal(fileContents.replace(/\r/g, ''), 'hello\nworld\n'); |
| 135 | +``` |
| 136 | + |
| 137 | +## python.end() |
| 138 | + |
| 139 | +Stops accepting new Python commands, and waits for queue to finish then gracefully closes the Python process. |
| 140 | + |
| 141 | +## python.disconnect() |
| 142 | + |
| 143 | +_Alias to [`python.end()`](#python-end)_ |
| 144 | + |
| 145 | +## python.kill([signal]) |
| 146 | + |
| 147 | +Send signal to Python process, same as [`child_process child.kill`](https://nodejs.org/api/child_process.html#child_process_event_exit). |
| 148 | + |
| 149 | +```javascript |
| 150 | +import { TimeoutError } from 'bluebird'; |
| 151 | + |
| 152 | +let python = pythonBridge(); |
| 153 | + |
| 154 | +try { |
| 155 | + await python.ex` |
| 156 | + from time import sleep |
| 157 | + sleep(9000) |
| 158 | + `.timeout(100); |
| 159 | + assert.ok(false); // should not reach this |
| 160 | +} catch (e) { |
| 161 | + if (e instanceof TimeoutError) { |
| 162 | + python.kill('SIGKILL'); |
| 163 | + python = pythonBridge(); |
| 164 | + } else { |
| 165 | + throw e; |
| 166 | + } |
| 167 | +} |
| 168 | +python.end(); |
| 169 | +``` |
| 170 | + |
| 171 | +# Handling Exceptions |
| 172 | + |
| 173 | +We can use Bluebird's [`promise.catch(...)`](http://bluebirdjs.com/docs/api/catch.html) catch handler in combination with Python's typed Exceptions to make exception handling easy. |
| 174 | + |
| 175 | + |
| 176 | +## python.Exception |
| 177 | + |
| 178 | +Catch any raised Python exception. |
| 179 | + |
| 180 | +```javascript |
| 181 | +python.ex` |
| 182 | + hello = 123 |
| 183 | + print(hello + world) |
| 184 | + world = 321 |
| 185 | +`.catch(python.Exception, () => console.log('Woops! `world` was used before it was defined.')); |
| 186 | +``` |
| 187 | + |
| 188 | +## python.isException(name) |
| 189 | + |
| 190 | +Catch a Python exception matching the passed name. |
| 191 | + |
| 192 | +```javascript |
| 193 | +import { isPythonException } from 'python-bridge'; |
| 194 | + |
| 195 | +async function pyDivide(numerator, denominator) { |
| 196 | + try { |
| 197 | + await python`${numerator} / ${denominator}`; |
| 198 | + } catch (e) { |
| 199 | + if (isPythonException('ZeroDivisionError', e)) { |
| 200 | + return Infinity; |
| 201 | + } |
| 202 | + throw e; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +async function main() { |
| 207 | + assert.equal(Infinity, await pyDivide(1, 0)); |
| 208 | + assert.equal(1 / 0, await pyDivide(1, 0)); |
| 209 | +} |
| 210 | + |
| 211 | +main().catch(console.error); |
| 212 | +``` |
| 213 | + |
| 214 | +## pythonBridge.PythonException |
| 215 | + |
| 216 | +_Alias to `python.Exception`, this is useful if you want to import the function to at the root of the module._ |
| 217 | + |
| 218 | +## pythonBridge.isPythonException |
| 219 | + |
| 220 | +_Alias to `python.isException`, this is useful if you want to import the function to at the root of the module._ |
| 221 | + |
| 222 | +---- |
| 223 | + |
| 224 | +# Features |
| 225 | + |
| 226 | +* Does not affect Python's stdin, stdout, or stderr pipes. |
| 227 | +* Exception stack traces forwarded to Node for easy debugging. |
| 228 | +* Python 2 & 3 support, end-to-end tested. |
| 229 | +* Windows support, end-to-end tested. |
| 230 | +* Command queueing, with promises. |
| 231 | +* Long running Python sessions. |
| 232 | +* ES6 template tags for easy interpolation & multiline code. |
| 233 | + |
| 234 | +# Comparisons |
| 235 | + |
| 236 | +After evaluating of the existing landscape of Python bridges, the following issues are why python-bridge was built. |
| 237 | + |
| 238 | +* [python-shell](https://github.com/extrabacon/python-shell) — No promises for queued requests; broken evaluation parser; conflates evaluation and stdout; complex configuration. |
| 239 | +* [python](https://github.com/73rhodes/node-python) — Broken evaluation parsing; no exception handling; conflates evaluation, stdout, and stderr. |
| 240 | +* [node-python](https://github.com/JeanSebTr/node-python) — Complects execution protocol with incomplete Python embedded DSL. |
| 241 | +* [python-runner](https://github.com/teamcarma/node-python-runner) — No long running sessions; `child_process.spawn` wrapper with unintuitive API; no serialization. |
| 242 | +* [python.js](https://github.com/monkeycz/python.js) — Embeds specific version of CPython; requires compiler and CPython dev packages; incomplete Python embedded DSL. |
| 243 | +* [cpython](https://github.com/eljefedelrodeodeljefe/node-cpython) — Complects execution protocol with incomplete Python embedded DSL. |
| 244 | +* [eval.py](https://www.npmjs.com/package/eval.py) — Can only evaluate single line expressions. |
| 245 | +* [py.js](https://www.npmjs.com/package/py.js) — For setting up virtualenvs only. |
| 246 | + |
| 247 | +# License |
| 248 | + |
| 249 | +MIT |
0 commit comments