Browse Source

Coroutines, Pygame

pull/109/merge
Jure Šorn 1 year ago
parent
commit
edac4bb3f6
5 changed files with 21 additions and 21 deletions
  1. 14
      README.md
  2. 18
      index.html
  3. 6
      parse.js
  4. 2
      pdf/index_for_pdf.html
  5. 2
      pdf/remove_links.py

14
README.md

@ -2154,7 +2154,7 @@ with <lock>: # Enters the block by calling acq
```
* **Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.**
* **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.**
* **An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments and results must be [pickable](#pickle).**
* **An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be [pickable](#pickle).**
Operator
@ -2347,10 +2347,9 @@ async def random_controller(id_, moves):
async def human_controller(screen, moves):
while True:
ch = screen.getch()
key_mappings = {258: D.s, 259: D.n, 260: D.w, 261: D.e}
if ch in key_mappings:
moves.put_nowait(('*', key_mappings[ch]))
if d := key_mappings.get(screen.getch()):
moves.put_nowait(('*', d))
await asyncio.sleep(0.005)
async def model(moves, state):
@ -2368,6 +2367,7 @@ async def view(state, screen):
for id_, p in state.items():
screen.addstr(offset.y + (p.y - state['*'].y + H//2) % H,
offset.x + (p.x - state['*'].x + W//2) % W, str(id_))
screen.refresh()
await asyncio.sleep(0.005)
if __name__ == '__main__':
@ -2987,7 +2987,7 @@ while not pg.event.get(pg.QUIT):
deltas = {pg.K_UP: (0, -20), pg.K_RIGHT: (20, 0), pg.K_DOWN: (0, 20), pg.K_LEFT: (-20, 0)}
for event in pg.event.get(pg.KEYDOWN):
dx, dy = deltas.get(event.key, (0, 0))
rect.move_ip((dx, dy))
rect = rect.move((dx, dy))
screen.fill((0, 0, 0))
pg.draw.rect(screen, (255, 255, 255), rect)
pg.display.flip()
@ -3016,7 +3016,7 @@ while not pg.event.get(pg.QUIT):
<Surf> = pg.Surface((width, height)) # New RGB surface. RGBA if `flags=pg.SRCALPHA`.
<Surf> = pg.image.load(<path/file>) # Loads the image. Format depends on source.
<Surf> = pg.surfarray.make_surface(<np_array>) # Also `<np_arr> = surfarray.pixels3d(<Surf>)`.
<Surf> = <Surf>.subsurface(<Rect>) # Returns a subsurface.
<Surf> = <Surf>.subsurface(<Rect>) # Creates a new surface from the cutout.
```
```python
@ -3161,7 +3161,7 @@ Name: a, dtype: int64
```python
<el> = <Sr>[key/index] # Or: <Sr>.key
<Sr> = <Sr>[keys/indexes] # Or: <Sr>[<keys_slice/slice>]
<Sr> = <Sr>[bools] # Or: <Sr>.i/loc[bools]
<Sr> = <Sr>[bools] # Or: <Sr>.loc/iloc[bools]
```
```python

