Skip to content

Commit 989c834

Browse files
committed
Duck types, NumPy, Cython
1 parent d3c9330 commit 989c834

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ class MySortable:
11641164
### Iterator
11651165
* **Any object that has methods next() and iter() is an iterator.**
11661166
* **Next() should return next item or raise StopIteration exception.**
1167-
* **Iter() should return 'self'.**
1167+
* **Iter() should return 'self', i.e. unmodified object on which it was called.**
11681168
```python
11691169
class Counter:
11701170
def __init__(self):
@@ -2718,14 +2718,14 @@ import numpy as np
27182718
### Broadcasting
27192719
**A set of rules by which NumPy functions operate on arrays of different shapes.**
27202720
```python
2721-
left = np.array([ 0.1, 0.6, 0.8 ]) # `left.shape == (3,)`
2722-
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
2721+
left = np.array([0.1, 0.6, 0.8]) # `left.shape == (3,)`
2722+
right = np.array([[0.1], [0.6], [0.8]]) # `right.shape == (3, 1)`
27232723
```
27242724

27252725
#### 1. If array shapes differ in length, left-pad the shorter shape with ones:
27262726
```python
27272727
left = np.array([[0.1, 0.6, 0.8]]) # `left.shape == (1, 3)`
2728-
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
2728+
right = np.array([[0.1], [0.6], [0.8]]) # `right.shape == (3, 1)`
27292729
```
27302730

27312731
#### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
@@ -3525,7 +3525,7 @@ Appendix
35253525
```python
35263526
# $ pip3 install cython
35273527
import pyximport; pyximport.install() # Module that runs imported Cython scripts.
3528-
import <cython_script> # Script's filename needs a '.pyx' extension.
3528+
import <cython_script> # Script must be saved with '.pyx' extension.
35293529
<cython_script>.main() # Main() isn't automatically executed.
35303530
```
35313531

index.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<body>
5757
<header>
58-
<aside>December 25, 2024</aside>
58+
<aside>December 26, 2024</aside>
5959
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6060
</header>
6161

@@ -978,7 +978,7 @@
978978
<div><h3 id="iterator-1">Iterator</h3><ul>
979979
<li><strong>Any object that has methods next() and iter() is an iterator.</strong></li>
980980
<li><strong>Next() should return next item or raise StopIteration exception.</strong></li>
981-
<li><strong>Iter() should return 'self'.</strong></li>
981+
<li><strong>Iter() should return 'self', i.e. unmodified object on which it was called.</strong></li>
982982
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span>:</span>
983983
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
984984
self.i = <span class="hljs-number">0</span>
@@ -2216,13 +2216,13 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
22162216
<li><strong><code class="python hljs"><span class="hljs-string">'ix_([1, 2], [3, 4])'</span></code> returns <code class="python hljs"><span class="hljs-string">'[[1], [2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4]]'</span></code>. Due to broadcasting rules, this is the same as using <code class="python hljs"><span class="hljs-string">'[[1, 1], [2, 2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4], [3, 4]]'</span></code>.</strong></li>
22172217
<li><strong>Any value that is broadcastable to the indexed shape can be assigned to the selection.</strong></li>
22182218
</ul>
2219-
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><code class="python language-python hljs">left = np.array([ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]) <span class="hljs-comment"># `left.shape == (3,)`</span>
2220-
right = np.array([[<span class="hljs-number">0.1</span>],[<span class="hljs-number">0.6</span>],[<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
2219+
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><code class="python language-python hljs">left = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]) <span class="hljs-comment"># `left.shape == (3,)`</span>
2220+
right = np.array([[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
22212221
</code></pre></div>
22222222

22232223

22242224
<div><h4 id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><code class="python language-python hljs">left = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `left.shape == (1, 3)`</span>
2225-
right = np.array([[<span class="hljs-number">0.1</span>],[<span class="hljs-number">0.6</span>],[<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
2225+
right = np.array([[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
22262226
</code></pre></div>
22272227

22282228
<div><h4 id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><code class="python language-python hljs">left = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], <span class="hljs-comment"># `left.shape == (3, 3)`</span>
@@ -2868,7 +2868,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
28682868

28692869
<div><h2 id="appendix"><a href="#appendix" name="appendix">#</a>Appendix</h2><div><h3 id="cython">Cython</h3><p><strong>Library that compiles Python-like code into C.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install cython</span>
28702870
<span class="hljs-keyword">import</span> pyximport; pyximport.install() <span class="hljs-comment"># Module that runs imported Cython scripts.</span>
2871-
<span class="hljs-keyword">import</span> &lt;cython_script&gt; <span class="hljs-comment"># Script's filename needs a '.pyx' extension.</span>
2871+
<span class="hljs-keyword">import</span> &lt;cython_script&gt; <span class="hljs-comment"># Script must be saved with '.pyx' extension.</span>
28722872
&lt;cython_script&gt;.main() <span class="hljs-comment"># Main() isn't automatically executed.</span>
28732873
</code></pre></div></div>
28742874

@@ -2934,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29342934

29352935

29362936
<footer>
2937-
<aside>December 25, 2024</aside>
2937+
<aside>December 26, 2024</aside>
29382938
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29392939
</footer>
29402940

0 commit comments

Comments
 (0)