Skip to content

Commit

Permalink
Coroutines, Pandas
Browse files Browse the repository at this point in the history
  • Loading branch information
gto76 committed Mar 1, 2022
1 parent 14233c4 commit 13d9744
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ class MyHashable:
```

### Sortable
* **With total_ordering decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.**
* **With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.**
* **Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.**
* **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.**

Expand Down Expand Up @@ -1525,11 +1525,11 @@ arguments = sys.argv[1:]
```python
from argparse import ArgumentParser, FileType
p = ArgumentParser(description=<str>)
p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag
p.add_argument('-<short_name>', '--<name>', type=<type>) # Option
p.add_argument('<name>', type=<type>, nargs=1) # First argument
p.add_argument('<name>', type=<type>, nargs='+') # Remaining arguments
p.add_argument('<name>', type=<type>, nargs='*') # Optional arguments
p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag.
p.add_argument('-<short_name>', '--<name>', type=<type>) # Option.
p.add_argument('<name>', type=<type>, nargs=1) # First argument.
p.add_argument('<name>', type=<type>, nargs='+') # Remaining arguments.
p.add_argument('<name>', type=<type>, nargs='*') # Optional arguments.
args = p.parse_args() # Exits on error.
value = args.<name>
```
Expand Down Expand Up @@ -2334,7 +2334,7 @@ async def human_controller(screen, moves):
await asyncio.sleep(0.005)

async def model(moves, state):
while state['*'] not in {p for id_, p in state.items() if id_ != '*'}:
while state['*'] not in (state[id_] for id_ in range(10)):
id_, d = await moves.get()
x, y = state[id_]
deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)}
Expand Down Expand Up @@ -3292,7 +3292,12 @@ b 3 4
```
* **Use `'<DF>[col_key_1, col_key_2][row_key]'` to get the fifth result's values.**

#### DataFrame — Encode, Decode, Plot:
#### DataFrame — Plot, Encode, Decode:
```python
import matplotlib.pyplot as plt
<DF>.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show()
```

```python
<DF> = pd.read_json/html('<str/path/url>')
<DF> = pd.read_csv/pickle/excel('<path/url>')
Expand All @@ -3307,11 +3312,6 @@ b 3 4
<DF>.to_sql('<table_name>', <connection>)
```

```python
import matplotlib.pyplot as plt
<DF>.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show()
```

### GroupBy
**Object that groups together rows of a dataframe based on the value of the passed column.**

Expand Down
36 changes: 18 additions & 18 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<body>
<header>
<aside>February 27, 2022</aside>
<aside>March 1, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>

Expand Down Expand Up @@ -950,7 +950,7 @@


<div><h3 id="sortable">Sortable</h3><ul>
<li><strong>With total_ordering decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.</strong></li>
<li><strong>With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.</strong></li>
<li><strong>Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.</strong></li>
<li><strong>When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> total_ordering
Expand Down Expand Up @@ -1057,7 +1057,7 @@
<div><h3 id="collection">Collection</h3><ul>
<li><strong>Only required methods are iter() and len().</strong></li>
<li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'&lt;iterable&gt;'</span></code> when it uses <code class="python hljs"><span class="hljs-string">'&lt;collection&gt;'</span></code>.</strong></li>
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only iterable objects that are not collections.</strong></li>
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are not collections while being iterable.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCollection</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
self.a = a
Expand Down Expand Up @@ -1298,11 +1298,11 @@

