Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18f9046

Browse files
committed
Updated Threading, Operator, Audio, Pygame
1 parent 6ae442d commit 18f9046

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
21042104
```
21052105
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
21062106
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
2107-
* **To delay thread execution use `'Timer(<float>, <func>)'` instead of Thread().**
2107+
* **To delay thread execution use `'Timer(seconds, <func>)'` instead of Thread().**
21082108

21092109
### Lock
21102110
```python
@@ -2137,16 +2137,16 @@ with <lock>: # Enters the block by calling acq
21372137

21382138
### Thread Pool Executor
21392139
```python
2140-
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: `
2140+
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: ...`
21412141
<iter> = <Exec>.map(<func>, <args_1>, ...) # Multithreaded and non-lazy map(). Keeps order.
21422142
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Creates a thread and returns its Future obj.
2143-
<Exec>.shutdown(wait=True) # Blocks until all threads finish executing.
2143+
<Exec>.shutdown() # Blocks until all threads finish executing.
21442144
```
21452145

21462146
```python
21472147
<bool> = <Future>.done() # Checks if the thread has finished executing.
21482148
<obj> = <Future>.result(timeout=None) # Waits for thread to finish and returns result.
2149-
<bool> = <Future>.cancel() # Returns False if thread is already running.
2149+
<bool> = <Future>.cancel() # Cancels or returns False if running/finished.
21502150
<iter> = as_completed(<coll_of_Futures>) # Each Future is yielded as it completes.
21512151
```
21522152
* **Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.**
@@ -2159,13 +2159,13 @@ Operator
21592159
**Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.**
21602160
```python
21612161
import operator as op
2162-
<bool> = op.not_(<obj>) # or, and (both missing), not
2163-
<bool> = op.eq/ne/lt/le/gt/ge/contains/is_(<obj>, <obj>) # ==, !=, <, <=, >, >=, in, is
2164-
<obj> = op.or_/xor/and_(<int/set>, <int/set>) # |, ^, &
2165-
<obj> = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>) # +, -, *, /, //, %
2166-
<num> = op.neg/invert(<num>) # -, ~
2167-
<num> = op.pow(<num>, <num>) # **
2168-
<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...]) # [index/key], .name, .name()
2162+
<bool> = op.not_(<obj>) # not (or/and bind even less)
2163+
<bool> = op.eq/ne/lt/le/gt/ge/contains/is_(<obj>, <obj>) # ==, !=, <, <=, >, >=, in, is
2164+
<obj> = op.or_/xor/and_(<int/set>, <int/set>) # |, ^, &
2165+
<obj> = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>) # +, -, *, /, //, %
2166+
<num> = op.neg/invert(<num>) # -, ~
2167+
<num> = op.pow(<num>, <num>) # **
2168+
<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...]) # [index/key], .name, .name()
21692169
```
21702170

21712171
```python
@@ -2920,22 +2920,22 @@ samples_f = (sin(i * 2 * pi * 440 / 44100) for i in range(100_000))
29202920
write_to_wav_file('test.wav', samples_f)
29212921
```
29222922

2923-
#### Adds noise to a mono WAV file:
2923+
#### Adds noise to the mono WAV file:
29242924
```python
29252925
from random import random
29262926
add_noise = lambda value: value + (random() - 0.5) * 0.03
29272927
samples_f = (add_noise(f) for f in read_wav_file('test.wav'))
29282928
write_to_wav_file('test.wav', samples_f)
29292929
```
29302930

2931-
#### Plays a WAV file:
2931+
#### Plays the WAV file:
29322932
```python
29332933
# $ pip3 install simpleaudio
29342934
from simpleaudio import play_buffer
29352935
with wave.open('test.wav', 'rb') as file:
29362936
p = file.getparams()
29372937
frames = file.readframes(-1)
2938-
play_buffer(frames, p.nchannels, p.sampwidth, p.framerate)
2938+
play_buffer(frames, p.nchannels, p.sampwidth, p.framerate).wait_done()
29392939
```
29402940

