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

Skip to content

Commit 0fab178

Browse files
committed
Datetime, Threading, Operator, Logging
1 parent b8bec1c commit 0fab178

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ from dateutil.tz import tzlocal, gettz
655655
```python
656656
<bool> = <D/T/DTn> > <D/T/DTn> # Ignores time jumps (fold attribute). Also ==.
657657
<bool> = <DTa> > <DTa> # Ignores time jumps if they share tzinfo object.
658-
<TD> = <D/DTn> - <D/DTn> # Returns the difference. Ignores time jumps.
658+
<TD> = <D/DTn> - <D/DTn> # Ignores jumps. Convert to UTC for actual delta.
659659
<TD> = <DTa> - <DTa> # Ignores time jumps if they share tzinfo object.
660660
<D/DT> = <D/DT> ± <TD> # Returned datetime can fall into missing hour.
661661
<TD> = <TD> * <int/float> # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
@@ -2148,7 +2148,7 @@ with <lock>: # Enters the block by calling acq
21482148
<bool> = <Future>.done() # Checks if the thread has finished executing.
21492149
<obj> = <Future>.result(timeout=None) # Waits for thread to finish and returns result.
21502150
<bool> = <Future>.cancel() # Cancels or returns False if running/finished.
2151-
<iter> = as_completed(<coll_of_Futures>) # Each Future is yielded as it completes.
2151+
<iter> = as_completed(<coll_of_Futures>) # Next() waits for next completed Future.
21522152
```
21532153
* **Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.**
21542154
* **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.**
@@ -2163,6 +2163,7 @@ import operator as op
21632163
<bool> = op.not_(<obj>) # or, and, not (or/and missing)
21642164
<bool> = op.eq/ne/lt/le/gt/ge/contains/is_(<obj>, <obj>) # ==, !=, <, <=, >, >=, in, is
21652165
<obj> = op.or_/xor/and_(<int/set>, <int/set>) # |, ^, &
2166+
<int> = op.lshift/rshift(<int>, <int>) # <<, >>
21662167
<obj> = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>) # +, -, *, /, //, %
21672168
<num> = op.neg/invert(<num>) # -, ~
21682169
<num> = op.pow(<num>, <num>) # **
@@ -2174,10 +2175,9 @@ elementwise_sum = map(op.add, list_a, list_b)
21742175
sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
21752176
sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
21762177
product_of_elems = functools.reduce(op.mul, <collection>)
2177-
union_of_sets = functools.reduce(op.or_, <coll_of_sets>)
21782178
first_element = op.methodcaller('pop', 0)(<list>)
21792179
```
2180-
* **Bitwise operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.**
2180+
* **Bitwise operators require objects to have or(), xor(), and(), lshift(), rshift() and invert() special methods, unlike logical operators that work on all types of objects.**
21812181
* **Also: `'<bool> = <bool> &|^ <bool>'` and `'<int> = <bool> &|^ <int>'`.**
21822182

21832183

@@ -2453,34 +2453,34 @@ import logging
24532453
```
24542454

24552455
```python
2456-
logging.basicConfig(filename=<path>) # Configures the root logger (see Setup).
2457-
logging.debug/info/warning/error/critical(<str>) # Logs to the root logger.
2458-
<Logger> = logging.getLogger(__name__) # Logger named after the module.
2459-
<Logger>.<level>(<str>) # Logs to the logger.
2460-
<Logger>.exception(<str>) # Calls error() with caught exception.
2456+
logging.basicConfig(filename=<path>, level='DEBUG') # Configures the root logger (see Setup).
2457+
logging.debug/info/warning/error/critical(<str>) # Logs to the root logger.
2458+
<Logger> = logging.getLogger(__name__) # Logger named after the module.
2459+
<Logger>.<level>(<str>) # Logs to the logger.
2460+
<Logger>.exception(<str>) # Calls error() with caught exception.
24612461
```
24622462

