Browse Source

Struct, Threading

main
Jure Šorn 6 days ago
parent
commit
e85825b873
2 changed files with 15 additions and 15 deletions
  1. 8
      README.md
  2. 22
      index.html

8
README.md

@ -1997,8 +1997,8 @@ Struct
```python
from struct import pack, unpack
<bytes> = pack('<format>', <el_1> [, ...]) # Packs objects according to format string.
<tuple> = unpack('<format>', <bytes>) # Use iter_unpack() to get iterator of tuples.
<bytes> = pack('<format>', <num_1> [, ...]) # Packs numbers according to format string.
<tuple> = unpack('<format>', <bytes>) # Use iter_unpack() to get iterator of tuples.
```
```python
@ -2259,7 +2259,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
<Thread>.join() # Waits for the thread to finish executing.
```
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
* **Use `'daemon=True'`, or program won't be able to exit while the thread is alive.**
* **Use `'daemon=True'`, or the program won't be able to exit while the thread is alive.**
### Lock
```python
@ -2295,7 +2295,7 @@ with <lock>: # Enters the block by calling acq
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: ...`
<iter> = <Exec>.map(<func>, <args_1>, ...) # Multithreaded and non-lazy map(). Keeps order.
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Creates a thread and returns its Future obj.
<Exec>.shutdown() # Blocks until all threads finish executing.
<Exec>.shutdown() # Waits for all submitted threads to finish.
```
```python

22
index.html

@ -56,7 +56,7 @@
<body>
<header>
<aside>May 3, 2025</aside>
<aside>May 7, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1430,8 +1430,8 @@ args = p.parse_args() <span class="h
</code></pre>
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without the final component.</span>
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns final component w/o last extension.</span>
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns last extension prepended with a dot.</span>
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns name's last extension (e.g. '.py').</span>
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns name without its last extension.</span>
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all path's components as strings.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Path&gt;.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span>
@ -1591,8 +1591,8 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># depending on whether any exception occurred.</span>
</code></pre></div>
<div><h3 id="placeholders">Placeholders</h3><pre><code class="python language-python hljs">&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces '?'s in query with values.</span>
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces ':&lt;key&gt;'s with values.</span>
<div><h3 id="placeholders">Placeholders</h3><pre><code class="python language-python hljs">&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces every '?' with an item.</span>
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces every ':&lt;key&gt;' with a value.</span>
&lt;conn&gt;.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_coll&gt;) <span class="hljs-comment"># Runs execute() multiple times.</span>
</code></pre></div>
@ -1612,7 +1612,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<span class="hljs-keyword">from</span> sqlalchemy <span class="hljs-keyword">import</span> create_engine, text
&lt;engine&gt; = create_engine(<span class="hljs-string">'&lt;url&gt;'</span>) <span class="hljs-comment"># Url: 'dialect://user:password@host/dbname'.</span>
&lt;conn&gt; = &lt;engine&gt;.connect() <span class="hljs-comment"># Creates a connection. Also &lt;conn&gt;.close().</span>
&lt;cursor&gt; = &lt;conn&gt;.execute(text(<span class="hljs-string">'&lt;query&gt;'</span>), …) <span class="hljs-comment"># `&lt;dict&gt;`. Replaces ':&lt;key&gt;'s with values.</span>
&lt;cursor&gt; = &lt;conn&gt;.execute(text(<span class="hljs-string">'&lt;query&gt;'</span>), …) <span class="hljs-comment"># `&lt;dict&gt;`. Replaces every ':&lt;key&gt;' with value.</span>
<span class="hljs-keyword">with</span> &lt;conn&gt;.begin(): ... <span class="hljs-comment"># Exits the block with commit or rollback.</span>
</code></pre></div>
@ -1660,8 +1660,8 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<li><strong>System’s type sizes, byte order, and alignment rules are used by default.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, unpack
&lt;bytes&gt; = pack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;el_1&gt; [, ...]) <span class="hljs-comment"># Packs objects according to format string.</span>
&lt;tuple&gt; = unpack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;bytes&gt;) <span class="hljs-comment"># Use iter_unpack() to get iterator of tuples.</span>
&lt;bytes&gt; = pack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;num_1&gt; [, ...]) <span class="hljs-comment"># Packs numbers according to format string.</span>
&lt;tuple&gt; = unpack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;bytes&gt;) <span class="hljs-comment"># Use iter_unpack() to get iterator of tuples.</span>
</code></pre></div>
@ -1865,7 +1865,7 @@ delattr(&lt;obj&gt;, <span class="hljs-string">'&lt;name&gt;'</span>)
<ul>
<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>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or program won't be able to exit while the thread is alive.</strong></li>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or the program won't be able to exit while the thread is alive.</strong></li>
</ul>
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = Lock/RLock() <span class="hljs-comment"># RLock can only be released by acquirer.</span>
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span>
@ -1891,7 +1891,7 @@ delattr(&lt;obj&gt;, <span class="hljs-string">'&lt;name&gt;'</span>)
<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>
&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>
&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>
&lt;Exec&gt;.shutdown() <span class="hljs-comment"># Blocks until all threads finish executing.</span>
&lt;Exec&gt;.shutdown() <span class="hljs-comment"># Waits for all submitted threads to finish.</span>
</code></pre></div>
<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>
@ -2940,7 +2940,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>May 3, 2025</aside>
<aside>May 7, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save