Browse Source

Set, Inline - a lot of changes

pull/135/head
Jure Šorn 2 years ago
parent
commit
8b2fac3b0a
2 changed files with 40 additions and 40 deletions
  1. 38
      README.md
  2. 42
      index.html

38
README.md

@ -109,7 +109,7 @@ Counter({'blue': 3, 'red': 2, 'yellow': 1})
Set
---
```python
<set> = set()
<set> = set() # {} returns a dictionary.
```
```python
@ -752,16 +752,16 @@ Inline
------
### Lambda
```python
<func> = lambda: <return_value> # A single statement function.
<func> = lambda <arg_1>, <arg_2>: <return_value> # Also accepts default arguments.
<func> = lambda: <return_value> # A single statement function.
<func> = lambda <arg_1>, <arg_2>: <return_value> # Also accepts default arguments.
```
### Comprehensions
```python
<list> = [i+1 for i in range(10)] # Or: [1, 2, ..., 10]
<iter> = (i for i in range(10) if i > 5) # Or: iter([6, 7, 8, 9])
<set> = {i+5 for i in range(10)} # Or: {5, 6, ..., 14}
<dict> = {i: i*2 for i in range(10)} # Or: {0: 0, 1: 2, ..., 9: 18}
<list> = [i+1 for i in range(10)] # Or: [1, 2, ..., 10]
<iter> = (i for i in range(10) if i > 5) # Or: iter([6, 7, 8, 9])
<set> = {i+5 for i in range(10)} # Or: {5, 6, ..., 14}
<dict> = {i: i*2 for i in range(10)} # Or: {0: 0, 1: 2, ..., 9: 18}
```
```python
@ -771,21 +771,21 @@ Inline
### Map, Filter, Reduce
```python
<iter> = map(lambda x: x + 1, range(10)) # Or: iter([1, 2, ..., 10])
<iter> = filter(lambda x: x > 5, range(10)) # Or: iter([6, 7, 8, 9])
<obj> = reduce(lambda out, x: out + x, range(10)) # Or: 45
<iter> = map(lambda x: x + 1, range(10)) # Or: iter([1, 2, ..., 10])
<iter> = filter(lambda x: x > 5, range(10)) # Or: iter([6, 7, 8, 9])
<obj> = reduce(lambda out, x: out + x, range(10)) # Or: 45
```
* **Reduce must be imported from the functools module.**
### Any, All
```python
<bool> = any(<collection>) # Is `bool(el)` True for any element.
<bool> = all(<collection>) # Is True for all elements or empty.
<bool> = any(<collection>) # Is `bool(el)` True for any element.
<bool> = all(<collection>) # Is True for all elements or empty.
```
### Conditional Expression
```python
<obj> = <exp_if_true> if <condition> else <exp_if_false> # Only one expression gets evaluated.
<obj> = <exp> if <condition> else <exp> # Only one expression gets evaluated.
```
```python
@ -796,20 +796,20 @@ Inline
### Named Tuple, Enum, Dataclass
```python
from collections import namedtuple
Point = namedtuple('Point', 'x y')
point = Point(0, 0)
Point = namedtuple('Point', 'x y') # Tuple's subclass with named elements.
point = Point(0, 0) # Tuple with x and y attributes.
```
```python
from enum import Enum
Direction = Enum('Direction', 'n e s w')
direction = Direction.n
Direction = Enum('Direction', 'n e s w') # Enum with n, e, s and w members.
direction = Direction.n # Member with name and value attributes.
```
```python
from dataclasses import make_dataclass
Creature = make_dataclass('Creature', ['loc', 'dir'])
creature = Creature(point, direction)
Player = make_dataclass('Player', ['loc', 'dir']) # Class with init, repr and eq methods.
player = Player(point, direction) # Object with loc and dir attributes.
```

42
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>June 22, 2022</aside>
<aside>June 23, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -158,7 +158,7 @@ Counter({<span class="hljs-string">'blue'</span>: <span class="hljs-number">3</s
(<span class="hljs-string">'blue'</span>, <span class="hljs-number">3</span>)
</code></pre></div>
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs">&lt;set&gt; = set()
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs">&lt;set&gt; = set() <span class="hljs-comment"># {} returns a dictionary.</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;set&gt;.add(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;set&gt; |= {&lt;el&gt;}</span>
@ -647,50 +647,50 @@ func(*args, **kwargs)
<pre><code class="python language-python hljs">head, *body, tail = &lt;coll.&gt; <span class="hljs-comment"># Head or tail can be omitted.</span>
</code></pre>
<div><h2 id="inline"><a href="#inline" name="inline">#</a>Inline</h2><div><h3 id="lambda">Lambda</h3><pre><code class="python language-python hljs">&lt;func&gt; = <span class="hljs-keyword">lambda</span>: &lt;return_value&gt; <span class="hljs-comment"># A single statement function.</span>
&lt;func&gt; = <span class="hljs-keyword">lambda</span> &lt;arg_1&gt;, &lt;arg_2&gt;: &lt;return_value&gt; <span class="hljs-comment"># Also accepts default arguments.</span>
<div><h2 id="inline"><a href="#inline" name="inline">#</a>Inline</h2><div><h3 id="lambda">Lambda</h3><pre><code class="python language-python hljs">&lt;func&gt; = <span class="hljs-keyword">lambda</span>: &lt;return_value&gt; <span class="hljs-comment"># A single statement function.</span>
&lt;func&gt; = <span class="hljs-keyword">lambda</span> &lt;arg_1&gt;, &lt;arg_2&gt;: &lt;return_value&gt; <span class="hljs-comment"># Also accepts default arguments.</span>
</code></pre></div></div>
<div><h3 id="comprehensions">Comprehensions</h3><pre><code class="python language-python hljs">&lt;list&gt; = [i+<span class="hljs-number">1</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)] <span class="hljs-comment"># Or: [1, 2, ..., 10]</span>
&lt;iter&gt; = (i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>) <span class="hljs-keyword">if</span> i &gt; <span class="hljs-number">5</span>) <span class="hljs-comment"># Or: iter([6, 7, 8, 9])</span>
&lt;set&gt; = {i+<span class="hljs-number">5</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)} <span class="hljs-comment"># Or: {5, 6, ..., 14}</span>
&lt;dict&gt; = {i: i*<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)} <span class="hljs-comment"># Or: {0: 0, 1: 2, ..., 9: 18}</span>
<div><h3 id="comprehensions">Comprehensions</h3><pre><code class="python language-python hljs">&lt;list&gt; = [i+<span class="hljs-number">1</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)] <span class="hljs-comment"># Or: [1, 2, ..., 10]</span>
&lt;iter&gt; = (i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>) <span class="hljs-keyword">if</span> i &gt; <span class="hljs-number">5</span>) <span class="hljs-comment"># Or: iter([6, 7, 8, 9])</span>
&lt;set&gt; = {i+<span class="hljs-number">5</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)} <span class="hljs-comment"># Or: {5, 6, ..., 14}</span>
&lt;dict&gt; = {i: i*<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)} <span class="hljs-comment"># Or: {0: 0, 1: 2, ..., 9: 18}</span>
</code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>[l+r <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-string">'abc'</span> <span class="hljs-keyword">for</span> r <span class="hljs-keyword">in</span> <span class="hljs-string">'abc'</span>]
[<span class="hljs-string">'aa'</span>, <span class="hljs-string">'ab'</span>, <span class="hljs-string">'ac'</span>, ..., <span class="hljs-string">'cc'</span>]
</code></pre>
<div><h3 id="mapfilterreduce">Map, Filter, Reduce</h3><pre><code class="python language-python hljs">&lt;iter&gt; = map(<span class="hljs-keyword">lambda</span> x: x + <span class="hljs-number">1</span>, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: iter([1, 2, ..., 10])</span>
&lt;iter&gt; = filter(<span class="hljs-keyword">lambda</span> x: x &gt; <span class="hljs-number">5</span>, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: iter([6, 7, 8, 9])</span>
&lt;obj&gt; = reduce(<span class="hljs-keyword">lambda</span> out, x: out + x, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: 45</span>
<div><h3 id="mapfilterreduce">Map, Filter, Reduce</h3><pre><code class="python language-python hljs">&lt;iter&gt; = map(<span class="hljs-keyword">lambda</span> x: x + <span class="hljs-number">1</span>, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: iter([1, 2, ..., 10])</span>
&lt;iter&gt; = filter(<span class="hljs-keyword">lambda</span> x: x &gt; <span class="hljs-number">5</span>, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: iter([6, 7, 8, 9])</span>
&lt;obj&gt; = reduce(<span class="hljs-keyword">lambda</span> out, x: out + x, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: 45</span>
</code></pre></div>
<ul>
<li><strong>Reduce must be imported from the functools module.</strong></li>
</ul>
<div><h3 id="anyall">Any, All</h3><pre><code class="python language-python hljs">&lt;bool&gt; = any(&lt;collection&gt;) <span class="hljs-comment"># Is `bool(el)` True for any element.</span>
&lt;bool&gt; = all(&lt;collection&gt;) <span class="hljs-comment"># Is True for all elements or empty.</span>
<div><h3 id="anyall">Any, All</h3><pre><code class="python language-python hljs">&lt;bool&gt; = any(&lt;collection&gt;) <span class="hljs-comment"># Is `bool(el)` True for any element.</span>
&lt;bool&gt; = all(&lt;collection&gt;) <span class="hljs-comment"># Is True for all elements or empty.</span>
</code></pre></div>
<div><h3 id="conditionalexpression">Conditional Expression</h3><pre><code class="python language-python hljs">&lt;obj&gt; = &lt;exp_if_true&gt; <span class="hljs-keyword">if</span> &lt;condition&gt; <span class="hljs-keyword">else</span> &lt;exp_if_false&gt; <span class="hljs-comment"># Only one expression gets evaluated.</span>
<div><h3 id="conditionalexpression">Conditional Expression</h3><pre><code class="python language-python hljs">&lt;obj&gt; = &lt;exp&gt; <span class="hljs-keyword">if</span> &lt;condition&gt; <span class="hljs-keyword">else</span> &lt;exp&gt; <span class="hljs-comment"># Only one expression gets evaluated.</span>
</code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>[a <span class="hljs-keyword">if</span> a <span class="hljs-keyword">else</span> <span class="hljs-string">'zero'</span> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)]
[<span class="hljs-string">'zero'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<div><h3 id="namedtupleenumdataclass">Named Tuple, Enum, Dataclass</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>)
point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Tuple's subclass with named elements.</span>
point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Tuple with x and y attributes.</span>
</code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'n e s w'</span>)
direction = Direction.n
Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Enum with n, e, s and w members.</span>
direction = Direction.n <span class="hljs-comment"># Member with name and value attributes.</span>
</code></pre>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
Creature = make_dataclass(<span class="hljs-string">'Creature'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>])
creature = Creature(point, direction)
Player = make_dataclass(<span class="hljs-string">'Player'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>]) <span class="hljs-comment"># Class with init, repr and eq methods.</span>
player = Player(point, direction) <span class="hljs-comment"># Object with loc and dir attributes.</span>
</code></pre>
<div><h2 id="imports"><a href="#imports" name="imports">#</a>Imports</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> &lt;module&gt; <span class="hljs-comment"># Imports a built-in or '&lt;module&gt;.py'.</span>
<span class="hljs-keyword">import</span> &lt;package&gt; <span class="hljs-comment"># Imports a built-in or '&lt;package&gt;/__init__.py'.</span>
@ -2901,7 +2901,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>June 22, 2022</aside>
<aside>June 23, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save