24632463
### Setup
24642464
```python
24652465
logging.basicConfig(
2466-
filename=None, # Logs to console (stderr) by default.
2467-
format='%(levelname)s:%(name)s:%(message)s', # Add `%(asctime)s` for local datetime.
2468-
level=logging.WARNING, # Drops messages with lower priority.
2469-
handlers=[logging.StreamHandler()] # Uses FileHandler if filename is set.
2466+
filename=None, # Logs to console (stderr) by default.
2467+
format='%(levelname)s:%(name)s:%(message)s', # Add `%(asctime)s` for local datetime.
2468+
level=logging.WARNING, # Drops messages with lower priority.
2469+
handlers=[logging.StreamHandler()] # Uses FileHandler if filename is set.
24702470
)
24712471
```
24722472

24732473
```python
2474-
<Formatter> = logging.Formatter('<format>') # Creates a Formatter.
2475-
<Handler> = logging.FileHandler(<path>) # Creates a Handler.
2476-
<Handler>.setFormatter(<Formatter>) # Adds Formatter to the Handler.
2477-
<Handler>.setLevel(<int/str>) # Processes all messages by default.
2478-
<Logger>.addHandler(<Handler>) # Adds Handler to the Logger.
2479-
<Logger>.setLevel(<int/str>) # What is sent to its/ancestor's handlers.
2474+
<Formatter> = logging.Formatter('<format>') # Creates a Formatter.
2475+
<Handler> = logging.FileHandler(<path>) # Creates a Handler.
2476+
<Handler>.setFormatter(<Formatter>) # Adds Formatter to the Handler.
2477+
<Handler>.setLevel(<int/str>) # Processes all messages by default.
2478+
<Logger>.addHandler(<Handler>) # Adds Handler to the Logger.
2479+
<Logger>.setLevel(<int/str>) # What is sent to its/ancestor's handlers.
24802480
```
24812481
* **Parent logger can be specified by naming the child logger `'<parent>.<name>'`.**
24822482
* **If logger doesn't have a set level it inherits it from the first ancestor that does.**
2483-
* **Formatter also supports: pathname, filename, funcName, lineno, thread and process.**
2483+
* **Formatter also accepts: pathname, filename, funcName, lineno, thread and process.**
24842484
* **A `'handlers.RotatingFileHandler'` creates and deletes log files based on 'maxBytes' and 'backupCount' arguments.**
24852485

24862486

index.html

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

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

