Skip to content

Commit dc94446

Browse files
authored
downgrading readme back to v0.5 while waiting for v1 publish
1 parent 7c1116a commit dc94446

File tree

1 file changed

+22
-89
lines changed

1 file changed

+22
-89
lines changed

README.md

+22-89
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,25 @@ npm test
2323

2424
## Documentation
2525

26-
### Running python code:
27-
28-
```typescript
29-
import {PythonShell} from 'python-shell';
30-
31-
PythonShell.runString('x=1+1;print(x)', function (err) {
32-
if (err) throw err;
33-
console.log('finished');
34-
});
35-
```
36-
37-
If the script exits with a non-zero code, an error will be thrown.
38-
3926
### Running a Python script:
4027

41-
```typescript
42-
import {PythonShell} from 'python-shell';
28+
```js
29+
var PythonShell = require('python-shell');
4330

4431
PythonShell.run('my_script.py', function (err) {
4532
if (err) throw err;
4633
console.log('finished');
4734
});
4835
```
4936

50-
If the script exits with a non-zero code, an error will be thrown.
37+
If the script writes to stderr or exits with a non-zero code, an error will be thrown.
5138

5239
### Running a Python script with arguments and options:
5340

54-
```typescript
55-
import {PythonShell} from 'python-shell';
41+
```js
42+
var PythonShell = require('python-shell');
5643

57-
let options = {
44+
var options = {
5845
mode: 'text',
5946
pythonPath: 'path/to/python',
6047
pythonOptions: ['-u'], // get print results in real-time
@@ -71,9 +58,9 @@ PythonShell.run('my_script.py', options, function (err, results) {
7158

7259
### Exchanging data between Node and Python:
7360

74-
```typescript
75-
import {PythonShell} from 'python-shell';
76-
let pyshell = new PythonShell('my_script.py');
61+
```js
62+
var PythonShell = require('python-shell');
63+
var pyshell = new PythonShell('my_script.py');
7764

7865
// sends a message to the Python script via stdin
7966
pyshell.send('hello');
@@ -105,10 +92,9 @@ For more details and examples including Python source code, take a look at the t
10592

10693
### Error Handling and extended stack traces
10794

108-
An error will be thrown if the process exits with a non-zero exit code. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace.
95+
An error will be thrown if the process exits with a non-zero exit code or if data has been written to stderr. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace.
10996

11097
Sample error with traceback (from test/python/error.py):
111-
11298
```
11399
Traceback (most recent call last):
114100
File "test/python/error.py", line 6, in <module>
@@ -117,10 +103,8 @@ Traceback (most recent call last):
117103
print 1/0
118104
ZeroDivisionError: integer division or modulo by zero
119105
```
120-
121106
would result into the following error:
122-
123-
```typescript
107+
```js
124108
{ [Error: ZeroDivisionError: integer division or modulo by zero]
125109
traceback: 'Traceback (most recent call last):\n File "test/python/error.py", line 6, in <module>\n divide_by_zero()\n File "test/python/error.py", line 4, in divide_by_zero\n print 1/0\nZeroDivisionError: integer division or modulo by zero\n',
126110
executable: 'python',
@@ -129,9 +113,7 @@ would result into the following error:
129113
args: null,
130114
exitCode: 1 }
131115
```
132-
133116
and `err.stack` would look like this:
134-
135117
```
136118
Error: ZeroDivisionError: integer division or modulo by zero
137119
at PythonShell.parseError (python-shell/index.js:131:17)
@@ -159,7 +141,6 @@ Creates an instance of `PythonShell` and starts the Python process
159141
* `binary`: data is streamed as-is through `stdout` and `stdin`
160142
* `formatter`: each message to send is transformed using this method, then appended with "\n"
161143
* `parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
162-
* `stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message
163144
* `encoding`: the text encoding to apply on the child process streams (default: "utf8")
164145
* `pythonPath`: The path where to locate the "python" executable. Default: "python"
165146
* `pythonOptions`: Array of option switches to pass to "python"
@@ -173,25 +154,23 @@ PythonShell instances have the following properties:
173154
* `command`: the full command arguments passed to the Python executable
174155
* `stdin`: the Python stdin stream, used to send data to the child process
175156
* `stdout`: the Python stdout stream, used for receiving data from the child process
176-
* `stderr`: the Python stderr stream, used for communicating logs & errors
157+
* `stderr`: the Python stderr stream, used for communicating errors
177158
* `childProcess`: the process instance created via `child_process.spawn`
178159
* `terminated`: boolean indicating whether the process has exited
179160
* `exitCode`: the process exit code, available after the process has ended
180161

181162
Example:
182-
183-
```typescript
163+
```js
184164
// create a new instance
185-
let shell = new PythonShell('script.py', options);
165+
var shell = new PythonShell('script.py', options);
186166
```
187167

