Browse Source

Closure, Operator

pull/188/head
Jure Šorn 3 months ago
parent
commit
1cf9996413
3 changed files with 15 additions and 15 deletions
  1. 12
      README.md
  2. 16
      index.html
  3. 2
      parse.js

12
README.md

@ -852,7 +852,7 @@ def get_multiplier(a):
### Partial
```python
from functools import partial
<function> = partial(<function> [, <arg_1>, <arg_2>, ...])
<function> = partial(<function> [, <arg_1> [, ...]])
```
```python
@ -2167,13 +2167,13 @@ Operator
```python
import operator as op
<bool> = op.not_(<obj>) # or, and, not (or/and missing)
<bool> = op.eq/ne/lt/le/gt/ge/is_/contains(<obj>, <obj>) # ==, !=, <, <=, >, >=, is, in
<bool> = op.eq/ne/lt/ge/is_/is_not/contains(<obj>, <obj>) # ==, !=, <, >=, is, is not, in
<obj> = op.or_/xor/and_(<int/set>, <int/set>) # |, ^, &
<int> = op.lshift/rshift(<int>, <int>) # <<, >>
<obj> = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>) # +, -, *, /, //, %
<num> = op.neg/invert(<num>) # -, ~
<num> = op.pow(<num>, <num>) # **
<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...]) # [index/key], .name, .name()
<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...]) # [index/key], .name, .name([…])
```
```python
@ -2183,8 +2183,8 @@ sorted_by_both = sorted(<coll.>, key=op.itemgetter(1, 0))
product_of_elems = functools.reduce(op.mul, <collection>)
first_element = op.methodcaller('pop', 0)(<list>)
```
* **Bitwise operators require objects to have or(), xor(), and(), lshift(), rshift() and invert() special methods, unlike logical operators that work on all types of objects.**
* **Also: `'<bool> = <bool> &|^ <bool>'` and `'<int> = <bool> &|^ <int>'`.**
* **Comparisons can be chained: `'x < y < z'` is the same as `'(x < y) and (y < z)`'.**
* **Most operators call the object's special method that is named after them (second object is passed as an argument), while logical operators call their own code that relies on bool().**
Match Statement
@ -2336,7 +2336,7 @@ import asyncio as aio
#### Runs a terminal game where you control an asterisk that must avoid numbers:
```python
import asyncio, collections, curses, curses.textpad, enum, random, time
import asyncio, collections, curses, curses.textpad, enum, random
P = collections.namedtuple('P', 'x y') # Position
D = enum.Enum('D', 'n e s w') # Direction

16
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>July 16, 2024</aside>
<aside>July 24, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -714,7 +714,7 @@ player = Player(point, direction) <span class="hljs-comment">#
<li><strong>Any value that is referenced from within multiple nested functions gets shared.</strong></li>
</ul>
<div><h3 id="partial">Partial</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
&lt;function&gt; = partial(&lt;function&gt; [, &lt;arg_1&gt;, &lt;arg_2&gt;, ...])
&lt;function&gt; = partial(&lt;function&gt; [, &lt;arg_1&gt; [, ...]])
</code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">multiply</span><span class="hljs-params">(a, b)</span>:</span>
@ -1777,13 +1777,13 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</ul>
<div><h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> operator <span class="hljs-keyword">as</span> op
&lt;bool&gt; = op.not_(&lt;obj&gt;) <span class="hljs-comment"># or, and, not (or/and missing)</span>
&lt;bool&gt; = op.eq/ne/lt/le/gt/ge/is_/contains(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># ==, !=, &lt;, &lt;=, &gt;, &gt;=, is, in</span>
&lt;bool&gt; = op.eq/ne/lt/ge/is_/is_not/contains(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># ==, !=, &lt;, &gt;=, is, is not, in</span>
&lt;obj&gt; = op.or_/xor/and_(&lt;int/set&gt;, &lt;int/set&gt;) <span class="hljs-comment"># |, ^, &amp;</span>
&lt;int&gt; = op.lshift/rshift(&lt;int&gt;, &lt;int&gt;) <span class="hljs-comment"># &lt;&lt;, &gt;&gt;</span>
&lt;obj&gt; = op.add/sub/mul/truediv/floordiv/mod(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># +, -, *, /, //, %</span>
&lt;num&gt; = op.neg/invert(&lt;num&gt;) <span class="hljs-comment"># -, ~</span>
&lt;num&gt; = op.pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># **</span>
&lt;func&gt; = op.itemgetter/attrgetter/methodcaller(&lt;obj&gt; [, ...]) <span class="hljs-comment"># [index/key], .name, .name()</span>
&lt;func&gt; = op.itemgetter/attrgetter/methodcaller(&lt;obj&gt; [, ...]) <span class="hljs-comment"># [index/key], .name, .name([…])</span>
</code></pre></div>
@ -1794,8 +1794,8 @@ product_of_elems = functools.reduce(op.mul, &lt;collection&gt;)
first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span class="hljs-number">0</span>)(&lt;list&gt;)
</code></pre>
<ul>
<li><strong>Bitwise operators require objects to have or(), xor(), and(), lshift(), rshift() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
<li><strong>Also: <code class="python hljs"><span class="hljs-string">'&lt;bool&gt; = &lt;bool&gt; &amp;|^ &lt;bool&gt;'</span></code> and <code class="python hljs"><span class="hljs-string">'&lt;int&gt; = &lt;bool&gt; &amp;|^ &lt;int&gt;'</span></code>.</strong></li>
<li><strong>Comparisons can be chained: <code class="python hljs"><span class="hljs-string">'x &lt; y &lt; z'</span></code> is the same as <code class="python hljs"><span class="hljs-string">'(x &lt; y) and (y &lt; z)</span></code>'.</strong></li>
<li><strong>Most operators call the object's special method that is named after them (second object is passed as an argument), while logical operators call their own code that relies on bool().</strong></li>
</ul>
<div><h2 id="matchstatement"><a href="#matchstatement" name="matchstatement">#</a>Match Statement</h2><p><strong>Executes the first block with matching pattern. Added in Python 3.10.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">match</span> &lt;object/expression&gt;:
<span class="hljs-keyword">case</span> &lt;pattern&gt; [<span class="hljs-keyword">if</span> &lt;condition&gt;]:
@ -1913,7 +1913,7 @@ delattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>)
&lt;coro&gt; = aio.wait(&lt;tasks&gt;, …) <span class="hljs-comment"># `aio.ALL/FIRST_COMPLETED`. Returns (done, pending).</span>
&lt;iter&gt; = aio.as_completed(&lt;coros/tasks&gt;) <span class="hljs-comment"># Iter of coros. All return next result when awaited.</span>
</code></pre>
<div><h4 id="runsaterminalgamewhereyoucontrolanasteriskthatmustavoidnumbers">Runs a terminal game where you control an asterisk that must avoid numbers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random, time
<div><h4 id="runsaterminalgamewhereyoucontrolanasteriskthatmustavoidnumbers">Runs a terminal game where you control an asterisk that must avoid numbers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
@ -2932,7 +2932,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer>
<aside>July 16, 2024</aside>
<aside>July 24, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

2
parse.js

@ -122,7 +122,7 @@ const MATCH_EXAMPLE =
'<span class="hljs-string">\'README.md is a readme file that belongs to user gto.\'</span>\n';
const COROUTINES =
'<span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random, time\n' +
'<span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random\n' +
'\n' +
'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
'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' +

Loading…
Cancel
Save