Skip to content

Commit d1f80ab

Browse files
committed
Coroutine example highlights
1 parent dccfea4 commit d1f80ab

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>April 12, 2024</aside>
57+
<aside>April 13, 2024</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -1959,7 +1959,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
19591959
offset = P(curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)
19601960
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
19611961
screen.erase()
1962-
curses.textpad.rectangle(screen, offset.y<span class="hljs-number">-1</span>, offset.x<span class="hljs-number">-1</span>, offset.y+H, offset.x+W)
1962+
curses.textpad.rectangle(screen, offset.y-<span class="hljs-number">1</span>, offset.x-<span class="hljs-number">1</span>, offset.y+H, offset.x+W)
19631963
<span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():
19641964
screen.addstr(offset.y + (p.y - state[<span class="hljs-string">'*'</span>].y + H//<span class="hljs-number">2</span>) % H,
19651965
offset.x + (p.x - state[<span class="hljs-string">'*'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))
@@ -2935,7 +2935,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29352935

29362936

29372937
<footer>
2938-
<aside>April 12, 2024</aside>
2938+
<aside>April 13, 2024</aside>
29392939
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29402940
</footer>
29412941

parse.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ const MATCH_EXAMPLE =
124124
const COROUTINES =
125125
'<span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random, time\n' +
126126
'\n' +
127-
'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
128-
'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
129-
'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
127+
'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
128+
'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
129+
'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
130130
'\n' +
131131
'<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>\n' +
132-
' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
133-
' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
134-
' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
132+
' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
133+
' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
134+
' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
135135
'\n' +
136136
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main_coroutine</span><span class="hljs-params">(screen)</span>:</span>\n' +
137137
' moves = asyncio.Queue()\n' +
@@ -150,37 +150,29 @@ const COROUTINES =
150150
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">human_controller</span><span class="hljs-params">(screen, moves)</span>:</span>\n' +
151151
' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
152152
' key_mappings = {<span class="hljs-number">258</span>: D.s, <span class="hljs-number">259</span>: D.n, <span class="hljs-number">260</span>: D.w, <span class="hljs-number">261</span>: D.e}\n' +
153-
' ch = screen.getch()\n' +
154-
' <span class="hljs-keyword">if</span> d := key_mappings.get(ch):\n' +
153+
' <span class="hljs-keyword">if</span> d := key_mappings.get(screen.getch()):\n' +
155154
' moves.put_nowait((<span class="hljs-string">\'*\'</span>, d))\n' +
156155
' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
157156
'\n' +
158157
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span><span class="hljs-params">(moves, state)</span>:</span>\n' +
159158
' <span class="hljs-keyword">while</span> state[<span class="hljs-string">\'*\'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (state[id_] <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)):\n' +
160159
' id_, d = <span class="hljs-keyword">await</span> moves.get()\n' +
161-
' x, y = state[id_]\n' +
162160
' deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}\n' +
163-
' dx, dy = deltas[d]\n' +
164-
' state[id_] = P((x + dx) % W, (y + dy) % H)\n' +
161+
' state[id_] = P((state[id_].x + deltas[d].x) % W, (state[id_].y + deltas[d].y) % H)\n' +
165162
'\n' +
166163
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view</span><span class="hljs-params">(state, screen)</span>:</span>\n' +
167164
' offset = P(curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)\n' +
168165
' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
169166
' screen.erase()\n' +
170167
' curses.textpad.rectangle(screen, offset.y-<span class="hljs-number">1</span>, offset.x-<span class="hljs-number">1</span>, offset.y+H, offset.x+W)\n' +
171168
' <span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():\n' +
172-
' screen.addstr(\n' +
173-
' offset.y + (p.y - state[<span class="hljs-string">\'*\'</span>].y + H//<span class="hljs-number">2</span>) % H,\n' +
174-
' offset.x + (p.x - state[<span class="hljs-string">\'*\'</span>].x + W//<span class="hljs-number">2</span>) % W,\n' +
175-
' str(id_)\n' +
176-
' )\n' +
169+
' screen.addstr(offset.y + (p.y - state[<span class="hljs-string">\'*\'</span>].y + H//<span class="hljs-number">2</span>) % H,\n' +
170+
' offset.x + (p.x - state[<span class="hljs-string">\'*\'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))\n' +
177171
' screen.refresh()\n' +
178172
' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
179173
'\n' +
180174
'<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">\'__main__\'</span>:\n' +
181-
' start_time = time.perf_counter()\n' +
182-
' curses.wrapper(main)\n' +
183-
' print(<span class="hljs-string">f\'You survived <span class="hljs-subst">{time.perf_counter() - start_time:<span class="hljs-number">.2</span>f}</span> seconds.\'</span>)\n';
175+
' curses.wrapper(main)\n';
184176

185177
const CURSES =
186178
'<span class="hljs-comment"># $ pip3 install windows-curses</span>\n' +
@@ -829,7 +821,7 @@ function fixHighlights() {
829821
$(`code:contains(\'<n>s\')`).html(STRUCT_FORMAT);
830822
$(`code:contains(match <object/expression>:)`).html(MATCH);
831823
$(`code:contains(>>> match Path)`).html(MATCH_EXAMPLE);
832-
//$(`code:contains(import asyncio, collections, curses, curses.textpad, enum, random)`).html(COROUTINES);
824+
$(`code:contains(import asyncio, collections, curses, curses.textpad, enum, random)`).html(COROUTINES);
833825
$(`code:contains(import curses, os)`).html(CURSES);
834826
$(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
835827
$(`code:contains(>>> logging.basicConfig()`).html(LOGGING_EXAMPLE);

0 commit comments

Comments
 (0)