|
1362 | 1362 | <div><h2 id="exit"><a href="#exit" name="exit">#</a>Exit</h2><p><strong>Exits the interpreter by raising SystemExit exception.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
|
1363 | 1363 | sys.exit() <span class="hljs-comment"># Exits with exit code 0 (success).</span>
|
1364 | 1364 | sys.exit(<el>) <span class="hljs-comment"># Prints object to stderr and exits with 1.</span>
|
1365 |
| -sys.exit(<int>) <span class="hljs-comment"># Exits with the passed exit code.</span> |
| 1365 | +sys.exit(<int>) <span class="hljs-comment"># Exits with passed exit code.</span> |
1366 | 1366 | </code></pre></div>
|
1367 | 1367 |
|
1368 | 1368 |
|
|
1378 | 1378 | </code></pre></div>
|
1379 | 1379 |
|
1380 | 1380 | <ul>
|
1381 |
| -<li><strong>Levels deeper than 'depth' get replaced by '…'.</strong></li> |
| 1381 | +<li><strong>Levels deeper than 'depth' get replaced with '…'.</strong></li> |
1382 | 1382 | </ul>
|
1383 |
| -<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><p><strong>Reads a line from user input or pipe if present.</strong></p><pre><code class="python language-python hljs"><str> = input(prompt=<span class="hljs-keyword">None</span>) |
| 1383 | +<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><p><strong>Reads a line from the user input or pipe if present.</strong></p><pre><code class="python language-python hljs"><str> = input(prompt=<span class="hljs-keyword">None</span>) |
1384 | 1384 | </code></pre></div>
|
1385 | 1385 |
|
1386 | 1386 |
|
1387 | 1387 | <ul>
|
1388 | 1388 | <li><strong>Trailing newline gets stripped.</strong></li>
|
1389 | 1389 | <li><strong>Prompt string is printed to the standard output before reading input.</strong></li>
|
1390 |
| -<li><strong>Raises EOFError when user hits EOF (ctrl-d) or input stream gets exhausted.</strong></li> |
| 1390 | +<li><strong>Raises EOFError when user hits EOF (ctrl-d/z) or input stream gets exhausted.</strong></li> |
1391 | 1391 | </ul>
|
1392 | 1392 | <div><h2 id="commandlinearguments"><a href="#commandlinearguments" name="commandlinearguments">#</a>Command Line Arguments</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
|
1393 | 1393 | script_name = sys.argv[<span class="hljs-number">0</span>]
|
|
1867 | 1867 | LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span> : op.or_})
|
1868 | 1868 | last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(<list>)
|
1869 | 1869 | </code></pre>
|
1870 |
| -<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs"><list> = dir() <span class="hljs-comment"># Returns names of local variables (incl. functions).</span> |
1871 |
| -<dict> = vars() <span class="hljs-comment"># Returns dict of local variables. Also locals().</span> |
1872 |
| -<dict> = globals() <span class="hljs-comment"># Returns dict of global variables.</span> |
| 1870 | +<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs"><list> = dir() <span class="hljs-comment"># Names of local variables (incl. functions).</span> |
| 1871 | +<dict> = vars() <span class="hljs-comment"># Dict of local variables. Also locals().</span> |
| 1872 | +<dict> = globals() <span class="hljs-comment"># Dict of global variables.</span> |
1873 | 1873 | </code></pre></div></div>
|
1874 | 1874 |
|
1875 | 1875 |
|
1876 | 1876 |
|
1877 |
| -<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs"><list> = dir(<object>) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span> |
1878 |
| -<dict> = vars(<object>) <span class="hljs-comment"># Returns dict of object's fields. Also <obj>.__dict__.</span> |
| 1877 | +<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs"><list> = dir(<object>) <span class="hljs-comment"># Names of object's attributes (incl. methods).</span> |
| 1878 | +<dict> = vars(<object>) <span class="hljs-comment"># Dict of object's fields. Also <obj>.__dict__.</span> |
1879 | 1879 | </code></pre></div>
|
1880 | 1880 |
|
1881 | 1881 | <pre><code class="python language-python hljs"><bool> = hasattr(<object>, <span class="hljs-string">'<attr_name>'</span>)
|
|
1916 | 1916 | <li><strong>Like in our case, new() can also be called directly, usually from a new() method of a child class (</strong><code class="python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls)</span>:</span> <span class="hljs-keyword">return</span> super().__new__(cls)</code><strong>).</strong></li>
|
1917 | 1917 | <li><strong>The only difference between the examples above is that my_meta_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.</strong></li>
|
1918 | 1918 | </ul>
|
1919 |
| -<div><h3 id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has a 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span> |
| 1919 | +<div><h3 id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span> |
1920 | 1920 | b = <span class="hljs-number">12345</span>
|
1921 | 1921 | </code></pre></div>
|
1922 | 1922 |
|
|
1934 | 1934 | | MyClass --> MyMetaClass |
|
1935 | 1935 | | | v |
|
1936 | 1936 | | object -----> type <+ |
|
1937 |
| -| | ^ +---+ | |
1938 |
| -| str ---------+ | |
| 1937 | +| | ^ +--+ | |
| 1938 | +| str ----------+ | |
1939 | 1939 | +-------------+-------------+
|
1940 | 1940 | </code></pre>
|
1941 | 1941 | <div><h3 id="inheritancediagram">Inheritance Diagram</h3><pre><code class="python language-python hljs">MyClass.__base__ == object <span class="hljs-comment"># MyClass is a subclass of object.</span>
|
|
2067 | 2067 | <span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
|
2068 | 2068 | P = namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>)
|
2069 | 2069 | height, width = screen.getmaxyx()
|
2070 |
| - <span class="hljs-keyword">return</span> P(width - <span class="hljs-number">1</span>, height - <span class="hljs-number">1</span>) |
| 2070 | + <span class="hljs-keyword">return</span> P(width<span class="hljs-number">-1</span>, height<span class="hljs-number">-1</span>) |
2071 | 2071 |
|
2072 | 2072 | <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
2073 | 2073 | main()
|
|
2367 | 2367 | y = sum(range(velocity+<span class="hljs-number">1</span>))
|
2368 | 2368 | frame = Image.new(<span class="hljs-string">'L'</span>, (WIDTH, WIDTH))
|
2369 | 2369 | draw = ImageDraw.Draw(frame)
|
2370 |
| - draw.ellipse((WIDTH/<span class="hljs-number">2</span>-R, y, WIDTH/<span class="hljs-number">2</span>+R, y+<span class="hljs-number">2</span>*R), fill=<span class="hljs-string">'white'</span>) |
| 2370 | + draw.ellipse((WIDTH/<span class="hljs-number">2</span>-R, y, WIDTH/<span class="hljs-number">2</span>+R, y+R*<span class="hljs-number">2</span>), fill=<span class="hljs-string">'white'</span>) |
2371 | 2371 | frames.append(frame)
|
2372 | 2372 | frames += reversed(frames[<span class="hljs-number">1</span>:<span class="hljs-number">-1</span>])
|
2373 | 2373 | imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<span class="hljs-number">0.03</span>)
|
|
2390 | 2390 | <Wave_write>.setnchannels(<int>) <span class="hljs-comment"># 1 for mono, 2 for stereo.</span>
|
2391 | 2391 | <Wave_write>.setsampwidth(<int>) <span class="hljs-comment"># 2 for CD quality sound.</span>
|
2392 | 2392 | <Wave_write>.setparams(<params>) <span class="hljs-comment"># Sets all parameters.</span>
|
2393 |
| -<Wave_write>.writeframes(<bytes>) <span class="hljs-comment"># Appends frames to file.</span> |
| 2393 | +<Wave_write>.writeframes(<bytes>) <span class="hljs-comment"># Appends frames to the file.</span> |
2394 | 2394 | </code></pre>
|
2395 | 2395 | <ul>
|
2396 | 2396 | <li><strong>Bytes object contains a sequence of frames, each consisting of one or more samples.</strong></li>
|
|
2494 | 2494 |
|
2495 | 2495 |
|
2496 | 2496 | <div><h3 id="rect">Rect</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs"><Rect> = pg.Rect(x, y, width, height)
|
2497 |
| -<int> = <Rect>.x/y/centerx/centery |
2498 |
| -<tup.> = <Rect>.topleft/center |
| 2497 | +<int> = <Rect>.x/y/centerx/centery/… |
| 2498 | +<tup.> = <Rect>.topleft/center/… |
2499 | 2499 | <Rect> = <Rect>.move((x, y))
|
2500 | 2500 | </code></pre></div>
|
2501 | 2501 |
|
|
2535 | 2535 | <Sound>.play() <span class="hljs-comment"># Starts playing the sound.</span>
|
2536 | 2536 | </code></pre></div>
|
2537 | 2537 |
|
2538 |
| -<div><h3 id="basicmariobrothersexample">Basic Mario Brothers Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections, dataclasses, enum, io, math, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it |
| 2538 | +<div><h3 id="supermariobrosexample">Super Mario Bros. Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections, dataclasses, enum, io, math, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it |
2539 | 2539 | <span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
2540 | 2540 |
|
2541 | 2541 | P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
|
|
2574 | 2574 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_speed</span><span class="hljs-params">(mario, tiles, pressed)</span>:</span>
|
2575 | 2575 | x, y = mario.spd
|
2576 | 2576 | x += <span class="hljs-number">2</span> * ((D.e <span class="hljs-keyword">in</span> pressed) - (D.w <span class="hljs-keyword">in</span> pressed))
|
2577 |
| - x = math.copysign(abs(x) - <span class="hljs-number">1</span>, x) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> |
| 2577 | + x -= x / abs(x) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> |
2578 | 2578 | y += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles) <span class="hljs-keyword">else</span> (<span class="hljs-number">-10</span> <span class="hljs-keyword">if</span> D.n <span class="hljs-keyword">in</span> pressed <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>)
|
2579 |
| - mario.spd = P(*[max(-thresh, min(thresh, s)) <span class="hljs-keyword">for</span> thresh, s <span class="hljs-keyword">in</span> zip(MAX_SPEED, P(x, y))]) |
| 2579 | + mario.spd = P(*[max(-limit, min(limit, s)) <span class="hljs-keyword">for</span> limit, s <span class="hljs-keyword">in</span> zip(MAX_SPEED, P(x, y))]) |
2580 | 2580 |
|
2581 | 2581 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_position</span><span class="hljs-params">(mario, tiles)</span>:</span>
|
2582 | 2582 | old_p, delta = mario.rect.topleft, P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
|
|
2601 | 2601 | <span class="hljs-keyword">return</span> next(mario.frame_cycle) <span class="hljs-keyword">if</span> {D.w, D.e} & pressed <span class="hljs-keyword">else</span> <span class="hljs-number">6</span>
|
2602 | 2602 | screen.fill((<span class="hljs-number">85</span>, <span class="hljs-number">168</span>, <span class="hljs-number">255</span>))
|
2603 | 2603 | mario.facing_left = (D.w <span class="hljs-keyword">in</span> pressed) <span class="hljs-keyword">if</span> {D.e, D.w} & pressed <span class="hljs-keyword">else</span> mario.facing_left
|
2604 |
| - screen.blit(images[get_frame_index() + mario.facing_left*<span class="hljs-number">9</span>], mario.rect) |
| 2604 | + screen.blit(images[get_frame_index() + mario.facing_left * <span class="hljs-number">9</span>], mario.rect) |
2605 | 2605 | <span class="hljs-keyword">for</span> rect <span class="hljs-keyword">in</span> tiles:
|
2606 | 2606 | screen.blit(images[<span class="hljs-number">19</span> <span class="hljs-keyword">if</span> {*rect.topleft} & {<span class="hljs-number">0</span>, (SIZE<span class="hljs-number">-1</span>)*<span class="hljs-number">16</span>} <span class="hljs-keyword">else</span> <span class="hljs-number">18</span>], rect)
|
2607 | 2607 | pygame.display.flip()
|
|
0 commit comments