29412941
### Text to Speech
@@ -3043,7 +3043,7 @@ rect(<Surf>, color, <Rect>, width=0) # Also polygon(<Surf>, color, po
30433043

30443044
### Sound
30453045
```python
3046-
<Sound> = pg.mixer.Sound(<path/file/bytes>) # Loads WAV file or array of signed shorts.
3046+
<Sound> = pg.mixer.Sound(<path/file/bytes>) # WAV file or bytes/array of signed shorts.
30473047
<Sound>.play/stop() # Also <Sound>.set_volume(<float>).
30483048
```
30493049

index.html

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>September 5, 2023</aside>
57+
<aside>September 7, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -1741,7 +1741,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17411741
<ul>
17421742
<li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=&lt;dict&gt;'</span></code> to pass keyword arguments to the function.</strong></li>
17431743
<li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li>
1744-
<li><strong>To delay thread execution use <code class="python hljs"><span class="hljs-string">'Timer(&lt;float&gt;, &lt;func&gt;)'</span></code> instead of Thread().</strong></li>
1744+
<li><strong>To delay thread execution use <code class="python hljs"><span class="hljs-string">'Timer(seconds, &lt;func&gt;)'</span></code> instead of Thread().</strong></li>
17451745
</ul>
17461746
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by acquirer.</span>
17471747
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span>
@@ -1764,15 +1764,15 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17641764
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
17651765
</code></pre></div>
17661766

1767-
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><pre><code class="python language-python hljs">&lt;Exec&gt; = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as &lt;name&gt;: `</span>
1767+
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><pre><code class="python language-python hljs">&lt;Exec&gt; = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as &lt;name&gt;: ...`</span>
17681768
&lt;iter&gt; = &lt;Exec&gt;.map(&lt;func&gt;, &lt;args_1&gt;, ...) <span class="hljs-comment"># Multithreaded and non-lazy map(). Keeps order.</span>
17691769
&lt;Futr&gt; = &lt;Exec&gt;.submit(&lt;func&gt;, &lt;arg_1&gt;, ...) <span class="hljs-comment"># Creates a thread and returns its Future obj.</span>
1770-
&lt;Exec&gt;.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Blocks until all threads finish executing.</span>
1770+
&lt;Exec&gt;.shutdown() <span class="hljs-comment"># Blocks until all threads finish executing.</span>
17711771
</code></pre></div>
17721772

