Browse Source

Dict, Regex, Logging

pull/167/head
Jure Šorn 1 year ago
parent
commit
4ce2f4dd1f
2 changed files with 12 additions and 12 deletions
  1. 10
      README.md
  2. 14
      index.html

10
README.md

@ -77,7 +77,7 @@ Dictionary
```python ```python
value = <dict>.get(key, default=None) # Returns default if key is missing. value = <dict>.get(key, default=None) # Returns default if key is missing.
value = <dict>.setdefault(key, default=None) # Returns and writes default if key is missing. value = <dict>.setdefault(key, default=None) # Returns and writes default if key is missing.
<dict> = collections.defaultdict(<type>) # Returns a dict with default value of type.
<dict> = collections.defaultdict(<type>) # Returns a dict with default value `<type>()`.
<dict> = collections.defaultdict(lambda: 1) # Returns a dict with default value 1. <dict> = collections.defaultdict(lambda: 1) # Returns a dict with default value 1.
``` ```
@ -353,7 +353,7 @@ Regex
import re import re
<str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences with 'new'. <str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences with 'new'.
<list> = re.findall(<regex>, text) # Returns all occurrences as strings. <list> = re.findall(<regex>, text) # Returns all occurrences as strings.
<list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to include the matches.
<list> = re.split(<regex>, text, maxsplit=0) # Add brackets around regex to include matches.
<Match> = re.search(<regex>, text) # Searches for first occurrence of the pattern. <Match> = re.search(<regex>, text) # Searches for first occurrence of the pattern.
<Match> = re.match(<regex>, text) # Searches only at the beginning of the text. <Match> = re.match(<regex>, text) # Searches only at the beginning of the text.
<iter> = re.finditer(<regex>, text) # Returns all occurrences as Match objects. <iter> = re.finditer(<regex>, text) # Returns all occurrences as Match objects.
@ -1400,7 +1400,7 @@ finally:
``` ```
* **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.** * **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.**
* **Code inside the `'finally'` block will always be executed (unless a signal is received).** * **Code inside the `'finally'` block will always be executed (unless a signal is received).**
* **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).**
* **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).**
* **To catch signals use `'signal.signal(signal_number, <func>)'`.** * **To catch signals use `'signal.signal(signal_number, <func>)'`.**
### Catching Exceptions ### Catching Exceptions
@ -2452,7 +2452,7 @@ import logging
``` ```
```python ```python
logging.basicConfig(filename=<path>) # Configures the root logger.
logging.basicConfig(filename=<path>) # Configures the root logger (see Setup).
logging.debug/info/warning/error/critical(<str>) # Logs to the root logger. logging.debug/info/warning/error/critical(<str>) # Logs to the root logger.
<Logger> = logging.getLogger(__name__) # Logger named after the module. <Logger> = logging.getLogger(__name__) # Logger named after the module.
<Logger>.<level>(<str>) # Messages propagate to the root logger. <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.
```python ```python
logging.basicConfig( logging.basicConfig(
filename=None, # Logs to console (stderr) by default. filename=None, # Logs to console (stderr) by default.
format='%(levelname)s:%(name)s:%(message)s', # Add `%(asctime)s` for datetime.
format='%(levelname)s:%(name)s:%(message)s', # Add `%(asctime)s` for local datetime.
level=logging.WARNING, # Drops messages with lower priority. level=logging.WARNING, # Drops messages with lower priority.
handlers=[logging.StreamHandler()] # Uses FileHandler if filename is set. handlers=[logging.StreamHandler()] # Uses FileHandler if filename is set.
) )

14
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>September 15, 2023</aside>
<aside>September 16, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -137,7 +137,7 @@ list_of_chars = list(&lt;str&gt;)
<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> <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>
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> 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>
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Returns a dict with default value of type.</span>
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Returns a dict with default value `&lt;type&gt;()`.</span>
&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> &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>
</code></pre> </code></pre>
<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> <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 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<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 <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
&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> &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>
&lt;list&gt; = re.findall(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as strings.</span> &lt;list&gt; = re.findall(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as strings.</span>
&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>
&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>
&lt;Match&gt; = re.search(&lt;regex&gt;, text) <span class="hljs-comment"># Searches for first occurrence of the pattern.</span> &lt;Match&gt; = re.search(&lt;regex&gt;, text) <span class="hljs-comment"># Searches for first occurrence of the pattern.</span>
&lt;Match&gt; = re.match(&lt;regex&gt;, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span> &lt;Match&gt; = re.match(&lt;regex&gt;, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span>
&lt;iter&gt; = re.finditer(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span> &lt;iter&gt; = re.finditer(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
@ -1197,7 +1197,7 @@ LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-st
<ul> <ul>
<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> <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>
<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> <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>
<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>
<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>
<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> <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>
</ul> </ul>
<div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;: ... <div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;: ...
@ -2011,7 +2011,7 @@ print(table)
<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 <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
</code></pre></div> </code></pre></div>
<pre><code class="python language-python hljs">logging.basicConfig(filename=&lt;path&gt;) <span class="hljs-comment"># Configures the root logger.</span>
<pre><code class="python language-python hljs">logging.basicConfig(filename=&lt;path&gt;) <span class="hljs-comment"># Configures the root logger (see Setup).</span>
logging.debug/info/warning/error/critical(&lt;str&gt;) <span class="hljs-comment"># Logs to the root logger.</span> logging.debug/info/warning/error/critical(&lt;str&gt;) <span class="hljs-comment"># Logs to the root logger.</span>
&lt;Logger&gt; = logging.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span> &lt;Logger&gt; = logging.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span>
&lt;Logger&gt;.&lt;level&gt;(&lt;str&gt;) <span class="hljs-comment"># Messages propagate to the root logger.</span> &lt;Logger&gt;.&lt;level&gt;(&lt;str&gt;) <span class="hljs-comment"># Messages propagate to the root logger.</span>
@ -2019,7 +2019,7 @@ logging.debug/info/warning/error/critical(&lt;str&gt;) <span class="hljs-commen
</code></pre> </code></pre>
<div><h3 id="setup">Setup</h3><pre><code class="python language-python hljs">logging.basicConfig( <div><h3 id="setup">Setup</h3><pre><code class="python language-python hljs">logging.basicConfig(
filename=<span class="hljs-keyword">None</span>, <span class="hljs-comment"># Logs to console (stderr) by default.</span> filename=<span class="hljs-keyword">None</span>, <span class="hljs-comment"># Logs to console (stderr) by default.</span>
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add `%(asctime)s` for datetime.</span>
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add `%(asctime)s` for local datetime.</span>
level=logging.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span> level=logging.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span>
handlers=[logging.StreamHandler()] <span class="hljs-comment"># Uses FileHandler if filename is set.</span> handlers=[logging.StreamHandler()] <span class="hljs-comment"># Uses FileHandler if filename is set.</span>
) )
@ -2928,7 +2928,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer> <footer>
<aside>September 15, 2023</aside>
<aside>September 16, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

Loading…
Cancel
Save