From a658334dfe5f5d49c6e8964577676f04972c59a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 19 May 2021 23:39:52 +0200 Subject: [PATCH] Regex, Format, Struct, Threading --- README.md | 30 ++++++++++++++++-------------- index.html | 38 ++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index b30c923..a88494f 100644 --- a/README.md +++ b/README.md @@ -377,7 +377,7 @@ import re ### Special Sequences * **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.** -* **As shown below, it restricts special sequence matches to `'[\x00-\x7f]'` and prevents `'\s'` from accepting `'[\x1c\x1d\x1e\x1f]'`.** +* **As shown below, it restricts special sequence matches to the first 128 characters and prevents `'\s'` from accepting `'[\x1c\x1d\x1e\x1f]'`.** * **Use a capital letter for negation.** ```python '\d' == '[0-9]' # Matches decimal characters. @@ -412,9 +412,10 @@ Format {:.<10} # '......' {:0} # '' ``` +* **Use `'{:{}[...]}'` to set options dynamically.** +* **Adding `'!r'` before the colon converts object to string by calling its [repr()](#class) method.** ### Strings -**`'!r'` calls object's [repr()](#class) method, instead of [str()](#class), to get a string.** ```python {'abcde'!r:10} # "'abcde' " {'abcde':10.3} # 'abc ' @@ -1286,7 +1287,9 @@ Enum ---- ```python from enum import Enum, auto +``` +```python class (Enum): = = , @@ -1857,7 +1860,7 @@ import sqlite3 #### Or: ```python -with : # Exits block with commit() or rollback(), +with : # Exits the block with commit() or rollback(), .execute('') # depending on whether an exception occurred. ``` @@ -1940,7 +1943,7 @@ def write_bytes(filename, bytes_obj): Struct ------ * **Module that performs conversions between a sequence of numbers and a bytes object.** -* **System’s native type sizes and byte order are used by default.** +* **System’s type sizes and byte order are used by default.** ```python from struct import pack, unpack, iter_unpack @@ -1962,7 +1965,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03' ### Format #### For standard type sizes start format string with: -* **`'='` - native byte order (usually little-endian)** +* **`'='` - system's byte order (usually little-endian)** * **`'<'` - little-endian** * **`'>'` - big-endian (also `'!'`)** @@ -1998,7 +2001,7 @@ Memory View * **A sequence object that points to the memory of another object.** * **Each element can reference a single or multiple consecutive bytes, depending on format.** * **Order and number of elements can be changed with slicing.** -* **Casting only works between char and other types and always uses native size and b. order.** +* **Casting only works between char and other types and uses system's sizes and byte order.** ```python = memoryview() # Immutable if bytes, else mutable. @@ -2052,10 +2055,10 @@ from concurrent.futures import ThreadPoolExecutor ### Thread ```python - = Thread(target=) # Use `args=` to set arguments. + = Thread(target=) # Use `args=` to set the arguments. .start() # Starts the thread. - = .is_alive() # Checks if thread has finished executing. -.join() # Waits for thread to finish. + = .is_alive() # Checks if the thread has finished executing. +.join() # Waits for the thread to finish. ``` * **Use `'kwargs='` to pass keyword arguments to the function.** * **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.** @@ -2063,15 +2066,14 @@ from concurrent.futures import ThreadPoolExecutor ### Lock ```python = RLock() # Lock that can only be released by the owner. -.acquire() # Waits for lock to be available. -.release() # Makes lock available again. +.acquire() # Waits for the lock to be available. +.release() # Makes the lock available again. ``` #### Or: ```python -lock = RLock() -with lock: - ... +with : # Enters the block by calling acquire(), + ... # and exits it with release(). ``` ### Semaphore, Event, Barrier diff --git a/index.html b/index.html index d8f6b6d..4833a5e 100644 --- a/index.html +++ b/index.html @@ -508,7 +508,7 @@ to_exclusive = <range>.stop

Special Sequences

  • By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless 'flags=re.ASCII' argument is used.
  • -
  • As shown below, it restricts special sequence matches to '[\x00-\x7f]' and prevents '\s' from accepting '[\x1c\x1d\x1e\x1f]'.
  • +
  • As shown below, it restricts special sequence matches to the first 128 characters and prevents '\s' from accepting '[\x1c\x1d\x1e\x1f]'.
  • Use a capital letter for negation.
