Skip to content

Commit 9fbf80a

Browse files
committed
Revert "downgrading readme back to v0.5 while waiting for v1 publish"
Reverting revert back to new readme now that i can publish v1
1 parent dc94446 commit 9fbf80a

File tree

1 file changed

+89
-22
lines changed

1 file changed

+89
-22
lines changed

README.md

+89-22
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,38 @@ 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+
2639
### Running a Python script:
2740

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

3144
PythonShell.run('my_script.py', function (err) {
3245
if (err) throw err;
3346
console.log('finished');
3447
});
3548
```
3649

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

3952
### Running a Python script with arguments and options:
4053

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

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

5972
### Exchanging data between Node and Python:
6073

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

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

93106
### Error Handling and extended stack traces
94107

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.
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.
96109

97110
Sample error with traceback (from test/python/error.py):
111+
98112
```
99113
Traceback (most recent call last):
100114
File "test/python/error.py", line 6, in <module>
@@ -103,8 +117,10 @@ Traceback (most recent call last):
103117
print 1/0
104118
ZeroDivisionError: integer division or modulo by zero
105119
```
120+
106121
would result into the following error:
107-
```js
122+
123+
```typescript
108124
{ [Error: ZeroDivisionError: integer division or modulo by zero]
109125
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',
110126
executable: 'python',
@@ -113,7 +129,9 @@ would result into the following error:
113129
args: null,
114130
exitCode: 1 }
115131
```
132+
116133
and `err.stack` would look like this:
134+
117135
```
118136
Error: ZeroDivisionError: integer division or modulo by zero
119137
at PythonShell.parseError (python-shell/index.js:131:17)
@@ -141,6 +159,7 @@ Creates an instance of `PythonShell` and starts the Python process
141159
* `binary`: data is streamed as-is through `stdout` and `stdin`
142160
* `formatter`: each message to send is transformed using this method, then appended with "\n"
143161
* `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
144163
* `encoding`: the text encoding to apply on the child process streams (default: "utf8")
145164
* `pythonPath`: The path where to locate the "python" executable. Default: "python"
146165
* `pythonOptions`: Array of option switches to pass to "python"
@@ -154,23 +173,25 @@ PythonShell instances have the following properties:
154173
* `command`: the full command arguments passed to the Python executable
155174
* `stdin`: the Python stdin stream, used to send data to the child process
156175
* `stdout`: the Python stdout stream, used for receiving data from the child process
157-
* `stderr`: the Python stderr stream, used for communicating errors
176+
* `stderr`: the Python stderr stream, used for communicating logs & errors
158177
* `childProcess`: the process instance created via `child_process.spawn`
159178
* `terminated`: boolean indicating whether the process has exited
160179
* `exitCode`: the process exit code, available after the process has ended
161180

162181
Example:
163-
```js
182+
183+
```typescript
164184
// create a new instance
165-
var shell = new PythonShell('script.py', options);
185+
let shell = new PythonShell('script.py', options);
166186
```
167187

168188
#### `#defaultOptions`
169189

170190
Configures default options for all new instances of PythonShell.
171191

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

184205
Example:
185-
```js
206+
207+
```typescript
186208
// run a simple script
187209
PythonShell.run('script.py', function (err, results) {
188210
// script finished
189211
});
190212
```
191213

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+
192229
#### `.send(message)`
193230

194231
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.
195232

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

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

207245
#### `.receive(data)`
208246

209247
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.
210248

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+
211253
#### `.end(callback)`
212254

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

217259
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.
218260

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+
219271
#### event: `message`
220272

221273
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.
222274

223275
Example:
224-
```js
276+
277+
```typescript
225278
// receive a message in text mode
226-
var shell = new PythonShell('script.py', { mode: 'text '});
279+
let shell = new PythonShell('script.py', { mode: 'text '});
227280
shell.on('message', function (message) {
228281
// handle message (a line of text from stdout)
229282
});
230283

231284
// receive a message in JSON mode
232-
var shell = new PythonShell('script.py', { mode: 'json '});
285+
let shell = new PythonShell('script.py', { mode: 'json '});
233286
shell.on('message', function (message) {
234287
// handle message (a line of text from stdout, parsed as JSON)
235288
});
236289
```
237290

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+
238305
#### event: `close`
239306

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

0 commit comments

Comments
 (0)