<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
p = ArgumentParser(description=&lt;str&gt;)
p.add_argument(<span class="hljs-string">'-&lt;short_name&gt;'</span>, <span class="hljs-string">'--&lt;name&gt;'</span>, action=<span class="hljs-string">'store_true'</span>) <span class="hljs-comment"># Flag</span>
p.add_argument(<span class="hljs-string">'-&lt;short_name&gt;'</span>, <span class="hljs-string">'--&lt;name&gt;'</span>, type=&lt;type&gt;) <span class="hljs-comment"># Option</span>
p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt;, nargs=<span class="hljs-number">1</span>) <span class="hljs-comment"># First argument</span>
p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt;, nargs=<span class="hljs-string">'+'</span>) <span class="hljs-comment"># Remaining arguments</span>
p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt;, nargs=<span class="hljs-string">'*'</span>) <span class="hljs-comment"># Optional arguments</span>
p.add_argument(<span class="hljs-string">'-&lt;short_name&gt;'</span>, <span class="hljs-string">'--&lt;name&gt;'</span>, action=<span class="hljs-string">'store_true'</span>) <span class="hljs-comment"># Flag.</span>
p.add_argument(<span class="hljs-string">'-&lt;short_name&gt;'</span>, <span class="hljs-string">'--&lt;name&gt;'</span>, type=&lt;type&gt;) <span class="hljs-comment"># Option.</span>
p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt;, nargs=<span class="hljs-number">1</span>) <span class="hljs-comment"># First argument.</span>
p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt;, nargs=<span class="hljs-string">'+'</span>) <span class="hljs-comment"># Remaining arguments.</span>
p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt;, nargs=<span class="hljs-string">'*'</span>) <span class="hljs-comment"># Optional arguments.</span>
args = p.parse_args() <span class="hljs-comment"># Exits on error.</span>
value = args.&lt;name&gt;
</code></pre></div>
Expand All @@ -1319,7 +1319,7 @@
<ul>
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means that the default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on either '\n', '\r' or '\r\n'.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on '\n', '\r' and '\r\n'.</strong></li>
</ul>
<div><h3 id="modes">Modes</h3><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read (default).</strong></li>
Expand All @@ -1330,7 +1330,7 @@
<li><strong><code class="python hljs"><span class="hljs-string">'r+'</span></code> - Read and write from the start.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'t'</span></code> - Text mode (default).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode (must be paired with one of above).</strong></li>
</ul><div><h3 id="exceptions-1">Exceptions</h3><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'FileNotFoundError'</span></code> can be raised when reading with <code class="python hljs"><span class="hljs-string">'r'</span></code> or <code class="python hljs"><span class="hljs-string">'r+'</span></code>.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'FileExistsError'</span></code> can be raised when writing with <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li>
Expand Down Expand Up @@ -1910,7 +1910,7 @@
<span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span><span class="hljs-params">(moves, state)</span>:</span>
<span class="hljs-keyword">while</span> state[<span class="hljs-string">'*'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> {p <span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items() <span class="hljs-keyword">if</span> id_ != <span class="hljs-string">'*'</span>}:
<span class="hljs-keyword">while</span> state[<span class="hljs-string">'*'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (state[id_] <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)):
id_, d = <span class="hljs-keyword">await</span> moves.get()
x, y = state[id_]
deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}
Expand Down Expand Up @@ -2680,20 +2680,20 @@
<ul>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'&lt;DF&gt;[col_key_1, col_key_2][row_key]'</span></code> to get the fifth result's values.</strong></li>
</ul>
<div><h4 id="dataframeencodedecodeplot">DataFrame — Encode, Decode, Plot:</h4><pre><code class="python language-python hljs">&lt;DF&gt; = pd.read_json/html(<span class="hljs-string">'&lt;str/path/url&gt;'</span>)
<div><h4 id="dataframeplotencodedecode">DataFrame — Plot, Encode, Decode:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
&lt;DF&gt;.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show()
</code></pre></div>

<pre><code class="python language-python hljs">&lt;DF&gt; = pd.read_json/html(<span class="hljs-string">'&lt;str/path/url&gt;'</span>)
&lt;DF&gt; = pd.read_csv/pickle/excel(<span class="hljs-string">'&lt;path/url&gt;'</span>)
&lt;DF&gt; = pd.read_sql(<span class="hljs-string">'&lt;table_name/query&gt;'</span>, &lt;connection&gt;)
&lt;DF&gt; = pd.read_clipboard()
</code></pre></div>

</code></pre>
<pre><code class="python language-python hljs">&lt;dict&gt; = &lt;DF&gt;.to_dict([<span class="hljs-string">'d/l/s/sp/r/i'</span>])
&lt;str&gt; = &lt;DF&gt;.to_json/html/csv/markdown/latex([&lt;path&gt;])
&lt;DF&gt;.to_pickle/excel(&lt;path&gt;)
&lt;DF&gt;.to_sql(<span class="hljs-string">'&lt;table_name&gt;'</span>, &lt;connection&gt;)
</code></pre>
<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
&lt;DF&gt;.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show()
</code></pre>
<div><h3 id="groupby">GroupBy</h3><p><strong>Object that groups together rows of a dataframe based on the value of the passed column.</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>df = DataFrame([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">6</span>]], index=list(<span class="hljs-string">'abc'</span>), columns=list(<span class="hljs-string">'xyz'</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>df.groupby(<span class="hljs-string">'z'</span>).get_group(<span class="hljs-number">6</span>)
x y
Expand Down Expand Up @@ -2884,7 +2884,7 @@


<footer>
<aside>February 27, 2022</aside>
<aside>March 1, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Expand Down

0 comments on commit 13d9744

Please sign in to comment.