From 594c8dec94509780d4bcd6e109fb06fbb7d15f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 18 Jun 2024 02:31:51 +0200 Subject: [PATCH 001/258] Image --- README.md | 14 +++++++------- index.html | 18 +++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8828f1d70..4c11b1e33 100644 --- a/README.md +++ b/README.md @@ -2770,8 +2770,8 @@ from PIL import Image ``` ```python - = Image.new('', (width, height)) # Also `color=`. - = Image.open() # Identifies format based on file contents. + = Image.new('', (width, height)) # Creates new image. Also `color=`. + = Image.open() # Identifies format based on file's contents. = .convert('') # Converts image to the new mode. .save() # Selects format based on the path extension. .show() # Opens image in the default preview app. @@ -2782,7 +2782,7 @@ from PIL import Image .putpixel((x, y), ) # Updates pixel's value. = .getdata() # Returns a flattened view of pixel values. .putdata() # Updates pixels with a copy of the sequence. -.paste(, (x, y)) # Draws passed image at specified location. +.paste(, (x, y)) # Draws passed image at the specified location. ``` ```python @@ -2796,10 +2796,10 @@ from PIL import Image ``` ### Modes -* **`'L'` - 8-bit pixels, greyscale.** -* **`'RGB'` - 3x8-bit pixels, true color.** -* **`'RGBA'` - 4x8-bit pixels, true color with transparency mask.** -* **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.** +* **`'L'` - Lightness (i.e. greyscale). Each pixel is an int between 0 and 255.** +* **`'RGB'` - Red, green, blue (i.e. true color). Each pixel is a tuple of three ints.** +* **`'RGBA'` - RGB with alpha. Low alpha (forth int) means more transparency.** +* **`'HSV'` - Hue, saturation, value color space.** ### Examples #### Creates a PNG image of a rainbow gradient: diff --git a/index.html b/index.html index 0909a38d9..667cad66c 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -2262,8 +2262,8 @@

Format

from PIL import Image

-
<Image> = Image.new('<mode>', (width, height))  # Also `color=<int/tuple/str>`.
-<Image> = Image.open(<path>)                    # Identifies format based on file contents.
+
<Image> = Image.new('<mode>', (width, height))  # Creates new image. Also `color=<int/tuple>`.
+<Image> = Image.open(<path>)                    # Identifies format based on file's contents.
 <Image> = <Image>.convert('<mode>')             # Converts image to the new mode.
 <Image>.save(<path>)                            # Selects format based on the path extension.
 <Image>.show()                                  # Opens image in the default preview app.
@@ -2272,7 +2272,7 @@ 

Format

# Updates pixel's value. <ImagingCore> = <Image>.getdata() # Returns a flattened view of pixel values. <Image>.putdata(<list/ImagingCore>) # Updates pixels with a copy of the sequence. -<Image>.paste(<Image>, (x, y)) # Draws passed image at specified location. +<Image>.paste(<Image>, (x, y)) # Draws passed image at the specified location.

<Image> = <Image>.filter(<Filter>)              # `<Filter> = ImageFilter.<name>(<args>)`
 <Image> = <Enhance>.enhance(<float>)            # `<Enhance> = ImageEnhance.<name>(<Image>)`
@@ -2281,10 +2281,10 @@ 

Format

# Use `<array>.clip(0, 255)` to clip values.

Modes

    -
  • 'L' - 8-bit pixels, greyscale.
  • -
  • 'RGB' - 3x8-bit pixels, true color.
  • -
  • 'RGBA' - 4x8-bit pixels, true color with transparency mask.
  • -
  • 'HSV' - 3x8-bit pixels, Hue, Saturation, Value color space.
  • +
  • 'L' - Lightness (i.e. greyscale). Each pixel is an int between 0 and 255.
  • +
  • 'RGB' - Red, green, blue (i.e. true color). Each pixel is a tuple of three ints.
  • +
  • 'RGBA' - RGB with alpha. Low alpha (forth int) means more transparency.
  • +
  • 'HSV' - Hue, saturation, value color space.

Examples

Creates a PNG image of a rainbow gradient:

WIDTH, HEIGHT = 100, 100
 n_pixels = WIDTH * HEIGHT
 hues = (255 * i/n_pixels for i in range(n_pixels))
@@ -2932,7 +2932,7 @@ 

Format

-

#Threading

CPython interpreter can only run a single thread at a time. Using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.

from threading import Thread, RLock, Semaphore, Event, Barrier
+

#Threading

CPython interpreter can only run a single thread at a time. Using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.

from threading import Thread, Lock, RLock, Semaphore, Event, Barrier
 from concurrent.futures import ThreadPoolExecutor, as_completed
 

Thread

<Thread> = Thread(target=<function>)           # Use `args=<collection>` to set the arguments.
-<Thread>.start()                               # Starts the thread.
-<bool> = <Thread>.is_alive()                   # Checks if the thread has finished executing.
+<Thread>.start()                               # Starts the thread. Also <Thread>.is_alive().
 <Thread>.join()                                # Waits for the thread to finish.
 
@@ -1738,7 +1737,7 @@

Format