'\d' == '[0-9]'                                # Matches decimal characters.
 '\w' == '[a-zA-Z0-9_]'                         # Matches alphanumerics and underscore.
@@ -536,12 +536,15 @@ to_exclusive   = <range>.stop
 {<el>:0}                                       # '<el>'
 
-

Strings

'!r' calls object's repr() method, instead of str(), to get a string.

{'abcde'!r:10}                                 # "'abcde'   "
+
    +
  • Use '{<el>:{<str/int/float>}[...]}' to set options dynamically.
  • +
  • Adding '!r' before the colon converts object to string by calling its repr() method.
  • +
+

Strings

{'abcde'!r:10}                                 # "'abcde'   "
 {'abcde':10.3}                                 # 'abc       '
 {'abcde':.3}                                   # 'abc'
 
-

Numbers

{ 123456:10,}                                  # '   123,456'
 { 123456:10_}                                  # '   123_456'
 { 123456:+10}                                  # '   +123456'
@@ -1252,13 +1255,13 @@ Hello World!
 
  • Names of their required methods are stored in '<abc>.__abstractmethods__'.
  • #Enum

    from enum import Enum, auto
    +
    -class <enum_name>(Enum): +
    class <enum_name>(Enum):
         <member_name_1> = <value_1>
         <member_name_2> = <value_2_a>, <value_2_b>
         <member_name_3> = auto()
    -
    - +
    • If there are no numeric values before auto(), it returns 1.
    • Otherwise it returns an increment of the last numeric value.
    • @@ -1691,7 +1694,7 @@ CompletedProcess(args=['bc', # Discards all changes since the last commit.
    -

    Or:

    with <conn>:                                    # Exits block with commit() or rollback(),
    +

    Or:

    with <conn>:                                    # Exits the block with commit() or rollback(),
         <conn>.execute('<query>')                   # depending on whether an exception occurred.
     
    @@ -1754,7 +1757,7 @@ CompletedProcess(args=['bc', #Struct
    • Module that performs conversions between a sequence of numbers and a bytes object.
    • -
    • System’s native type sizes and byte order are used by default.
    • +
    • System’s type sizes and byte order are used by default.
    from struct import pack, unpack, iter_unpack
     
    @@ -1770,7 +1773,7 @@ CompletedProcess(args=['bc', Format

    For standard type sizes start format string with:

      -
    • '=' - native byte order (usually little-endian)
    • +
    • '=' - system's byte order (usually little-endian)
    • '<' - little-endian
    • '>' - big-endian (also '!')

    Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:

      @@ -1803,7 +1806,7 @@ CompletedProcess(args=['bc', <mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable. <real> = <mview>[<index>] # Returns an int or a float. <mview> = <mview>[<slice>] # Mview with rearranged elements. @@ -1841,10 +1844,10 @@ CompletedProcess(args=['bc', Thread
      <Thread> = Thread(target=<function>)           # Use `args=<collection>` to set arguments.
      +

      Thread

      <Thread> = Thread(target=<function>)           # Use `args=<collection>` to set the arguments.
       <Thread>.start()                               # Starts the thread.
      -<bool> = <Thread>.is_alive()                   # Checks if thread has finished executing.
      -<Thread>.join()                                # Waits for thread to finish.
      +<bool> = <Thread>.is_alive()                   # Checks if the thread has finished executing.
      +<Thread>.join()                                # Waits for the thread to finish.
       
        @@ -1852,13 +1855,12 @@ CompletedProcess(args=['bc', 'daemon=True', or the program will not be able to exit while the thread is alive.

      Lock

      <lock> = RLock()                               # Lock that can only be released by the owner.
      -<lock>.acquire()                               # Waits for lock to be available.
      -<lock>.release()                               # Makes lock available again.
      +<lock>.acquire()                               # Waits for the lock to be available.
      +<lock>.release()                               # Makes the lock available again.
       
      -

      Or:

      lock = RLock()
      -with lock:
      -    ...
      +

      Or:

      with <lock>:                                   # Enters the block by calling acquire(),
      +    ...                                        # and exits it with release().
       

      Semaphore, Event, Barrier

      <Semaphore> = Semaphore(value=1)               # Lock that can be acquired by 'value' threads.