Skip to content

Commit 46f99ca

Browse files
committed
Made README easier to read raw
1 parent 76e7abf commit 46f99ca

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

README.md

+51-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ A Python client for [Scratch](scratch.mit.edu)
66

77
1. Start up Scratch
88

9-
2. [Enable remote sensor connections](http://wiki.scratch.mit.edu/wiki/Remote_Sensor_Connections#Enabling), or "Host Mesh" in [BYOB](http://byob.berkeley.edu/)
9+
2. [Enable remote sensor connections](http://wiki.scratch.mit.edu/wiki/Remote_Sensor_Connections#Enabling),
10+
or "Host Mesh" in [BYOB](http://byob.berkeley.edu/)
1011

1112
3. Create a variable for all sprites named `foo`
1213

@@ -30,7 +31,8 @@ Or
3031
# easy_install scratchpy
3132
```
3233

33-
If you're installing from source, you can untar the source tarball, `cd` into the project directory and run
34+
If you're installing from source, you can untar the source tarball, `cd` into
35+
the project directory and run
3436

3537
```
3638
# make install
@@ -46,7 +48,9 @@ import scratch
4648
s = scratch.Scratch()
4749
```
4850

49-
This will create a connection on `localhost` port 42001 and set `s.connected` to `True`. If you want to change the host or port, you can provide them to the constructor
51+
This will create a connection on `localhost` port 42001 and set `s.connected`
52+
to `True`. If you want to change the host or port, you can provide them to the
53+
constructor
5054

5155
```
5256
s = scratch.Scratch(host='0.0.0.0', port=40000)
@@ -59,7 +63,8 @@ s.connect()
5963
```
6064

6165
### Broadcasting
62-
Broadcasting messages to Scratch will function like a broadcast block in Scratch. You can broadcast either a single message
66+
Broadcasting messages to Scratch will function like a broadcast block in
67+
Scratch. You can broadcast either a single message
6368

6469
```
6570
s.broadcast('Hello, Scratch!')
@@ -71,10 +76,13 @@ Or a list of messages
7176
s.broadcast(['Hello, Scratch!', 'How are you doing?'])
7277
```
7378

74-
Actually, you can give `broadcast` any iterable object (list, tuple, set, generator, etc.).
79+
Actually, you can give `broadcast` any iterable object (list, tuple, set,
80+
generator, etc.).
7581

7682
### Sensor updates
77-
Sending sensor updates to Scratch will create new sensors in the Sensing category, or update sensors with new values. The `sensorupdate` method accepts a dict whose keys are sensor names, and values are sensor values.
83+
Sending sensor updates to Scratch will create new sensors in the Sensing
84+
category, or update sensors with new values. The `sensorupdate` method accepts
85+
a dict whose keys are sensor names, and values are sensor values.
7886

7987
```
8088
s.sensorupdate({'temperature' : 75})
@@ -87,28 +95,43 @@ Use the `receive` method to receive messages from Scratch
8795
msg = s.receive()
8896
```
8997

90-
A call to `receive` will block until it reads a message from Scratch. If the call is successful, it returns a tuple of the message received. If the call failed, it raises an exception. If the message received is not a proper Scratch message, `receive` returns `None`.
98+
A call to `receive` will block until it reads a message from Scratch. If the
99+
call is successful, it returns a tuple of the message received. If the call
100+
failed, it raises an exception. If the message received is not a proper Scratch
101+
message, `receive` returns `None`.
91102

92-
The first element of the tuple will be the message type and the second element will be the message data.
103+
The first element of the tuple will be the message type and the second element
104+
will be the message data.
93105

94-
Two types of messages can be received: broadcast messages and sensor update messages.
106+
Two types of messages can be received: broadcast messages and sensor update
107+
messages.
95108

96109
#### Broadcasts
97-
Broadcast messages are received anytime a broadcast block is executed in Scratch. The message data is a string of the message that was broadcast. An example broadcast message returned from `receive` looks like this:
110+
Broadcast messages are received anytime a broadcast block is executed in
111+
Scratch. The message data is a string of the message that was broadcast. An
112+
example broadcast message returned from `receive` looks like this:
98113

99114
```
100115
('broadcast', 'Hello, Python!')
101116
```
102117

103118
#### Sensor updates
104-
Sensor updates are received when global variables (i.e. variables created for all sprites) are created, or when their value changes. The message data is a dict that maps global variable names to their values. Suppose you created two variables, `foo` and `bar`. Upon their creation, `receive` would return a message that looks like this:
119+
Sensor updates are received when global variables (i.e. variables created for
120+
all sprites) are created, or when their value changes. The message data is a
121+
dict that maps global variable names to their values. Suppose you created two
122+
variables, `foo` and `bar`. Upon their creation, `receive` would return a
123+
message that looks like this:
105124

106125
```
107126
('sensor-update', {'foo': 0, 'bar': 0})
108127
```
109128

110129
#### Handling Multiple Messages
111-
`receive` returns only one message. If Scratch has sent more than one message, they will stay on the network buffer until `receive` is called again. To receive all the messages from Scratch you must repeatedly call `receive`. A nice way to handle this is to have a generator function that yields a message everytime it receives, and exits on error.
130+
`receive` returns only one message. If Scratch has sent more than one message,
131+
they will stay on the network buffer until `receive` is called again. To
132+
receive all the messages from Scratch you must repeatedly call `receive`. A
133+
nice way to handle this is to have a generator function that yields a message
134+
everytime it receives, and exits on error.
112135

113136
```
114137
def listen():
@@ -129,7 +152,8 @@ for msg in listen():
129152
# code to handle sensor updates
130153
```
131154

132-
If an error occurs or the connection to Scratch is closed, Python simply exits the loop.
155+
If an error occurs or the connection to Scratch is closed, Python simply exits
156+
the loop.
133157

134158
### Disconnecting
135159
To close a connection to Scratch
@@ -138,16 +162,25 @@ To close a connection to Scratch
138162
s.disconnect()
139163
```
140164

141-
A disconnection may occur without an explicit call to `disconnect`. These usually happen when either Scratch is closed, remote sensor connections are disabled, or when there is something up with the network.
165+
A disconnection may occur without an explicit call to `disconnect`. These
166+
usually happen when either Scratch is closed, remote sensor connections are
167+
disabled, or when there is something up with the network.
142168

143169
### Errors
144-
There are two kinds of errors that can be caught in `connect`, `receive`, `broadcast`, and `sensorupdate`:
170+
There are two kinds of errors that can be caught in `connect`, `receive`,
171+
`broadcast`, and `sensorupdate`:
145172

146-
`ScratchError` is raised when there are errors with the network (e.g. connection refused, connection established, etc. basically any error from [errno](http://docs.python.org/2/library/errno.html)). The error message returned is the error message from `errno`.
173+
`ScratchError` is raised when there are errors with the network (e.g.
174+
connection refused, connection established, etc. basically any error from
175+
[errno](http://docs.python.org/2/library/errno.html)). The error message
176+
returned is the error message from `errno`.
147177

148-
`ScratchConnectionError` is raised when reading or writing data has no effect (i.e. reading from Scratch returns an empty string, or writing to Scratch writes 0 bytes). This is usually a sign that Scratch has been disconnected.
178+
`ScratchConnectionError` is raised when reading or writing data has no effect
179+
(i.e. reading from Scratch returns an empty string, or writing to Scratch
180+
writes 0 bytes). This is usually a sign that Scratch has been disconnected.
149181

150-
If you do not care about the difference, you can just except `ScratchError` and it will also catch `ScratchConnectionError`.
182+
If you do not care about the difference, you can just except `ScratchError` and
183+
it will also catch `ScratchConnectionError`.
151184

152185
## License
153186
MIT

0 commit comments

Comments
 (0)