17731773
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Future&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
17741774
&lt;obj&gt; = &lt;Future&gt;.result(timeout=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1775-
&lt;bool&gt; = &lt;Future&gt;.cancel() <span class="hljs-comment"># Returns False if thread is already running.</span>
1775+
&lt;bool&gt; = &lt;Future&gt;.cancel() <span class="hljs-comment"># Cancels or returns False if running/finished.</span>
17761776
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Each Future is yielded as it completes.</span>
17771777
</code></pre>
17781778
<ul>
@@ -1781,13 +1781,13 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17811781
<li><strong>ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be <a href="#pickle">pickable</a>. Queues must be sent using executor's 'initargs' and 'initializer' parameters.</strong></li>
17821782
</ul>
17831783
<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
1784-
&lt;bool&gt; = op.not_(&lt;obj&gt;) <span class="hljs-comment"># or, and (both missing), not</span>
1785-
&lt;bool&gt; = op.eq/ne/lt/le/gt/ge/contains/is_(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># ==, !=, &lt;, &lt;=, &gt;, &gt;=, in, is</span>
1786-
&lt;obj&gt; = op.or_/xor/and_(&lt;int/set&gt;, &lt;int/set&gt;) <span class="hljs-comment"># |, ^, &amp;</span>
1787-
&lt;obj&gt; = op.add/sub/mul/truediv/floordiv/mod(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># +, -, *, /, //, %</span>
1788-
&lt;num&gt; = op.neg/invert(&lt;num&gt;) <span class="hljs-comment"># -, ~</span>
1789-
&lt;num&gt; = op.pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># **</span>
1790-
&lt;func&gt; = op.itemgetter/attrgetter/methodcaller(&lt;obj&gt; [, ...]) <span class="hljs-comment"># [index/key], .name, .name()</span>
1784+
&lt;bool&gt; = op.not_(&lt;obj&gt;) <span class="hljs-comment"># not (or/and bind even less)</span>
1785+
&lt;bool&gt; = op.eq/ne/lt/le/gt/ge/contains/is_(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># ==, !=, &lt;, &lt;=, &gt;, &gt;=, in, is</span>
1786+
&lt;obj&gt; = op.or_/xor/and_(&lt;int/set&gt;, &lt;int/set&gt;) <span class="hljs-comment"># |, ^, &amp;</span>
1787+
&lt;obj&gt; = op.add/sub/mul/truediv/floordiv/mod(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># +, -, *, /, //, %</span>
1788+
&lt;num&gt; = op.neg/invert(&lt;num&gt;) <span class="hljs-comment"># -, ~</span>
1789+
&lt;num&gt; = op.pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># **</span>
1790+
&lt;func&gt; = op.itemgetter/attrgetter/methodcaller(&lt;obj&gt; [, ...]) <span class="hljs-comment"># [index/key], .name, .name()</span>
17911791
</code></pre></div>
17921792

17931793

@@ -2389,18 +2389,18 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
23892389
</code></pre></div></div>
23902390

23912391

2392-
<div><h4 id="addsnoisetoamonowavfile">Adds noise to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random
2392+
<div><h4 id="addsnoisetothemonowavfile">Adds noise to the mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random
23932393
add_noise = <span class="hljs-keyword">lambda</span> value: value + (random() - <span class="hljs-number">0.5</span>) * <span class="hljs-number">0.03</span>
23942394
samples_f = (add_noise(f) <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> read_wav_file(<span class="hljs-string">'test.wav'</span>))
23952395
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, samples_f)
23962396
</code></pre></div>
23972397

2398-
<div><h4 id="playsawavfile">Plays a WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
2398+
<div><h4 id="playsthewavfile">Plays the WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
23992399
<span class="hljs-keyword">from</span> simpleaudio <span class="hljs-keyword">import</span> play_buffer
24002400
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'test.wav'</span>, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
24012401
p = file.getparams()
24022402
frames = file.readframes(<span class="hljs-number">-1</span>)
2403-
play_buffer(frames, p.nchannels, p.sampwidth, p.framerate)
2403+
play_buffer(frames, p.nchannels, p.sampwidth, p.framerate).wait_done()
24042404
</code></pre></div>
24052405

24062406
<div><h3 id="texttospeech">Text to Speech</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pyttsx3</span>
@@ -2482,7 +2482,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
24822482
&lt;Surf&gt; = &lt;Font&gt;.render(text, antialias, color) <span class="hljs-comment"># Background color can be specified at the end.</span>
24832483
</code></pre></div>
24842484

2485-
<div><h3 id="sound">Sound</h3><pre><code class="python language-python hljs">&lt;Sound&gt; = pg.mixer.Sound(&lt;path/file/bytes&gt;) <span class="hljs-comment"># Loads WAV file or array of signed shorts.</span>
2485+
<div><h3 id="sound">Sound</h3><pre><code class="python language-python hljs">&lt;Sound&gt; = pg.mixer.Sound(&lt;path/file/bytes&gt;) <span class="hljs-comment"># WAV file or bytes/array of signed shorts.</span>
24862486
&lt;Sound&gt;.play/stop() <span class="hljs-comment"># Also &lt;Sound&gt;.set_volume(&lt;float&gt;).</span>
24872487
</code></pre></div>
24882488

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

29292929

29302930
<footer>
2931-
<aside>September 5, 2023</aside>
2931+
<aside>September 7, 2023</aside>
29322932
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29332933
</footer>
29342934

0 commit comments

Comments
 (0)