From ed30a30b7a801e3d364e56e0e86adbd5d4f52abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= 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() + = max() # Returns largest element. Also max(, ...). + = sum() # Returns sum of elements. Also math.prod(). +``` + +```python elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)] sorted_by_second = sorted(, key=lambda el: el[1]) sorted_by_both = sorted(, key=lambda el: (el[1], el[0])) flatter_list = list(itertools.chain.from_iterable()) -product_of_elems = functools.reduce(lambda out, el: out * el, ) -list_of_chars = list() ``` -* **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
= datetime(year, month, day, hour=0) # Also: `minute=0, second=0, microsecond=0, …`. = timedelta(weeks=0, days=0, hours=0) # Also: `minutes=0, seconds=0, microseconds=0`. ``` -* **Aware `` time and datetime objects have defined timezone, while naive `` 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 `'.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(, key=op.itemgetter(1)) -sorted_by_both = sorted(, key=op.itemgetter(1, 0)) -product_of_elems = functools.reduce(op.mul, ) +sorted_by_second = sorted(, key=op.itemgetter(1)) +sorted_by_both = sorted(, key=op.itemgetter(1, 0)) first_element = op.methodcaller('pop', 0)() ``` * **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 @@
- +
@@ -114,16 +114,16 @@ <list> = sorted(<collection>) # Returns new list with sorted elements. <iter> = reversed(<list>) # Returns reversed iterator of elements. -
sum_of_elements  = sum(<collection>)
-elementwise_sum  = [sum(pair) for pair in zip(list_a, list_b)]
+
<el>  = max(<collection>)       # Returns largest element. Also max(<el_1>, ...).
+<num> = sum(<collection>)       # Returns sum of elements. Also math.prod(<coll>).
+
+
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.
  • +
  • For details about sort(), sorted(), max() and min() see sortable.
  • Module operator provides functions itemgetter() and mul() that offer the same functionality as lambda expressions above.
<int> = len(<list>)             # Returns number of items. Also works on other collections.
@@ -527,7 +527,7 @@ Point(x=1, y=2
 <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.
  • @@ -1789,9 +1789,8 @@ CompletedProcess(args=['bc', # [index/key], .name, .name([…])
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>)
 
    @@ -2924,7 +2923,7 @@ $ deactivate # Deactivates the active
    - +
    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 = { - 'For details about sorted(), min() and max() see sortable.': 'For details about sorted(), min() and max() see sortable (p. 16).', + 'For details about sort(), sorted(), max() and min() see sortable.': 'For details about sort(), sorted(), max() and min() see sortable (p. 16).', 'Module operator provides functions itemgetter() and mul() that offer the same functionality as lambda expressions above.': 'Module \'operator\' (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions (p. 11) above.', 'Adding \'!r\' to the expression converts object to string by calling its repr() method.': 'Adding \'!r\' to the expression converts object to string by calling its repr() method.', 'It can be any callable, but is usually implemented as a function that returns a closure.': 'It can be any callable, but is usually implemented as a function that returns a closure.',