18
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>June 4, 2023</aside>
<aside>June 9, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1782,7 +1782,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<ul>
<li><strong>Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.</strong></li>
<li><strong>Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.</strong></li>
<li><strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments and results must be <a href="#pickle">pickable</a>.</strong></li>
<li><strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be <a href="#pickle">pickable</a>.</strong></li>
</ul>
<div><h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> operator <span class="hljs-keyword">as</span> op
&lt;obj&gt; = op.add/sub/mul/truediv/floordiv/mod(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># +, -, *, /, //, %</span>
@ -1927,10 +1927,9 @@ W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span>
<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>
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
ch = screen.getch()
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}
<span class="hljs-keyword">if</span> ch <span class="hljs-keyword">in</span> key_mappings:
moves.put_nowait((<span class="hljs-string">'*'</span>, key_mappings[ch]))
<span class="hljs-keyword">if</span> d := key_mappings.get(screen.getch()):
moves.put_nowait((<span class="hljs-string">'*'</span>, d))
<span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)
<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>
@ -1948,6 +1947,7 @@ W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span>
<span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():
screen.addstr(offset.y + (p.y - state[<span class="hljs-string">'*'</span>].y + H//<span class="hljs-number">2</span>) % H,
offset.x + (p.x - state[<span class="hljs-string">'*'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))
screen.refresh()
<span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
@ -2442,7 +2442,7 @@ rect = pg.Rect(<span class="hljs-number">240</span>, <span class="hljs-number">2
deltas = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-20</span>), pg.K_RIGHT: (<span class="hljs-number">20</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">20</span>), pg.K_LEFT: (<span class="hljs-number">-20</span>, <span class="hljs-number">0</span>)}
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN):
dx, dy = deltas.get(event.key, (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
rect.move_ip((dx, dy))
rect = rect.move((dx, dy))
screen.fill((<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
pg.draw.rect(screen, (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>), rect)
pg.display.flip()
@ -2464,7 +2464,7 @@ rect = pg.Rect(<span class="hljs-number">240</span>, <span class="hljs-number">2
&lt;Surf&gt; = pg.Surface((width, height)) <span class="hljs-comment"># New RGB surface. RGBA if `flags=pg.SRCALPHA`.</span>
&lt;Surf&gt; = pg.image.load(&lt;path/file&gt;) <span class="hljs-comment"># Loads the image. Format depends on source.</span>
&lt;Surf&gt; = pg.surfarray.make_surface(&lt;np_array&gt;) <span class="hljs-comment"># Also `&lt;np_arr&gt; = surfarray.pixels3d(&lt;Surf&gt;)`.</span>
&lt;Surf&gt; = &lt;Surf&gt;.subsurface(&lt;Rect&gt;) <span class="hljs-comment"># Returns a subsurface.</span>
&lt;Surf&gt; = &lt;Surf&gt;.subsurface(&lt;Rect&gt;) <span class="hljs-comment"># Creates a new surface from the cutout.</span>
</code></pre></div>
@ -2586,7 +2586,7 @@ Name: a, dtype: int64
</code></pre>
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;Sr&gt;[key/index] <span class="hljs-comment"># Or: &lt;Sr&gt;.key</span>
&lt;Sr&gt; = &lt;Sr&gt;[keys/indexes] <span class="hljs-comment"># Or: &lt;Sr&gt;[&lt;keys_slice/slice&gt;]</span>
&lt;Sr&gt; = &lt;Sr&gt;[bools] <span class="hljs-comment"># Or: &lt;Sr&gt;.i/loc[bools]</span>
&lt;Sr&gt; = &lt;Sr&gt;[bools] <span class="hljs-comment"># Or: &lt;Sr&gt;.loc/iloc[bools]</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;Sr&gt; = &lt;Sr&gt; &gt;&lt;== &lt;el/Sr&gt; <span class="hljs-comment"># Returns a Series of bools.</span>
&lt;Sr&gt; = &lt;Sr&gt; +-*/ &lt;el/Sr&gt; <span class="hljs-comment"># Items with non-matching keys get value NaN.</span>
@ -2934,7 +2934,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>June 4, 2023</aside>
<aside>June 9, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

6
parse.js

@ -137,10 +137,9 @@ const COROUTINES =
'\n' +
'<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' +
' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
' ch = screen.getch()\n' +
' 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' +
' <span class="hljs-keyword">if</span> ch <span class="hljs-keyword">in</span> key_mappings:\n' +
' moves.put_nowait((<span class="hljs-string">\'*\'</span>, key_mappings[ch]))\n' +
' <span class="hljs-keyword">if</span> d := key_mappings.get(screen.getch()):\n' +
' moves.put_nowait((<span class="hljs-string">\'*\'</span>, d))\n' +
' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
'\n' +
'<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' +
@ -158,6 +157,7 @@ const COROUTINES =
' <span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():\n' +
' screen.addstr(offset.y + (p.y - state[<span class="hljs-string">\'*\'</span>].y + H//<span class="hljs-number">2</span>) % H,\n' +
' offset.x + (p.x - state[<span class="hljs-string">\'*\'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))\n' +
' screen.refresh()\n' +
' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
'\n' +
'<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">\'__main__\'</span>:\n' +

2
pdf/index_for_pdf.html

@ -133,7 +133,7 @@
<strong>super function, <a href="#inheritance">14</a></strong><br>
<strong>sys module, <a href="#lrucache">13</a>, <a href="#exit">21</a>-<a href="#commandlinearguments">22</a></strong> </p>
<h3 id="t">T</h3>
<p><strong>table, <a href="#csv">26</a>, <a href="#example">27</a>, <a href="#table">34</a>, <a href="#numpy">37</a>-<a href="#indexing">38</a>, <a href="#dataframe">45</a>-<a href="#dataframeaggregatetransformmap">46</a></strong><br>
<p><strong>table, <a href="#csv">26</a>, <a href="#example-1">27</a>, <a href="#table">34</a>, <a href="#numpy">37</a>-<a href="#indexing">38</a>, <a href="#dataframe">45</a>-<a href="#dataframeaggregatetransformmap">46</a></strong><br>
<strong>template, <a href="#format">6</a>, <a href="#dynamicrequest">36</a></strong><br>
<strong>threading module, <a href="#threading">30</a></strong><br>
<strong>time module, <a href="#progressbar">34</a>, <a href="#stopwatch">36</a></strong><br>

2
pdf/remove_links.py

@ -22,7 +22,7 @@ MATCHES = {
'<strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong>': '<strong>To print the spreadsheet to the console use Tabulate library (p. 34).</strong>',
'<strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#dataframeplotencodedecode">Pandas</a> library.</strong>': '<strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library (p. 46).</strong>',
'<strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong>': '<strong>Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).</strong>',
'<strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be <a href="#pickle">pickable</a>.</strong>': '<strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be pickable (p. 25).</strong>',
'<strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be <a href="#pickle">pickable</a>.</strong>': '<strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be pickable.</strong>',
'<strong>Asyncio module also provides its own <a href="#queue">Queue</a>, <a href="#semaphoreeventbarrier">Event</a>, <a href="#lock">Lock</a> and <a href="#semaphoreeventbarrier">Semaphore</a> classes.</strong>': '<strong>Asyncio module also provides its own Queue, Event, Lock and Semaphore classes (p. 30).</strong>',
'<strong>A WSGI server like <a href="https://flask.palletsprojects.com/en/latest/deploying/waitress/">Waitress</a> and a HTTP server such as <a href="https://flask.palletsprojects.com/en/latest/deploying/nginx/">Nginx</a> are needed to run globally.</strong>': '<strong>A WSGI server like Waitress and a HTTP server such as Nginx are needed to run globally.</strong>',
'<strong>The "latest and greatest" profiler that can also monitor GPU usage is called <a href="https://github.com/plasma-umass/scalene">Scalene</a>.</strong>': '<strong>The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.</strong>',

Loading…
Cancel
Save