|
389 | 389 | <pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>type(<span class="hljs-string">'a'</span>), <span class="hljs-string">'a'</span>.__class__, str
|
390 | 390 | (<<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">str</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'>)
|
391 | 391 | </span></code></pre>
|
392 |
| -<div><h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have built-in names, so they must be imported:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType |
| 392 | +<div><h4 id="sometypesdonthavebuiltinnamessotheymustbeimported">Some types don't have built-in names, so they must be imported:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType |
393 | 393 | </code></pre></div>
|
394 | 394 |
|
395 | 395 | <div><h3 id="abstractbaseclasses">Abstract Base Classes</h3><p><strong>Each abstract base class specifies a set of virtual subclasses. These classes are then recognized by isinstance() and issubclass() as subclasses of the ABC, although they are really not.</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Sequence, Collection, Iterable
|
|
559 | 559 | | 567.89 | '5.7e+02' | '567.89' | '5.68e+02' | '56789.00%' |
|
560 | 560 | +---------------+-----------------+-----------------+-----------------+-----------------+
|
561 | 561 | </code></pre>
|
562 |
| -<div><h3 id="ints">Ints</h3><pre><code class="python language-python hljs">{<span class="hljs-number">90</span>:c} <span class="hljs-comment"># 'Z'</span> |
| 562 | +<div><h3 id="integers">Integers</h3><pre><code class="python language-python hljs">{<span class="hljs-number">90</span>:c} <span class="hljs-comment"># 'Z'</span> |
563 | 563 | {<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'</span>
|
564 | 564 | {<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'</span>
|
565 | 565 | </code></pre></div>
|
|
846 | 846 | <li><strong>Partial is also useful in cases when function needs to be passed as an argument, because it enables us to set its arguments beforehand.</strong></li>
|
847 | 847 | <li><strong>A few examples being: <code class="python hljs"><span class="hljs-string">'defaultdict(<function>)'</span></code>, <code class="python hljs"><span class="hljs-string">'iter(<function>, to_exclusive)'</span></code> and dataclass's <code class="python hljs"><span class="hljs-string">'field(default_factory=<function>)'</span></code>.</strong></li>
|
848 | 848 | </ul>
|
849 |
| -<div><h3 id="nonlocal">Nonlocal</h3><p><strong>If variable is being assigned to anywhere in the scope, it is regarded as a local variable, unless it is declared as a 'global' or a 'nonlocal'.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_counter</span><span class="hljs-params">()</span>:</span> |
| 849 | +<div><h3 id="nonlocal">Non-Local</h3><p><strong>If variable is being assigned to anywhere in the scope, it is regarded as a local variable, unless it is declared as a 'global' or a 'nonlocal'.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_counter</span><span class="hljs-params">()</span>:</span> |
850 | 850 | i = <span class="hljs-number">0</span>
|
851 | 851 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">()</span>:</span>
|
852 | 852 | <span class="hljs-keyword">nonlocal</span> i
|
|
1394 | 1394 | arguments = sys.argv[<span class="hljs-number">1</span>:]
|
1395 | 1395 | </code></pre></div>
|
1396 | 1396 |
|
1397 |
| -<div><h3 id="argparse">Argparse</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> argparse <span class="hljs-keyword">import</span> ArgumentParser, FileType |
| 1397 | +<div><h3 id="argumentparser">Argument Parser</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> argparse <span class="hljs-keyword">import</span> ArgumentParser, FileType |
1398 | 1398 | p = ArgumentParser(description=<str>)
|
1399 | 1399 | p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span class="hljs-string">'--<name>'</span>, action=<span class="hljs-string">'store_true'</span>) <span class="hljs-comment"># Flag</span>
|
1400 | 1400 | p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span class="hljs-string">'--<name>'</span>, type=<type>) <span class="hljs-comment"># Option</span>
|
|
1876 | 1876 |
|
1877 | 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 | 1878 | <dict> = vars(<object>) <span class="hljs-comment"># Dict of object's fields. Also <obj>.__dict__.</span>
|
1879 |
| -</code></pre></div> |
1880 |
| - |
1881 |
| -<pre><code class="python language-python hljs"><bool> = hasattr(<object>, <span class="hljs-string">'<attr_name>'</span>) |
| 1879 | +<bool> = hasattr(<object>, <span class="hljs-string">'<attr_name>'</span>) |
1882 | 1880 | value = getattr(<object>, <span class="hljs-string">'<attr_name>'</span>)
|
1883 | 1881 | setattr(<object>, <span class="hljs-string">'<attr_name>'</span>, value)
|
1884 | 1882 | delattr(<object>, <span class="hljs-string">'<attr_name>'</span>)
|
1885 |
| -</code></pre> |
| 1883 | +</code></pre></div> |
| 1884 | + |
1886 | 1885 | <div><h3 id="parameters-1">Parameters</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> inspect <span class="hljs-keyword">import</span> signature
|
1887 | 1886 | <sig> = signature(<function>)
|
1888 | 1887 | no_of_params = len(<sig>.parameters)
|
|
1980 | 1979 | <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>
|
1981 | 1980 | state = {<span class="hljs-string">'*'</span>: P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), **{id_: P(<span class="hljs-number">30</span>, <span class="hljs-number">10</span>) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)}}
|
1982 | 1981 | moves = asyncio.Queue()
|
1983 |
| - coros = (*(random_controller(id_, moves) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)), |
| 1982 | + coros = [*[random_controller(id_, moves) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)], |
1984 | 1983 | human_controller(screen, moves),
|
1985 | 1984 | model(moves, state, *screen.getmaxyx()),
|
1986 |
| - view(state, screen)) |
| 1985 | + view(state, screen)] |
1987 | 1986 | <span class="hljs-keyword">await</span> asyncio.wait(coros, return_when=asyncio.FIRST_COMPLETED)
|
1988 | 1987 |
|
1989 | 1988 | <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">random_controller</span><span class="hljs-params">(id_, moves)</span>:</span>
|
|
2493 | 2492 | </code></pre></div></div>
|
2494 | 2493 |
|
2495 | 2494 |
|
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) |
| 2495 | +<div><h3 id="rectangle">Rectangle</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 | 2496 | <int> = <Rect>.x/y/centerx/centery/…
|
2498 | 2497 | <tup.> = <Rect>.topleft/center/…
|
2499 | 2498 | <Rect> = <Rect>.move((x, y))
|
|
2535 | 2534 | <Sound>.play() <span class="hljs-comment"># Starts playing the sound.</span>
|
2536 | 2535 | </code></pre></div>
|
2537 | 2536 |
|
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 |
| 2537 | +<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, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it |
2539 | 2538 | <span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
2540 | 2539 |
|
2541 | 2540 | P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
|
|
0 commit comments