188168
#### `#defaultOptions`
189169

190170
Configures default options for all new instances of PythonShell.
191171

192172
Example:
193-
194-
```typescript
173+
```js
195174
// setup a default "scriptPath"
196175
PythonShell.defaultOptions = { scriptPath: '../scripts' };
197176
```
@@ -203,53 +182,32 @@ Runs the Python script and invokes `callback` with the results. The callback con
203182
This method is also returning the `PythonShell` instance.
204183

205184
Example:
206-
207-
```typescript
185+
```js
208186
// run a simple script
209187
PythonShell.run('script.py', function (err, results) {
210188
// script finished
211189
});
212190
```
213191

214-
#### `#runString(code, options, callback)`
215-
216-
Runs the Python code and invokes `callback` with the results. The callback contains the execution error (if any) as well as an array of messages emitted from the Python script.
217-
218-
This method is also returning the `PythonShell` instance.
219-
220-
Example:
221-
222-
```typescript
223-
// run a simple script
224-
PythonShell.run('x=1;print(x)', function (err, results) {
225-
// script finished
226-
});
227-
```
228-
229192
#### `.send(message)`
230193

231194
Sends a message to the Python script via stdin. The data is formatted according to the selected mode (text or JSON), or through a custom function when `formatter` is specified.
232195

233196
Example:
234-
235-
```typescript
197+
```js
236198
// send a message in text mode
237-
let shell = new PythonShell('script.py', { mode: 'text '});
199+
var shell = new PythonShell('script.py', { mode: 'text '});
238200
shell.send('hello world!');
239201

240202
// send a message in JSON mode
241-
let shell = new PythonShell('script.py', { mode: 'json '});
203+
var shell = new PythonShell('script.py', { mode: 'json '});
242204
shell.send({ command: "do_stuff", args: [1, 2, 3] });
243205
```
244206

245207
#### `.receive(data)`
246208

247209
Parses incoming data from the Python script written via stdout and emits `message` events. This method is called automatically as data is being received from stdout.
248210

249-
#### `.receiveStderr(data)`
250-
251-
Parses incoming logs from the Python script written via stderr and emits `stderr` events. This method is called automatically as data is being received from stderr.
252-
253211
#### `.end(callback)`
254212

255213
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
@@ -258,50 +216,25 @@ Closes the stdin stream, allowing the Python script to finish and exit. The opti
258216

259217
Terminates the python script, the optional end callback is invoked if specified. A kill signal may be provided by `signal`, if `signal` is not specified SIGTERM is sent.
260218

261-
#### `checkSyntax(code:string)`
262-
263-
Checks the syntax of the code and returns a promise.
264-
Promise is rejected if there is a syntax error.
265-
266-
#### `checkSyntaxFile(filePath:string)`
267-
268-
Checks the syntax of the file and returns a promise.
269-
Promise is rejected if there is a syntax error.
270-
271219
#### event: `message`
272220

273221
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. If a `parser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
274222

275223
Example:
276-
277-
```typescript
224+
```js
278225
// receive a message in text mode
279-
let shell = new PythonShell('script.py', { mode: 'text '});
226+
var shell = new PythonShell('script.py', { mode: 'text '});
280227
shell.on('message', function (message) {
281228
// handle message (a line of text from stdout)
282229
});
283230

284231
// receive a message in JSON mode
285-
let shell = new PythonShell('script.py', { mode: 'json '});
232+
var shell = new PythonShell('script.py', { mode: 'json '});
286233
shell.on('message', function (message) {
287234
// handle message (a line of text from stdout, parsed as JSON)
288235
});
289236
```
290237

291-
#### event: `stderr`
292-
293-
Fires when a chunk of logs is parsed from the stderr stream via the `receiveStderr` method. If a `stderrParser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
294-
295-
Example:
296-
297-
```typescript
298-
// receive a message in text mode
299-
let shell = new PythonShell('script.py', { mode: 'text '});
300-
shell.on('stderr', function (stderr) {
301-
// handle stderr (a line of text from stderr)
302-
});
303-
```
304-
305238
#### event: `close`
306239

307240
Fires when the process has been terminated, with an error or not.

0 commit comments

Comments
 (0)