From edac4bb3f631af09f4ac54c838985ef3b198cab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 9 Jun 2023 09:36:58 +0200 Subject: [PATCH] Coroutines, Pygame --- README.md | 14 +++++++------- index.html | 18 +++++++++--------- parse.js | 6 +++--- pdf/index_for_pdf.html | 2 +- pdf/remove_links.py | 2 +- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 30596ec..3a91684 100644 --- a/README.md +++ b/README.md @@ -2154,7 +2154,7 @@ with : # 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): = pg.Surface((width, height)) # New RGB surface. RGBA if `flags=pg.SRCALPHA`. = pg.image.load() # Loads the image. Format depends on source. = pg.surfarray.make_surface() # Also ` = surfarray.pixels3d()`. - = .subsurface() # Returns a subsurface. + = .subsurface() # Creates a new surface from the cutout. ``` ```python @@ -3161,7 +3161,7 @@ Name: a, dtype: int64 ```python = [key/index] # Or: .key = [keys/indexes] # Or: [] - = [bools] # Or: .i/loc[bools] + = [bools] # Or: .loc/iloc[bools] ``` ```python diff --git a/index.html b/index.html index bee409e..8c76e65 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -1782,7 +1782,7 @@ CompletedProcess(args=['bc', pickable. +
  • An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be pickable.
  • #Operator

    Module of functions that provide the functionality of operators.

    import operator as op
     <obj>     = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>)     # +, -, *, /, //, %
    @@ -1927,10 +1927,9 @@ W, H = 15, 7
     
     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):
    @@ -1948,6 +1947,7 @@ W, H = 15, 7
             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__':
    @@ -2442,7 +2442,7 @@ rect = pg.Rect(240, 2
         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()
    @@ -2464,7 +2464,7 @@ rect = pg.Rect(240, 2
     <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.
     
    @@ -2586,7 +2586,7 @@ Name: a, dtype: int64
    <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]
     
    <Sr> = <Sr> ><== <el/Sr>                       # Returns a Series of bools.
     <Sr> = <Sr> +-*/ <el/Sr>                       # Items with non-matching keys get value NaN.
    @@ -2934,7 +2934,7 @@ $ pyinstaller script.py --add-data '<path>:.'  
      
     
       
     
    diff --git a/parse.js b/parse.js
    index c958588..93d9630 100755
    --- a/parse.js
    +++ b/parse.js
    @@ -137,10 +137,9 @@ const COROUTINES =
       '\n' +
       'async def human_controller(screen, moves):\n' +
       '    while True:\n' +
    -  '        ch = screen.getch()\n' +
       '        key_mappings = {258: D.s, 259: D.n, 260: D.w, 261: D.e}\n' +
    -  '        if ch in key_mappings:\n' +
    -  '            moves.put_nowait((\'*\', key_mappings[ch]))\n' +
    +  '        if d := key_mappings.get(screen.getch()):\n' +
    +  '            moves.put_nowait((\'*\', d))\n' +
       '        await asyncio.sleep(0.005)\n' +
       '\n' +
       'async def model(moves, state):\n' +
    @@ -158,6 +157,7 @@ const COROUTINES =
       '        for id_, p in state.items():\n' +
       '            screen.addstr(offset.y + (p.y - state[\'*\'].y + H//2) % H,\n' +
       '                          offset.x + (p.x - state[\'*\'].x + W//2) % W, str(id_))\n' +
    +  '        screen.refresh()\n' +
       '        await asyncio.sleep(0.005)\n' +
       '\n' +
       'if __name__ == \'__main__\':\n' +
    diff --git a/pdf/index_for_pdf.html b/pdf/index_for_pdf.html
    index 9b80128..70fbdb6 100644
    --- a/pdf/index_for_pdf.html
    +++ b/pdf/index_for_pdf.html
    @@ -133,7 +133,7 @@
     super function, 14
    sys module, 13, 21-22

    T

    -

    table, 26, 27, 34, 37-38, 45-46
    +

    table, 26, 27, 34, 37-38, 45-46
    template, 6, 36
    threading module, 30
    time module, 34, 36
    diff --git a/pdf/remove_links.py b/pdf/remove_links.py index ba44974..3134eb3 100755 --- a/pdf/remove_links.py +++ b/pdf/remove_links.py @@ -22,7 +22,7 @@ MATCHES = { 'To print the spreadsheet to the console use Tabulate library.': 'To print the spreadsheet to the console use Tabulate library (p. 34).', 'For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library.': 'For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library (p. 46).', 'Bools will be stored and returned as ints and dates as ISO formatted strings.': 'Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).', - 'An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be pickable.': '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).', + 'An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be pickable.': 'An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be pickable.', 'Asyncio module also provides its own Queue, Event, Lock and Semaphore classes.': 'Asyncio module also provides its own Queue, Event, Lock and Semaphore classes (p. 30).', 'A WSGI server like Waitress and a HTTP server such as Nginx are needed to run globally.': 'A WSGI server like Waitress and a HTTP server such as Nginx are needed to run globally.', 'The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.': 'The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.',