From ed30a30b7a801e3d364e56e0e86adbd5d4f52abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= <sornjure@gmail.com> Date: Thu, 14 Nov 2024 13:57:51 +0100 Subject: [PATCH] List, Datetime, Operator --- README.md | 18 +++++++++--------- index.html | 21 ++++++++++----------- pdf/remove_links.py | 2 +- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d61ef64..16b5295 100644 --- a/README.md +++ b/README.md @@ -50,15 +50,17 @@ List ``` ```python -sum_of_elements = sum(<collection>) +<el> = max(<collection>) # Returns largest element. Also max(<el_1>, ...). +<num> = sum(<collection>) # Returns sum of elements. Also math.prod(<coll>). +``` + +```python elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)] sorted_by_second = sorted(<collection>, key=lambda el: el[1]) sorted_by_both = sorted(<collection>, key=lambda el: (el[1], el[0])) flatter_list = list(itertools.chain.from_iterable(<list>)) -product_of_elems = functools.reduce(lambda out, el: out * el, <collection>) -list_of_chars = list(<str>) ``` -* **For details about sorted(), min() and max() see [sortable](#sortable).** +* **For details about sort(), sorted(), max() and min() see [sortable](#sortable).** * **Module [operator](#operator) provides functions itemgetter() and mul() that offer the same functionality as [lambda](#lambda) expressions above.** ```python @@ -388,7 +390,6 @@ import re '\w' == '[a-zA-Z0-9_]' # Also [ª²³…]. Matches an alphanumeric or _. '\s' == '[ \t\n\r\f\v]' # Also [\x1c-\x1f…]. Matches a whitespace. ``` - * **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.** * **It restricts special sequence matches to `'[\x00-\x7f]'` (the first 128 characters) and also prevents `'\s'` from accepting `'[\x1c-\x1f]'` (file, table, row, and field separators).** * **Use a capital letter for negation (all non-ASCII characters will be matched when used in combination with ASCII flag).** @@ -605,7 +606,7 @@ import zoneinfo, dateutil.tz <DT> = datetime(year, month, day, hour=0) # Also: `minute=0, second=0, microsecond=0, …`. <TD> = timedelta(weeks=0, days=0, hours=0) # Also: `minutes=0, seconds=0, microseconds=0`. ``` -* **Aware `<a>` time and datetime objects have defined timezone, while naive `<n>` don't. If object is naive, it is presumed to be in the system's timezone!** +* **Aware times and datetimes have defined timezone, while naive don't. If object is naive, it is presumed to be in the system's timezone!** * **`'fold=1'` means the second pass in case of time jumping back for one hour.** * **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns `'[±D, ]H:MM:SS[.…]'` and total_seconds() a float of all seconds.** * **Use `'<D/DT>.weekday()'` to get the day of the week as an int, with Monday being 0.** @@ -2185,9 +2186,8 @@ import operator as op ```python elementwise_sum = map(op.add, list_a, list_b) -sorted_by_second = sorted(<coll.>, key=op.itemgetter(1)) -sorted_by_both = sorted(<coll.>, key=op.itemgetter(1, 0)) -product_of_elems = functools.reduce(op.mul, <collection>) +sorted_by_second = sorted(<coll>, key=op.itemgetter(1)) +sorted_by_both = sorted(<coll>, key=op.itemgetter(1, 0)) first_element = op.methodcaller('pop', 0)(<list>) ``` * **Most operators call the object's special method that is named after them (second object is passed as an argument), while logical operators call their own code that relies on bool().** diff --git a/index.html b/index.html index 21d0344..74c262c 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@ <body> <header> - <aside>November 13, 2024</aside> + <aside>November 14, 2024</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </header> @@ -114,16 +114,16 @@ <list> = sorted(<collection>) <span class="hljs-comment"># Returns new list with sorted elements.</span> <iter> = reversed(<list>) <span class="hljs-comment"># Returns reversed iterator of elements.</span> </code></pre> -<pre><code class="python language-python hljs">sum_of_elements = sum(<collection>) -elementwise_sum = [sum(pair) <span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> zip(list_a, list_b)] +<pre><code class="python language-python hljs"><el> = max(<collection>) <span class="hljs-comment"># Returns largest element. Also max(<el_1>, ...).</span> +<num> = sum(<collection>) <span class="hljs-comment"># Returns sum of elements. Also math.prod(<coll>).</span> +</code></pre> +<pre><code class="python language-python hljs">elementwise_sum = [sum(pair) <span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> zip(list_a, list_b)] sorted_by_second = sorted(<collection>, key=<span class="hljs-keyword">lambda</span> el: el[<span class="hljs-number">1</span>]) sorted_by_both = sorted(<collection>, key=<span class="hljs-keyword">lambda</span> el: (el[<span class="hljs-number">1</span>], el[<span class="hljs-number">0</span>])) flatter_list = list(itertools.chain.from_iterable(<list>)) -product_of_elems = functools.reduce(<span class="hljs-keyword">lambda</span> out, el: out * el, <collection>) -list_of_chars = list(<str>) </code></pre> <ul> -<li><strong>For details about sorted(), min() and max() see <a href="#sortable">sortable</a>.</strong></li> +<li><strong>For details about sort(), sorted(), max() and min() see <a href="#sortable">sortable</a>.</strong></li> <li><strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong></li> </ul> <pre><code class="python language-python hljs"><int> = len(<list>) <span class="hljs-comment"># Returns number of items. Also works on other collections.</span> @@ -527,7 +527,7 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span> <TD> = timedelta(weeks=<span class="hljs-number">0</span>, days=<span class="hljs-number">0</span>, hours=<span class="hljs-number">0</span>) <span class="hljs-comment"># Also: `minutes=0, seconds=0, microseconds=0`.</span> </code></pre> <ul> -<li><strong>Aware <code class="apache hljs"><span class="hljs-section"><a></span></code> time and datetime objects have defined timezone, while naive <code class="apache hljs"><span class="hljs-section"><n></span></code> don't. If object is naive, it is presumed to be in the system's timezone!</strong></li> +<li><strong>Aware times and datetimes have defined timezone, while naive don't. If object is naive, it is presumed to be in the system's timezone!</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li> <li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns <code class="python hljs"><span class="hljs-string">'[±D, ]H:MM:SS[.…]'</span></code> and total_seconds() a float of all seconds.</strong></li> <li><strong>Use <code class="python hljs"><span class="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li> @@ -1789,9 +1789,8 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs- <func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...]) <span class="hljs-comment"># [index/key], .name, .name([…])</span> </code></pre> <pre><code class="python language-python hljs">elementwise_sum = map(op.add, list_a, list_b) -sorted_by_second = sorted(<coll.>, key=op.itemgetter(<span class="hljs-number">1</span>)) -sorted_by_both = sorted(<coll.>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)) -product_of_elems = functools.reduce(op.mul, <collection>) +sorted_by_second = sorted(<coll>, key=op.itemgetter(<span class="hljs-number">1</span>)) +sorted_by_both = sorted(<coll>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)) first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span class="hljs-number">0</span>)(<list>) </code></pre> <ul> @@ -2924,7 +2923,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active <footer> - <aside>November 13, 2024</aside> + <aside>November 14, 2024</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </footer> diff --git a/pdf/remove_links.py b/pdf/remove_links.py index 527d8d5..747ffb3 100755 --- a/pdf/remove_links.py +++ b/pdf/remove_links.py @@ -7,7 +7,7 @@ from pathlib import Path MATCHES = { - '<strong>For details about sorted(), min() and max() see <a href="#sortable">sortable</a>.</strong>': '<strong>For details about sorted(), min() and max() see sortable (p. 16).</strong>', + '<strong>For details about sort(), sorted(), max() and min() see <a href="#sortable">sortable</a>.</strong>': '<strong>For details about sort(), sorted(), max() and min() see sortable (p. 16).</strong>', '<strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong>': '<strong>Module \'operator\' (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions (p. 11) above.</strong>', '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> to the expression converts object to string by calling its <a href="#class">repr()</a> method.</strong>': '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> to the expression converts object to string by calling its repr() method.</strong>', '<strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong>': '<strong>It can be any callable, but is usually implemented as a function that returns a closure.</strong>',