Skip to content

Commit a658334

Browse files
committed
Regex, Format, Struct, Threading
1 parent 606208a commit a658334

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ import re
377377

378378
### Special Sequences
379379
* **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.**
380-
* **As shown below, it restricts special sequence matches to `'[\x00-\x7f]'` and prevents `'\s'` from accepting `'[\x1c\x1d\x1e\x1f]'`.**
380+
* **As shown below, it restricts special sequence matches to the first 128 characters and prevents `'\s'` from accepting `'[\x1c\x1d\x1e\x1f]'`.**
381381
* **Use a capital letter for negation.**
382382
```python
383383
'\d' == '[0-9]' # Matches decimal characters.
@@ -412,9 +412,10 @@ Format
412412
{<el>:.<10} # '<el>......'
413413
{<el>:0} # '<el>'
414414
```
415+
* **Use `'{<el>:{<str/int/float>}[...]}'` to set options dynamically.**
416+
* **Adding `'!r'` before the colon converts object to string by calling its [repr()](#class) method.**
415417

416418
### Strings
417-
**`'!r'` calls object's [repr()](#class) method, instead of [str()](#class), to get a string.**
418419
```python
419420
{'abcde'!r:10} # "'abcde' "
420421
{'abcde':10.3} # 'abc '
@@ -1286,7 +1287,9 @@ Enum
12861287
----
12871288
```python
12881289
from enum import Enum, auto
1290+
```
12891291

1292+
```python
12901293
class <enum_name>(Enum):
12911294
<member_name_1> = <value_1>
12921295
<member_name_2> = <value_2_a>, <value_2_b>
@@ -1857,7 +1860,7 @@ import sqlite3
18571860

18581861
#### Or:
18591862
```python
1860-
with <conn>: # Exits block with commit() or rollback(),
1863+
with <conn>: # Exits the block with commit() or rollback(),
18611864
<conn>.execute('<query>') # depending on whether an exception occurred.
18621865
```
18631866

@@ -1940,7 +1943,7 @@ def write_bytes(filename, bytes_obj):
19401943
Struct
19411944
------
19421945
* **Module that performs conversions between a sequence of numbers and a bytes object.**
1943-
* **System’s native type sizes and byte order are used by default.**
1946+
* **System’s type sizes and byte order are used by default.**
19441947

19451948
```python
19461949
from struct import pack, unpack, iter_unpack
@@ -1962,7 +1965,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03'
19621965

19631966
### Format
19641967
#### For standard type sizes start format string with:
1965-
* **`'='` - native byte order (usually little-endian)**
1968+
* **`'='` - system's byte order (usually little-endian)**
19661969
* **`'<'` - little-endian**
19671970
* **`'>'` - big-endian (also `'!'`)**
19681971

@@ -1998,7 +2001,7 @@ Memory View
19982001
* **A sequence object that points to the memory of another object.**
19992002
* **Each element can reference a single or multiple consecutive bytes, depending on format.**
20002003
* **Order and number of elements can be changed with slicing.**
2001-
* **Casting only works between char and other types and always uses native size and b. order.**
2004+
* **Casting only works between char and other types and uses system's sizes and byte order.**
20022005

20032006
```python
20042007
<mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable.
@@ -2052,26 +2055,25 @@ from concurrent.futures import ThreadPoolExecutor
20522055

20532056
### Thread
20542057
```python
2055-
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
2058+
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set the arguments.
20562059
<Thread>.start() # Starts the thread.
2057-
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
2058-
<Thread>.join() # Waits for thread to finish.
2060+
<bool> = <Thread>.is_alive() # Checks if the thread has finished executing.
2061+
<Thread>.join() # Waits for the thread to finish.
20592062
```
20602063
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
20612064
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
20622065

20632066
### Lock
20642067
```python
20652068
<lock> = RLock() # Lock that can only be released by the owner.
2066-
<lock>.acquire() # Waits for lock to be available.
2067-
<lock>.release() # Makes lock available again.
2069+
<lock>.acquire() # Waits for the lock to be available.
2070+
<lock>.release() # Makes the lock available again.
20682071
```
20692072

20702073
#### Or:
20712074
```python
2072-
lock = RLock()
2073-
with lock:
2074-
...
2075+
with <lock>: # Enters the block by calling acquire(),
2076+
... # and exits it with release().
20752077
```
20762078

20772079
### Semaphore, Event, Barrier

index.html

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@
508508

509509
<div><h3 id="specialsequences">Special Sequences</h3><ul>
510510
<li><strong>By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless <code class="python hljs"><span class="hljs-string">'flags=re.ASCII'</span></code> argument is used.</strong></li>
511-
<li><strong>As shown below, it restricts special sequence matches to <code class="python hljs"><span class="hljs-string">'[\x00-\x7f]'</span></code> and prevents <code class="python hljs"><span class="hljs-string">'\s'</span></code> from accepting <code class="python hljs"><span class="hljs-string">'[\x1c\x1d\x1e\x1f]'</span></code>.</strong></li>
511+
<li><strong>As shown below, it restricts special sequence matches to the first 128 characters and prevents <code class="python hljs"><span class="hljs-string">'\s'</span></code> from accepting <code class="python hljs"><span class="hljs-string">'[\x1c\x1d\x1e\x1f]'</span></code>.</strong></li>
512512
<li><strong>Use a capital letter for negation.</strong></li>
513513
</ul><pre><code class="python language-python hljs"><span class="hljs-string">'\d'</span> == <span class="hljs-string">'[0-9]'</span> <span class="hljs-comment"># Matches decimal characters.</span>
514514
<span class="hljs-string">'\w'</span> == <span class="hljs-string">'[a-zA-Z0-9_]'</span> <span class="hljs-comment"># Matches alphanumerics and underscore.</span>
@@ -536,12 +536,15 @@
536536
{&lt;el&gt;:<span class="hljs-number">0</span>} <span class="hljs-comment"># '&lt;el&gt;'</span>
537537
</code></pre></div>
538538

539-
<div><h3 id="strings">Strings</h3><p><strong><code class="python hljs"><span class="hljs-string">'!r'</span></code> calls object's <a href="#class">repr()</a> method, instead of <a href="#class">str()</a>, to get a string.</strong></p><pre><code class="python language-python hljs">{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
539+
<ul>
540+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'{&lt;el&gt;:{&lt;str/int/float&gt;}[...]}'</span></code> to set options dynamically.</strong></li>
541+
<li><strong>Adding <code class="python hljs"><span class="hljs-string">'!r'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong></li>
542+
</ul>
543+
<div><h3 id="strings">Strings</h3><pre><code class="python language-python hljs">{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
540544
{<span class="hljs-string">'abcde'</span>:<span class="hljs-number">10.3</span>} <span class="hljs-comment"># 'abc '</span>
541545
{<span class="hljs-string">'abcde'</span>:<span class="hljs-number">.3</span>} <span class="hljs-comment"># 'abc'</span>
542546
</code></pre></div>
543547

544-
545548
<div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span>
546549
{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span>
547550
{ <span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span>
@@ -1252,13 +1255,13 @@
12521255
<li><strong>Names of their required methods are stored in <code class="python hljs"><span class="hljs-string">'&lt;abc&gt;.__abstractmethods__'</span></code>.</strong></li>
12531256
</ul>
12541257
<div><h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto
1258+
</code></pre></div>
12551259

1256-
<span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">enum_name</span>&gt;<span class="hljs-params">(Enum)</span>:</span>
1260+
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">enum_name</span>&gt;<span class="hljs-params">(Enum)</span>:</span>
12571261
&lt;member_name_1&gt; = &lt;value_1&gt;
12581262
&lt;member_name_2&gt; = &lt;value_2_a&gt;, &lt;value_2_b&gt;
12591263
&lt;member_name_3&gt; = auto()
1260-
</code></pre></div>
1261-
1264+
</code></pre>
12621265
<ul>
12631266
<li><strong>If there are no numeric values before auto(), it returns 1.</strong></li>
12641267
<li><strong>Otherwise it returns an increment of the last numeric value.</strong></li>
@@ -1691,7 +1694,7 @@
16911694
&lt;conn&gt;.rollback() <span class="hljs-comment"># Discards all changes since the last commit.</span>
16921695
</code></pre></div>
16931696

1694-
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;conn&gt;: <span class="hljs-comment"># Exits block with commit() or rollback(),</span>
1697+
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;conn&gt;: <span class="hljs-comment"># Exits the block with commit() or rollback(),</span>
16951698
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># depending on whether an exception occurred.</span>
16961699
</code></pre></div>
16971700

@@ -1754,7 +1757,7 @@
17541757

17551758
<div><h2 id="struct"><a href="#struct" name="struct">#</a>Struct</h2><ul>
17561759
<li><strong>Module that performs conversions between a sequence of numbers and a bytes object.</strong></li>
1757-
<li><strong>System’s native type sizes and byte order are used by default.</strong></li>
1760+
<li><strong>System’s type sizes and byte order are used by default.</strong></li>
17581761
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, unpack, iter_unpack
17591762
</code></pre></div>
17601763

@@ -1770,7 +1773,7 @@
17701773
</code></pre></div>
17711774

17721775
<div><h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesstartformatstringwith">For standard type sizes start format string with:</h4><ul>
1773-
<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - native byte order (usually little-endian)</strong></li>
1776+
<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - system's byte order (usually little-endian)</strong></li>
17741777
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;'</span></code> - little-endian</strong></li>
17751778
<li><strong><code class="python hljs"><span class="hljs-string">'&gt;'</span></code> - big-endian (also <code class="python hljs"><span class="hljs-string">'!'</span></code>)</strong></li>
17761779
</ul></div></div><div><h4 id="integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:</h4><ul>
@@ -1803,7 +1806,7 @@
18031806
<li><strong>A sequence object that points to the memory of another object.</strong></li>
18041807
<li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
18051808
<li><strong>Order and number of elements can be changed with slicing.</strong></li>
1806-
<li><strong>Casting only works between char and other types and always uses native size and b. order.</strong></li>
1809+
<li><strong>Casting only works between char and other types and uses system's sizes and byte order.</strong></li>
18071810
</ul><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-comment"># Immutable if bytes, else mutable.</span>
18081811
&lt;real&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int or a float.</span>
18091812
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Mview with rearranged elements.</span>
@@ -1841,24 +1844,23 @@
18411844
</code></pre></div>
18421845

18431846

1844-
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">&lt;Thread&gt; = Thread(target=&lt;function&gt;) <span class="hljs-comment"># Use `args=&lt;collection&gt;` to set arguments.</span>
1847+
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">&lt;Thread&gt; = Thread(target=&lt;function&gt;) <span class="hljs-comment"># Use `args=&lt;collection&gt;` to set the arguments.</span>
18451848
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
1846-
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1847-
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for thread to finish.</span>
1849+
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
1850+
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for the thread to finish.</span>
18481851
</code></pre></div>
18491852

18501853
<ul>
18511854
<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>
18521855
<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>
18531856
</ul>
18541857
<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 the owner.</span>
1855-
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1856-
&lt;lock&gt;.release() <span class="hljs-comment"># Makes lock available again.</span>
1858+
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span>
1859+
&lt;lock&gt;.release() <span class="hljs-comment"># Makes the lock available again.</span>
18571860
</code></pre></div>
18581861

1859-
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
1860-
<span class="hljs-keyword">with</span> lock:
1861-
...
1862+
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;lock&gt;: <span class="hljs-comment"># Enters the block by calling acquire(),</span>
1863+
... <span class="hljs-comment"># and exits it with release().</span>
18621864
</code></pre></div>
18631865

18641866
<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-python hljs">&lt;Semaphore&gt; = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads.</span>

0 commit comments

Comments
 (0)