@@ -580,7 +580,7 @@
580580
</ul>
581581
<div><h3 id="arithmetics">Arithmetics</h3><pre><code class="python language-python apache hljs">&lt;bool&gt; = &lt;D/T/DTn&gt; &gt; &lt;D/T/DTn&gt; <span class="hljs-comment"># Ignores time jumps (fold attribute). Also ==.</span>
582582
&lt;bool&gt; = &lt;DTa&gt; &gt; &lt;DTa&gt; <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>
583-
&lt;TD&gt; = &lt;D/DTn&gt; - &lt;D/DTn&gt; <span class="hljs-comment"># Returns the difference. Ignores time jumps.</span>
583+
&lt;TD&gt; = &lt;D/DTn&gt; - &lt;D/DTn&gt; <span class="hljs-comment"># Ignores jumps. Convert to UTC for actual delta.</span>
584584
&lt;TD&gt; = &lt;DTa&gt; - &lt;DTa&gt; <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>
585585
&lt;D/DT&gt; = &lt;D/DT&gt; ± &lt;TD&gt; <span class="hljs-comment"># Returned datetime can fall into missing hour.</span>
586586
&lt;TD&gt; = &lt;TD&gt; * &lt;int/float&gt; <span class="hljs-comment"># Also: &lt;TD&gt; = abs(&lt;TD&gt;) and &lt;TD&gt; = &lt;TD&gt; ±% &lt;TD&gt;.</span>
@@ -1774,7 +1774,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17741774
<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>
17751775
&lt;obj&gt; = &lt;Future&gt;.result(timeout=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
17761776
&lt;bool&gt; = &lt;Future&gt;.cancel() <span class="hljs-comment"># Cancels or returns False if running/finished.</span>
1777-
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Each Future is yielded as it completes.</span>
1777+
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Next() waits for next completed Future.</span>
17781778
</code></pre>
17791779
<ul>
17801780
<li><strong>Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.</strong></li>
@@ -1785,6 +1785,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17851785
&lt;bool&gt; = op.not_(&lt;obj&gt;) <span class="hljs-comment"># or, and, not (or/and missing)</span>
17861786
&lt;bool&gt; = op.eq/ne/lt/le/gt/ge/contains/is_(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># ==, !=, &lt;, &lt;=, &gt;, &gt;=, in, is</span>
17871787
&lt;obj&gt; = op.or_/xor/and_(&lt;int/set&gt;, &lt;int/set&gt;) <span class="hljs-comment"># |, ^, &amp;</span>
1788+
&lt;int&gt; = op.lshift/rshift(&lt;int&gt;, &lt;int&gt;) <span class="hljs-comment"># &lt;&lt;, &gt;&gt;</span>
17881789
&lt;obj&gt; = op.add/sub/mul/truediv/floordiv/mod(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># +, -, *, /, //, %</span>
17891790
&lt;num&gt; = op.neg/invert(&lt;num&gt;) <span class="hljs-comment"># -, ~</span>
17901791
&lt;num&gt; = op.pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># **</span>
@@ -1796,11 +1797,10 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17961797
sorted_by_second = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hljs-number">1</span>))
17971798
sorted_by_both = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
17981799
product_of_elems = functools.reduce(op.mul, &lt;collection&gt;)
1799-
union_of_sets = functools.reduce(op.or_, &lt;coll_of_sets&gt;)
18001800
first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span class="hljs-number">0</span>)(&lt;list&gt;)
18011801
</code></pre>
18021802
<ul>
1803-
<li><strong>Bitwise operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
1803+
<li><strong>Bitwise operators require objects to have or(), xor(), and(), lshift(), rshift() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
18041804
<li><strong>Also: <code class="python hljs"><span class="hljs-string">'&lt;bool&gt; = &lt;bool&gt; &amp;|^ &lt;bool&gt;'</span></code> and <code class="python hljs"><span class="hljs-string">'&lt;int&gt; = &lt;bool&gt; &amp;|^ &lt;int&gt;'</span></code>.</strong></li>
18051805
</ul>
18061806
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Names of local variables, functions, classes, etc.</span>
@@ -2012,31 +2012,31 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
20122012
<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
20132013
</code></pre></div>
20142014

2015-
<pre><code class="python language-python hljs">logging.basicConfig(filename=&lt;path&gt;) <span class="hljs-comment"># Configures the root logger (see Setup).</span>
2016-
logging.debug/info/warning/error/critical(&lt;str&gt;) <span class="hljs-comment"># Logs to the root logger.</span>
2017-
&lt;Logger&gt; = logging.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span>
2018-
&lt;Logger&gt;.&lt;level&gt;(&lt;str&gt;) <span class="hljs-comment"># Logs to the logger.</span>
2019-
&lt;Logger&gt;.exception(&lt;str&gt;) <span class="hljs-comment"># Calls error() with caught exception.</span>
2015+
<pre><code class="python language-python hljs">logging.basicConfig(filename=&lt;path&gt;, level=<span class="hljs-string">'DEBUG'</span>) <span class="hljs-comment"># Configures the root logger (see Setup).</span>
2016+
logging.debug/info/warning/error/critical(&lt;str&gt;) <span class="hljs-comment"># Logs to the root logger.</span>
2017+
&lt;Logger&gt; = logging.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span>
2018+
&lt;Logger&gt;.&lt;level&gt;(&lt;str&gt;) <span class="hljs-comment"># Logs to the logger.</span>
2019+
&lt;Logger&gt;.exception(&lt;str&gt;) <span class="hljs-comment"># Calls error() with caught exception.</span>
20202020
</code></pre>
20212021
<div><h3 id="setup">Setup</h3><pre><code class="python language-python hljs">logging.basicConfig(
2022-
filename=<span class="hljs-keyword">None</span>, <span class="hljs-comment"># Logs to console (stderr) by default.</span>
2023-
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add `%(asctime)s` for local datetime.</span>
2024-
level=logging.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span>
2025-
handlers=[logging.StreamHandler()] <span class="hljs-comment"># Uses FileHandler if filename is set.</span>
2022+
filename=<span class="hljs-keyword">None</span>, <span class="hljs-comment"># Logs to console (stderr) by default.</span>
2023+
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add `%(asctime)s` for local datetime.</span>
2024+
level=logging.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span>
2025+
handlers=[logging.StreamHandler()] <span class="hljs-comment"># Uses FileHandler if filename is set.</span>
20262026
)
20272027
</code></pre></div>
20282028

2029-
<pre><code class="python language-python hljs">&lt;Formatter&gt; = logging.Formatter(<span class="hljs-string">'&lt;format&gt;'</span>) <span class="hljs-comment"># Creates a Formatter.</span>
2030-
&lt;Handler&gt; = logging.FileHandler(&lt;path&gt;) <span class="hljs-comment"># Creates a Handler.</span>
2031-
&lt;Handler&gt;.setFormatter(&lt;Formatter&gt;) <span class="hljs-comment"># Adds Formatter to the Handler.</span>
2032-
&lt;Handler&gt;.setLevel(&lt;int/str&gt;) <span class="hljs-comment"># Processes all messages by default.</span>
2033-
&lt;Logger&gt;.addHandler(&lt;Handler&gt;) <span class="hljs-comment"># Adds Handler to the Logger.</span>
2034-
&lt;Logger&gt;.setLevel(&lt;int/str&gt;) <span class="hljs-comment"># What is sent to its/ancestor's handlers.</span>
2029+
<pre><code class="python language-python hljs">&lt;Formatter&gt; = logging.Formatter(<span class="hljs-string">'&lt;format&gt;'</span>) <span class="hljs-comment"># Creates a Formatter.</span>
2030+
&lt;Handler&gt; = logging.FileHandler(&lt;path&gt;) <span class="hljs-comment"># Creates a Handler.</span>
2031+
&lt;Handler&gt;.setFormatter(&lt;Formatter&gt;) <span class="hljs-comment"># Adds Formatter to the Handler.</span>
2032+
&lt;Handler&gt;.setLevel(&lt;int/str&gt;) <span class="hljs-comment"># Processes all messages by default.</span>
2033+
&lt;Logger&gt;.addHandler(&lt;Handler&gt;) <span class="hljs-comment"># Adds Handler to the Logger.</span>
2034+
&lt;Logger&gt;.setLevel(&lt;int/str&gt;) <span class="hljs-comment"># What is sent to its/ancestor's handlers.</span>
20352035
</code></pre>
20362036
<ul>
20372037
<li><strong>Parent logger can be specified by naming the child logger <code class="python hljs"><span class="hljs-string">'&lt;parent&gt;.&lt;name&gt;'</span></code>.</strong></li>
20382038
<li><strong>If logger doesn't have a set level it inherits it from the first ancestor that does.</strong></li>
2039-
<li><strong>Formatter also supports: pathname, filename, funcName, lineno, thread and process.</strong></li>
2039+
<li><strong>Formatter also accepts: pathname, filename, funcName, lineno, thread and process.</strong></li>
20402040
<li><strong>A <code class="python hljs"><span class="hljs-string">'handlers.RotatingFileHandler'</span></code> creates and deletes log files based on 'maxBytes' and 'backupCount' arguments.</strong></li>
20412041
</ul>
20422042
<div><h4 id="createsaloggerthatwritesallmessagestofileandsendsthemtotherootshandlerthatprintswarningsorhigher">Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>logger = logging.getLogger(<span class="hljs-string">'my_module'</span>)
@@ -2929,7 +2929,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29292929

29302930

29312931
<footer>
2932-
<aside>September 29, 2023</aside>
2932+
<aside>October 4, 2023</aside>
29332933
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29342934
</footer>
29352935

0 commit comments

Comments
 (0)