From 031875e7c14ee0e4b42f0d906ccb57e925980167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 2 Apr 2021 06:31:10 +0200 Subject: [PATCH] Inline, Struct, Threading --- README.md | 60 ++++++++++++++++++------------------------ index.html | 76 +++++++++++++++++++++++++----------------------------- parse.js | 3 ++- 3 files changed, 62 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 55692d0..f2c320a 100644 --- a/README.md +++ b/README.md @@ -599,7 +599,7 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary ``` * **Use `'.weekday()'` to get the day of the week (Mon == 0).** * **`'fold=1'` means the second pass in case of time jumping back for one hour.** -* **`' = resolve_imaginary()'` fixes DTs that fall into missing hour.** +* **`' = resolve_imaginary()'` fixes DTs that fall into the missing hour.** ### Now ```python @@ -731,10 +731,10 @@ def f(x, *args, z, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3 ### Other Uses ```python - = [* [, ...]] - = {* [, ...]} - = (*, [...]) - = {** [, ...]} + = [* [, ...]] + = {* [, ...]} + = (*, [...]) + = {** [, ...]} ``` ```python @@ -746,48 +746,40 @@ Inline ------ ### Lambda ```python - = lambda: - = lambda , : + = lambda: + = lambda , : ``` ### Comprehensions ```python - = [i+1 for i in range(10)] # [1, 2, ..., 10] - = {i for i in range(10) if i > 5} # {6, 7, 8, 9} - = (i+5 for i in range(10)) # (5, 6, ..., 14) - = {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18} + = [i+1 for i in range(10)] # [1, 2, ..., 10] + = {i for i in range(10) if i > 5} # {6, 7, 8, 9} + = (i+5 for i in range(10)) # (5, 6, ..., 14) + = {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18} ``` ```python -out = [i+j for i in range(10) for j in range(10)] -``` - -#### Is the same as: -```python -out = [] -for i in range(10): - for j in range(10): - out.append(i+j) +>>> [l+r for l in 'abc' for r in 'abc'] +['aa', 'ab', 'ac', ..., 'cc'] ``` ### Map, Filter, Reduce ```python -from functools import reduce - - = map(lambda x: x + 1, range(10)) # (1, 2, ..., 10) - = filter(lambda x: x > 5, range(10)) # (6, 7, 8, 9) - = reduce(lambda out, x: out + x, range(10)) # 45 + = map(lambda x: x + 1, range(10)) # (1, 2, ..., 10) + = filter(lambda x: x > 5, range(10)) # (6, 7, 8, 9) + = reduce(lambda out, x: out + x, range(10)) # 45 ``` +* **Reduce must be imported from functools module.** ### Any, All ```python - = any() # False if empty. - = all(el[1] for el in ) # True if empty. + = any() # False if empty. + = all(el[1] for el in ) # True if empty. ``` -### If - Else +### Conditional Expression ```python - = if else + = if else ``` ```python @@ -810,7 +802,7 @@ direction = Direction.n ```python from dataclasses import make_dataclass -Creature = make_dataclass('Creature', ['location', 'direction']) +Creature = make_dataclass('Creature', ['loc', 'dir']) creature = Creature(Point(0, 0), Direction.n) ``` @@ -1950,13 +1942,14 @@ Struct ```python from struct import pack, unpack, iter_unpack +``` +```python = pack('', [, , ...]) = unpack('', ) = iter_unpack('', ) ``` -### Example ```python >>> pack('>hhl', 1, 2, 3) b'\x00\x01\x00\x02\x00\x00\x00\x03' @@ -2049,6 +2042,7 @@ Threading * **That is why using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.** ```python from threading import Thread, RLock, Semaphore, Event, Barrier +from concurrent.futures import ThreadPoolExecutor ``` ### Thread @@ -2084,10 +2078,6 @@ with lock: ### Thread Pool Executor **Object that manages thread execution.** -```python -from concurrent.futures import ThreadPoolExecutor -``` - ```python = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as : …` .shutdown(wait=True) # Blocks until all threads finish executing. diff --git a/index.html b/index.html index 5ec5d95..782ecd9 100644 --- a/index.html +++ b/index.html @@ -681,7 +681,7 @@ to_exclusive = <range>.stop
  • Use '<D/DT>.weekday()' to get the day of the week (Mon == 0).
  • 'fold=1' means the second pass in case of time jumping back for one hour.
  • -
  • '<DTa> = resolve_imaginary(<DTa>)' fixes DTs that fall into missing hour.
  • +
  • '<DTa> = resolve_imaginary(<DTa>)' fixes DTs that fall into the missing hour.

Now

<D/DTn>  = D/DT.today()                     # Current local date or naive datetime.
 <DTn>    = DT.utcnow()                      # Naive datetime from current UTC time.
@@ -781,45 +781,41 @@ func(*args, **kwargs)
 def f(*args, y, **kwargs):     # f(x=1, y=2, z=3) | f(1, y=2, z=3)
 def f(x, *args, z, **kwargs):  # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)
 
-

Other Uses

<list>  = [*<collection> [, ...]]
-<set>   = {*<collection> [, ...]}
-<tuple> = (*<collection>, [...])
-<dict>  = {**<dict> [, ...]}
+

Other Uses

<list> = [*<collection> [, ...]]
+<set>  = {*<collection> [, ...]}
+<tup.> = (*<collection>, [...])
+<dict> = {**<dict> [, ...]}
 
head, *body, tail = <collection>
 
-

#Inline

Lambda

<function> = lambda: <return_value>
-<function> = lambda <argument_1>, <argument_2>: <return_value>
+

#Inline

Lambda

<func> = lambda: <return_value>
+<func> = lambda <arg_1>, <arg_2>: <return_value>
 
-

Comprehensions

<list> = [i+1 for i in range(10)]                   # [1, 2, ..., 10]
-<set>  = {i for i in range(10) if i > 5}            # {6, 7, 8, 9}
-<iter> = (i+5 for i in range(10))                   # (5, 6, ..., 14)
-<dict> = {i: i*2 for i in range(10)}                # {0: 0, 1: 2, ..., 9: 18}
+

Comprehensions

<list> = [i+1 for i in range(10)]                         # [1, 2, ..., 10]
+<set>  = {i for i in range(10) if i > 5}                  # {6, 7, 8, 9}
+<iter> = (i+5 for i in range(10))                         # (5, 6, ..., 14)
+<dict> = {i: i*2 for i in range(10)}                      # {0: 0, 1: 2, ..., 9: 18}
 
-
out = [i+j for i in range(10) for j in range(10)]
+
>>> [l+r for l in 'abc' for r in 'abc']
+['aa', 'ab', 'ac', ..., 'cc']
 
-

Is the same as:

out = []
-for i in range(10):
-    for j in range(10):
-        out.append(i+j)
+

Map, Filter, Reduce

<iter> = map(lambda x: x + 1, range(10))                  # (1, 2, ..., 10)
+<iter> = filter(lambda x: x > 5, range(10))               # (6, 7, 8, 9)
+<obj>  = reduce(lambda out, x: out + x, range(10))        # 45
 
-

Map, Filter, Reduce

from functools import reduce
-
-<iter> = map(lambda x: x + 1, range(10))            # (1, 2, ..., 10)
-<iter> = filter(lambda x: x > 5, range(10))         # (6, 7, 8, 9)
-<obj>  = reduce(lambda out, x: out + x, range(10))  # 45
-
- -

Any, All

<bool> = any(<collection>)                          # False if empty.
-<bool> = all(el[1] for el in <collection>)          # True if empty.
+
    +
  • Reduce must be imported from functools module.
  • +
+

Any, All

<bool> = any(<collection>)                                # False if empty.
+<bool> = all(el[1] for el in <collection>)                # True if empty.
 
-

If - Else

<obj> = <expression_if_true> if <condition> else <expression_if_false>
+

Conditional Expression

<obj> = <exp_if_true> if <condition> else <exp_if_false>
 
>>> [a if a else 'zero' for a in (0, 1, 2, 3)]
@@ -835,7 +831,7 @@ Direction = Enum('Direction', from dataclasses import make_dataclass
-Creature  = make_dataclass('Creature', ['location', 'direction'])
+Creature  = make_dataclass('Creature', ['loc', 'dir'])
 creature  = Creature(Point(0, 0), Direction.n)
 

#Closure

We have a closure in Python when:

    @@ -884,7 +880,7 @@ creature = Creature(Point(0, >>> counter(), counter(), counter() (1, 2, 3)
-

#Decorator

A decorator takes a function, adds some functionality and returns it.

@decorator_name
+

#Decorator

A decorator takes a function, adds some functionality and returns it.

@decorator_name
 def function_that_gets_passed_to_decorator():
     ...
 
@@ -1397,7 +1393,7 @@ sys.exit(<int>) # Exits with
-

#Print

print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
+

#Print

print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
 
    @@ -1756,19 +1752,18 @@ CompletedProcess(args=['bc', from struct import pack, unpack, iter_unpack - -<bytes> = pack('<format>', <num_1> [, <num_2>, ...]) -<tuple> = unpack('<format>', <bytes>) -<tuples> = iter_unpack('<format>', <bytes>)
-

Example

>>> pack('>hhl', 1, 2, 3)
+
<bytes>  = pack('<format>', <num_1> [, <num_2>, ...])
+<tuple>  = unpack('<format>', <bytes>)
+<tuples> = iter_unpack('<format>', <bytes>)
+
+
>>> pack('>hhl', 1, 2, 3)
 b'\x00\x01\x00\x02\x00\x00\x00\x03'
 >>> unpack('>hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
 (1, 2, 3)
-
- +

Format

For standard type sizes start format string with:

  • '=' - native byte order (usually little-endian)
  • '<' - little-endian
  • @@ -1835,6 +1830,7 @@ CompletedProcess(args=['bc', from threading import Thread, RLock, Semaphore, Event, Barrier +from concurrent.futures import ThreadPoolExecutor
@@ -1863,13 +1859,11 @@ CompletedProcess(args=['bc', # Wait() blocks until it's called n_times.
-

Thread Pool Executor

Object that manages thread execution.

from concurrent.futures import ThreadPoolExecutor
+

Thread Pool Executor

Object that manages thread execution.

<Exec> = ThreadPoolExecutor(max_workers=None)  # Or: `with ThreadPoolExecutor() as <name>: …`
+<Exec>.shutdown(wait=True)                     # Blocks until all threads finish executing.
 
-
<Exec> = ThreadPoolExecutor(max_workers=None)  # Or: `with ThreadPoolExecutor() as <name>: …`
-<Exec>.shutdown(wait=True)                     # Blocks until all threads finish executing.
-
<iter> = <Exec>.map(<func>, <args_1>, ...)     # A multithreaded and non-lazy map().
 <Futr> = <Exec>.submit(<func>, <arg_1>, ...)   # Starts a thread and returns its Future object.
 <bool> = <Futr>.done()                         # Checks if the thread has finished executing.
@@ -2303,7 +2297,7 @@ right = [[0.1 ,  0.6<
 right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]]  # Shape: (3, 3) <- !
 
-

3. If neither non-matching dimension has size 1, raise an error.

Example

For each point returns index of its nearest point ([0.1, 0.6, 0.8] => [1, 2, 1]):

>>> points = np.array([0.1, 0.6, 0.8])
+

3. If neither non-matching dimension has size 1, raise an error.

Example

For each point returns index of its nearest point ([0.1, 0.6, 0.8] => [1, 2, 1]):

>>> points = np.array([0.1, 0.6, 0.8])
  [ 0.1,  0.6,  0.8]
 >>> wrapped_points = points.reshape(3, 1)
 [[ 0.1],
diff --git a/parse.js b/parse.js
index 2779c07..0081b67 100755
--- a/parse.js
+++ b/parse.js
@@ -552,7 +552,8 @@ function move(anchor_el, el_id) {
 }
 
 function insertPageBreaks() {
-  insertPageBreakBefore('#print')
+  insertPageBreakBefore('#decorator')
+  // insertPageBreakBefore('#print')
 }
 
 function insertPageBreakBefore(an_id) {