@@ -43,7 +43,7 @@ the project directory and run
43
43
### Connecting
44
44
Constructing a new ` Scratch ` object will automatically connect to Scratch
45
45
46
- ```
46
+ ``` python
47
47
import scratch
48
48
s = scratch.Scratch()
49
49
```
@@ -52,27 +52,27 @@ This will create a connection on `localhost` port 42001 and set `s.connected`
52
52
to ` True ` . If you want to change the host or port, you can provide them to the
53
53
constructor
54
54
55
- ```
55
+ ``` python
56
56
s = scratch.Scratch(host = ' 0.0.0.0' , port = 40000 )
57
57
```
58
58
59
59
If you are disconnected, you can reconnect using the ` connect ` method
60
60
61
- ```
61
+ ``` python
62
62
s.connect()
63
63
```
64
64
65
65
### Broadcasting
66
66
Broadcasting messages to Scratch will function like a broadcast block in
67
67
Scratch. You can broadcast either a single message
68
68
69
- ```
69
+ ``` python
70
70
s.broadcast(' Hello, Scratch!' )
71
71
```
72
72
73
73
Or a list of messages
74
74
75
- ```
75
+ ``` python
76
76
s.broadcast([' Hello, Scratch!' , ' How are you doing?' ])
77
77
```
78
78
@@ -84,14 +84,14 @@ Sending sensor updates to Scratch will create new sensors in the Sensing
84
84
category, or update sensors with new values. The ` sensorupdate ` method accepts
85
85
a dict whose keys are sensor names, and values are sensor values.
86
86
87
- ```
87
+ ``` python
88
88
s.sensorupdate({' temperature' : 75 })
89
89
```
90
90
91
91
### Receiving
92
92
Use the ` receive ` method to receive messages from Scratch
93
93
94
- ```
94
+ ``` python
95
95
msg = s.receive()
96
96
```
97
97
@@ -111,7 +111,7 @@ Broadcast messages are received anytime a broadcast block is executed in
111
111
Scratch. The message data is a string of the message that was broadcast. An
112
112
example broadcast message returned from ` receive ` looks like this:
113
113
114
- ```
114
+ ``` python
115
115
(' broadcast' , ' Hello, Python!' )
116
116
```
117
117
@@ -122,7 +122,7 @@ dict that maps global variable names to their values. Suppose you created two
122
122
variables, ` foo ` and ` bar ` . Upon their creation, ` receive ` would return a
123
123
message that looks like this:
124
124
125
- ```
125
+ ``` python
126
126
(' sensor-update' , {' foo' : 0 , ' bar' : 0 })
127
127
```
128
128
@@ -133,23 +133,23 @@ receive all the messages from Scratch you must repeatedly call `receive`. A
133
133
nice way to handle this is to have a generator function that yields a message
134
134
everytime it receives, and exits on error.
135
135
136
- ```
136
+ ``` python
137
137
def listen ():
138
138
while True :
139
- try:
140
- yield s.receive()
141
- except scratch.ScratchError:
142
- raise StopIteration
139
+ try :
140
+ yield s.receive()
141
+ except scratch.ScratchError:
142
+ raise StopIteration
143
143
```
144
144
145
145
Now you can iterate over all the messages from Scratch
146
146
147
- ```
147
+ ``` python
148
148
for msg in listen():
149
149
if msg[0 ] == ' broadcast' :
150
- # code to handle broadcasts
150
+ # code to handle broadcasts
151
151
elif msg[0 ] == ' sensor-update' :
152
- # code to handle sensor updates
152
+ # code to handle sensor updates
153
153
```
154
154
155
155
If an error occurs or the connection to Scratch is closed, Python simply exits
@@ -158,7 +158,7 @@ the loop.
158
158
### Disconnecting
159
159
To close a connection to Scratch
160
160
161
- ```
161
+ ``` python
162
162
s.disconnect()
163
163
```
164
164
0 commit comments