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

Skip to content

Commit 4ce2f4d

Browse files
committed
Dict, Regex, Logging
1 parent 6482fdb commit 4ce2f4d

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
@@ -77,7 +77,7 @@ Dictionary
7777
```python
7878
value = <dict>.get(key, default=None) # Returns default if key is missing.
7979
value = <dict>.setdefault(key, default=None) # Returns and writes default if key is missing.
80-
<dict> = collections.defaultdict(<type>) # Returns a dict with default value of type.
80+
<dict> = collections.defaultdict(<type>) # Returns a dict with default value `<type>()`.
8181
<dict> = collections.defaultdict(lambda: 1) # Returns a dict with default value 1.
8282
```
8383

@@ -353,7 +353,7 @@ Regex
353353
import re
354354
<str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences with 'new'.
355355
<list> = re.findall(<regex>, text) # Returns all occurrences as strings.
356-
<list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to include the matches.
356+
<list> = re.split(<regex>, text, maxsplit=0) # Add brackets around regex to include matches.
357357
<Match> = re.search(<regex>, text) # Searches for first occurrence of the pattern.
358358
<Match> = re.match(<regex>, text) # Searches only at the beginning of the text.
359359
<iter> = re.finditer(<regex>, text) # Returns all occurrences as Match objects.
@@ -1400,7 +1400,7 @@ finally:
14001400
```
14011401
* **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.**
14021402
* **Code inside the `'finally'` block will always be executed (unless a signal is received).**
1403-
* **All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try/except clause (only function blocks delimit scope).**
1403+
* **All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try/except clause (only function block delimits scope).**
14041404
* **To catch signals use `'signal.signal(signal_number, <func>)'`.**
14051405

14061406
### Catching Exceptions
@@ -2452,7 +2452,7 @@ import logging
24522452
```
24532453

24542454
```python
2455-
logging.basicConfig(filename=<path>) # Configures the root logger.
2455+
logging.basicConfig(filename=<path>) # Configures the root logger (see Setup).
24562456
logging.debug/info/warning/error/critical(<str>) # Logs to the root logger.
24572457
<Logger> = logging.getLogger(__name__) # Logger named after the module.
24582458
<Logger>.<level>(<str>) # Messages propagate to the root logger.
@@ -2463,7 +2463,7 @@ logging.debug/info/warning/error/critical(<str>) # Logs to the root logger.
24632463
```python
24642464
logging.basicConfig(
24652465
filename=None, # Logs to console (stderr) by default.
2466-
format='%(levelname)s:%(name)s:%(message)s', # Add `%(asctime)s` for datetime.
2466+
format='%(levelname)s:%(name)s:%(message)s', # Add `%(asctime)s` for local datetime.
24672467
level=logging.WARNING, # Drops messages with lower priority.
24682468
handlers=[logging.StreamHandler()] # Uses FileHandler if filename is set.
24692469
)

index.html

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

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

@@ -137,7 +137,7 @@
137137

138138
<pre><code class="python language-python hljs">value = &lt;dict&gt;.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns default if key is missing.</span>
139139
value = &lt;dict&gt;.setdefault(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns and writes default if key is missing.</span>
140-
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Returns a dict with default value of type.</span>
140+
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Returns a dict with default value `&lt;type&gt;()`.</span>
141141
&lt;dict&gt; = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Returns a dict with default value 1.</span>
142142
</code></pre>
143143
<pre><code class="python language-python hljs">&lt;dict&gt; = dict(&lt;collection&gt;) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
@@ -332,7 +332,7 @@
332332
<div><h2 id="regex"><a href="#regex" name="regex">#</a>Regex</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> re
333333
&lt;str&gt; = re.sub(&lt;regex&gt;, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span>
334334
&lt;list&gt; = re.findall(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as strings.</span>
335-
&lt;list&gt; = re.split(&lt;regex&gt;, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Use brackets in regex to include the matches.</span>
335+
&lt;list&gt; = re.split(&lt;regex&gt;, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to include matches.</span>
336336
&lt;Match&gt; = re.search(&lt;regex&gt;, text) <span class="hljs-comment"># Searches for first occurrence of the pattern.</span>
337337
&lt;Match&gt; = re.match(&lt;regex&gt;, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span>
338338
&lt;iter&gt; = re.finditer(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
@@ -1197,7 +1197,7 @@
11971197
<ul>
11981198
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'else'</span></code> block will only be executed if <code class="python hljs"><span class="hljs-string">'try'</span></code> block had no exceptions.</strong></li>
11991199
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed (unless a signal is received).</strong></li>
1200-
<li><strong>All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try/except clause (only function blocks delimit scope).</strong></li>
1200+
<li><strong>All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try/except clause (only function block delimits scope).</strong></li>
12011201
<li><strong>To catch signals use <code class="python hljs"><span class="hljs-string">'signal.signal(signal_number, &lt;func&gt;)'</span></code>.</strong></li>
12021202
</ul>
12031203
<div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;: ...
@@ -2011,15 +2011,15 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
20112011
<div><h2 id="logging"><a href="#logging" name="logging">#</a>Logging</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> logging
20122012
</code></pre></div>
20132013

2014-
<pre><code class="python language-python hljs">logging.basicConfig(filename=&lt;path&gt;) <span class="hljs-comment"># Configures the root logger.</span>
2014+
<pre><code class="python language-python hljs">logging.basicConfig(filename=&lt;path&gt;) <span class="hljs-comment"># Configures the root logger (see Setup).</span>
20152015
logging.debug/info/warning/error/critical(&lt;str&gt;) <span class="hljs-comment"># Logs to the root logger.</span>
20162016
&lt;Logger&gt; = logging.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span>
20172017
&lt;Logger&gt;.&lt;level&gt;(&lt;str&gt;) <span class="hljs-comment"># Messages propagate to the root logger.</span>
20182018
&lt;Logger&gt;.exception(&lt;str&gt;) <span class="hljs-comment"># Calls error() with caught exception.</span>
20192019
</code></pre>
20202020
<div><h3 id="setup">Setup</h3><pre><code class="python language-python hljs">logging.basicConfig(
20212021
filename=<span class="hljs-keyword">None</span>, <span class="hljs-comment"># Logs to console (stderr) by default.</span>
2022-
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add `%(asctime)s` for datetime.</span>
2022+
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add `%(asctime)s` for local datetime.</span>
20232023
level=logging.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span>
20242024
handlers=[logging.StreamHandler()] <span class="hljs-comment"># Uses FileHandler if filename is set.</span>
20252025
)
@@ -2928,7 +2928,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29282928

29292929

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

0 commit comments

Comments
 (0)