'kwargs=<dict>' to pass keyword arguments to the function.
  • Use '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 acquirer.
    +

    Lock

    <lock> = Lock/RLock()                          # RLock can only be released by acquirer.
     <lock>.acquire()                               # Waits for the lock to be available.
     <lock>.release()                               # Makes the lock available again.
     
    @@ -1773,23 +1772,23 @@

    Format

    pickable. Queues must be sent using executor's 'initargs' and 'initializer' parameters. +
  • ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be pickable, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor needs to be reachable via 'if __name__ == "__main__": ...'.
  • #Operator

    Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.

    import operator as op
    -<bool> = op.not_(<obj>)                                         # or, and, not (or/and missing)
    -<bool> = op.eq/ne/lt/le/gt/ge/is_/contains(<obj>, <obj>)        # ==, !=, <, <=, >, >=, is, in
    -<obj>  = op.or_/xor/and_(<int/set>, <int/set>)                  # |, ^, &
    -<int>  = op.lshift/rshift(<int>, <int>)                         # <<, >>
    -<obj>  = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>)      # +, -, *, /, //, %
    -<num>  = op.neg/invert(<num>)                                   # -, ~
    -<num>  = op.pow(<num>, <num>)                                   # **
    -<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...])   # [index/key], .name, .name()
    +<bool> = op.not_(<obj>)                                        # or, and, not (or/and missing)
    +<bool> = op.eq/ne/lt/le/gt/ge/is_/contains(<obj>, <obj>)       # ==, !=, <, <=, >, >=, is, in
    +<obj>  = op.or_/xor/and_(<int/set>, <int/set>)                 # |, ^, &
    +<int>  = op.lshift/rshift(<int>, <int>)                        # <<, >>
    +<obj>  = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>)     # +, -, *, /, //, %
    +<num>  = op.neg/invert(<num>)                                  # -, ~
    +<num>  = op.pow(<num>, <num>)                                  # **
    +<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...])  # [index/key], .name, .name()
     
    elementwise_sum  = map(op.add, list_a, list_b)
    -sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
    -sorted_by_both   = sorted(<collection>, key=op.itemgetter(1, 0))
    +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>)
     first_element    = op.methodcaller('pop', 0)(<list>)
     
    @@ -2265,7 +2264,7 @@

    Format

    <Image> = Image.new('<mode>', (width, height)) # Creates new image. Also `color=<int/tuple>`. <Image> = Image.open(<path>) # Identifies format based on file's contents. <Image> = <Image>.convert('<mode>') # Converts image to the new mode. -<Image>.save(<path>) # Selects format based on the path extension. +<Image>.save(<path>) # Selects format based on extension (png/jpg…). <Image>.show() # Opens image in the default preview app.

    <int/tuple> = <Image>.getpixel((x, y))          # Returns pixel's value (its color).
    diff --git a/pdf/remove_links.py b/pdf/remove_links.py
    index 937990eb3..72dcc503e 100755
    --- a/pdf/remove_links.py
    +++ b/pdf/remove_links.py
    @@ -24,8 +24,7 @@
         'To print the spreadsheet to the console use Tabulate library.': 'To print the spreadsheet to the console use Tabulate library (p. 34).',
         'For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library.': 'For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library (p. 46).',
         'Bools will be stored and returned as ints and dates as ISO formatted strings.': 'Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).',
    -    'An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be pickable.': 'An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be pickable.',
    -    'ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be pickable. Queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters.': 'ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be pickable. Queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters.',
    +    'ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be pickable, queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters, and executor needs to be reachable via \'if __name__ == "__main__": ...\'.': 'ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be pickable, queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters, and executor needs to be reachable via \'if __name__ == "__main__": ...\'.',
         'Asyncio module also provides its own Queue, Event, Lock and Semaphore classes.': 'Asyncio module also provides its own Queue, Event, Lock and Semaphore classes (p. 30).',
         'Install a WSGI server like Waitress and a HTTP server such as Nginx for better security.': 'Install a WSGI server like Waitress and a HTTP server such as Nginx for better security.',
         'The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.': 'The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.',
    
    From 68f4b1f34650d7ad1d6d17be3bc15c313ed81264 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Jure=20=C5=A0orn?= 
    Date: Wed, 19 Jun 2024 14:38:56 +0200
    Subject: [PATCH 003/258] Enum
    
    ---
     README.md  | 4 ++--
     index.html | 8 ++++----
     2 files changed, 6 insertions(+), 6 deletions(-)
    
    diff --git a/README.md b/README.md
    index d5c640dda..f1dbbaa1f 100644
    --- a/README.md
    +++ b/README.md
    @@ -1338,13 +1338,13 @@ from enum import Enum, auto
     class (Enum):
          = auto()              # Increment of the last numeric value or 1.
          =              # Values don't have to be hashable.
    -     = ,     # Tuple can be used for multiple values.
    +     = ,     # Values can be collections (like this tuple).
     ```
     * **Methods receive the member they were called on as the 'self' argument.**
     * **Accessing a member named after a reserved keyword causes SyntaxError.**
     
     ```python
    - = .         # Returns a member.
    + = .         # Returns a member. Raises AttributeError.
      = ['']      # Returns a member. Raises KeyError.
      = ()              # Returns a member. Raises ValueError.
         = .name                # Returns member's name.
    diff --git a/index.html b/index.html
    index f8f434ef9..85351f7ad 100644
    --- a/index.html
    +++ b/index.html
    @@ -54,7 +54,7 @@
     
     
       
    - +
    @@ -1142,13 +1142,13 @@
    class <enum_name>(Enum):
         <member_name> = auto()              # Increment of the last numeric value or 1.
         <member_name> = <value>             # Values don't have to be hashable.
    -    <member_name> = <value>, <value>    # Tuple can be used for multiple values.
    +    <member_name> = <value>, <value>    # Values can be collections (like this tuple).
     
    • Methods receive the member they were called on as the 'self' argument.
    • Accessing a member named after a reserved keyword causes SyntaxError.
    -
    <member> = <enum>.<member_name>         # Returns a member.
    +
    <member> = <enum>.<member_name>         # Returns a member. Raises AttributeError.
     <member> = <enum>['<member_name>']      # Returns a member. Raises KeyError.
     <member> = <enum>(<value>)              # Returns a member. Raises ValueError.
     <str>    = <member>.name                # Returns member's name.
    @@ -2931,7 +2931,7 @@ 

    Format