Skip to content

Commit 799f0b1

Browse files
committed
updated docs
1 parent f9f4698 commit 799f0b1

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ A simple way to run Python scripts from Node.js with basic but efficient inter-p
55
## Features
66

77
+ Reliably spawn Python scripts in a child process
8-
+ Text, JSON and binary modes
8+
+ Built-in text, JSON and binary modes
9+
+ Custom parsers and formatters
910
+ Simple and efficient data transfers through stdin and stdout streams
1011
+ Extended stack traces when an error is thrown
1112

@@ -78,6 +79,12 @@ pyshell.end(function (err) {
7879

7980
Use `.send(message)` to send a message to the Python script. Attach the `message` event to listen to messages emitted from the Python script.
8081

82+
Use `options.mode` to quickly setup how data is sent and received between your Node and Python applications.
83+
84+
* use `text` mode for exchanging lines of text
85+
* use `json` mode for exchanging JSON fragments
86+
* use `binary` mode for anything else (data is sent and received as-is)
87+
8188
For more details and examples including Python source code, take a look at the tests.
8289

8390
### Error Handling and extended stack traces
@@ -129,7 +136,9 @@ Creates an instance of `PythonShell` and starts the Python process
129136
* `text`: each line of data (ending with "\n") is emitted as a message (default)
130137
* `json`: each line of data (ending with "\n") is parsed as JSON and emitted as a message
131138
* `binary`: data is streamed as-is through `stdout` and `stdin`
132-
* `parser`: each line of data (ending with "\n") is parsed with this customer parser (overrides `mode`)
139+
* `formatter`: each message to send is transformed using this method, then appended with "\n"
140+
* `parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
141+
* `encoding`: the text encoding to apply on the child process streams (default: "utf8")
133142
* `pythonPath`: The path where to locate the "python" executable. Default: "python"
134143
* `pythonOptions`: Array of option switches to pass to "python"
135144
* `scriptPath`: The default path where to look for scripts. Default: "./python"
@@ -179,9 +188,7 @@ PythonShell.run('script.py', function (err, results) {
179188

180189
#### `.send(message)`
181190

182-
Sends a message to the Python script via stdin. The data is formatted according to the selected mode (text or JSON). This method can be overridden in order to format the data in some other way.
183-
184-
This method should not be used in binary mode.
191+
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.
185192

186193
Example:
187194
```js
@@ -196,15 +203,15 @@ shell.send({ command: "do_stuff", args: [1, 2, 3] });
196203

197204
#### `.receive(data)`
198205

199-
Parses incoming data from the Python script written via stdout and emits `message` events. The data is parsed as JSON if mode has been set to "json". This method is called automatically as data is being received from stdout and can be overridden to parse the data differently.
206+
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.
200207

201208
#### `.end(callback)`
202209

203210
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
204211

205212
#### event: `message`
206213

207-
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. This event is not emitted in binary mode.
214+
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.
208215

209216
Example:
210217
```js

0 commit comments

Comments
 (0)