|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>September 15, 2023</aside> |
| 57 | + <aside>September 16, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
137 | 137 |
|
138 | 138 | <pre><code class="python language-python hljs">value = <dict>.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns default if key is missing.</span>
|
139 | 139 | value = <dict>.setdefault(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns and writes default if key is missing.</span>
|
140 |
| -<dict> = collections.defaultdict(<type>) <span class="hljs-comment"># Returns a dict with default value of type.</span> |
| 140 | +<dict> = collections.defaultdict(<type>) <span class="hljs-comment"># Returns a dict with default value `<type>()`.</span> |
141 | 141 | <dict> = 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>
|
142 | 142 | </code></pre>
|
143 | 143 | <pre><code class="python language-python hljs"><dict> = dict(<collection>) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
|
|
332 | 332 | <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
|
333 | 333 | <str> = re.sub(<regex>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span>
|
334 | 334 | <list> = re.findall(<regex>, text) <span class="hljs-comment"># Returns all occurrences as strings.</span>
|
335 |
| -<list> = re.split(<regex>, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Use brackets in regex to include the matches.</span> |
| 335 | +<list> = re.split(<regex>, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to include matches.</span> |
336 | 336 | <Match> = re.search(<regex>, text) <span class="hljs-comment"># Searches for first occurrence of the pattern.</span>
|
337 | 337 | <Match> = re.match(<regex>, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span>
|
338 | 338 | <iter> = re.finditer(<regex>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
|
|
1197 | 1197 | <ul>
|
1198 | 1198 | <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>
|
1199 | 1199 | <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> |
1201 | 1201 | <li><strong>To catch signals use <code class="python hljs"><span class="hljs-string">'signal.signal(signal_number, <func>)'</span></code>.</strong></li>
|
1202 | 1202 | </ul>
|
1203 | 1203 | <div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception>: ...
|
@@ -2011,15 +2011,15 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2011 | 2011 | <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
|
2012 | 2012 | </code></pre></div>
|
2013 | 2013 |
|
2014 |
| -<pre><code class="python language-python hljs">logging.basicConfig(filename=<path>) <span class="hljs-comment"># Configures the root logger.</span> |
| 2014 | +<pre><code class="python language-python hljs">logging.basicConfig(filename=<path>) <span class="hljs-comment"># Configures the root logger (see Setup).</span> |
2015 | 2015 | logging.debug/info/warning/error/critical(<str>) <span class="hljs-comment"># Logs to the root logger.</span>
|
2016 | 2016 | <Logger> = logging.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span>
|
2017 | 2017 | <Logger>.<level>(<str>) <span class="hljs-comment"># Messages propagate to the root logger.</span>
|
2018 | 2018 | <Logger>.exception(<str>) <span class="hljs-comment"># Calls error() with caught exception.</span>
|
2019 | 2019 | </code></pre>
|
2020 | 2020 | <div><h3 id="setup">Setup</h3><pre><code class="python language-python hljs">logging.basicConfig(
|
2021 | 2021 | 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> |
2023 | 2023 | level=logging.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span>
|
2024 | 2024 | handlers=[logging.StreamHandler()] <span class="hljs-comment"># Uses FileHandler if filename is set.</span>
|
2025 | 2025 | )
|
@@ -2928,7 +2928,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2928 | 2928 |
|
2929 | 2929 |
|
2930 | 2930 | <footer>
|
2931 |
| - <aside>September 15, 2023</aside> |
| 2931 | + <aside>September 16, 2023</aside> |
2932 | 2932 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2933 | 2933 | </footer>
|
2934 | 2934 |
|
|
0 commit comments