diff --git a/README.md b/README.md index 82173e8..38278b3 100644 --- a/README.md +++ b/README.md @@ -337,8 +337,8 @@ String ``` ```python -<str> = chr(<int>) # Converts int to Unicode character. -<int> = ord(<str>) # Converts Unicode character to int. +<str> = chr(<int>) # Converts passed integer to Unicode character. +<int> = ord(<str>) # Converts passed Unicode character to integer. ``` * **Use `'unicodedata.normalize("NFC", <str>)'` on strings like `'Motörhead'` before comparing them to other strings, because `'ö'` can be stored as one or two characters.** * **`'NFC'` converts such characters to a single character, while `'NFD'` converts them to two.** @@ -972,9 +972,8 @@ class MyClass: >>> obj.a, str(obj), repr(obj) (1, '1', 'MyClass(1)') ``` -* **Return value of str() should be readable and of repr() unambiguous.** -* **If only repr() is defined, it will also be used for str().** -* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first argument.** +* **Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand, for example, `'print(a)'` calls `'a.__str__()'` and `'a + b'` calls `'a.__add__(b)'`.** +* **Return value of str() special method should be readable and of repr() unambiguous. If only repr() is defined, it will also be used for str().** #### Expressions that call the str() method: ```python @@ -982,7 +981,6 @@ print(<obj>) f'{<obj>}' logging.warning(<obj>) csv.writer(<file>).writerow([<obj>]) -raise Exception(<obj>) ``` #### Expressions that call the repr() method: @@ -991,11 +989,10 @@ print/str/repr([<obj>]) print/str/repr({<obj>: <obj>}) f'{<obj>!r}' Z = make_dataclass('Z', ['a']); print/str/repr(Z(<obj>)) ->>> <obj> ``` ### Subclass -* **Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.** +* **Inheritance is a mechanism that enables a class to extend some other class (that is, subclass to extend its parent), and by doing so inherit all its methods and attributes.** * **Subclass can then add its own methods and attributes or override inherited ones by reusing their names.** ```python @@ -1694,7 +1691,7 @@ from pathlib import Path ``` ```python -<str> = str(<Path>) # Returns path as str. Also <Path>.as_uri(). +<str> = str(<Path>) # Returns path as string. Also <Path>.as_uri(). <file> = open(<Path>) # Also <Path>.read/write_text/bytes(<args>). ``` @@ -2765,7 +2762,7 @@ from PIL import Image <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 (see Modes). -<Image>.save(<path>) # Selects format based on extension (PNG/JPG…). +<Image>.save(<path>) # Accepts `quality=<int>` if extension is jpg. <Image>.show() # Displays image in default preview app. ``` @@ -2788,8 +2785,8 @@ from PIL import Image ``` ### Modes -* **`'L'` - Lightness (greyscale image). Each pixel is an int between 0 and 255.** -* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three ints.** +* **`'L'` - Lightness (greyscale image). Each pixel is an integer between 0 and 255.** +* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three integers.** * **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.** * **`'HSV'` - Hue, saturation, value. Three ints representing color in HSV color space.** @@ -2989,10 +2986,10 @@ pg.init() screen = pg.display.set_mode((500, 500)) rect = pg.Rect(240, 240, 20, 20) while not pg.event.get(pg.QUIT): - deltas = {pg.K_UP: (0, -1), pg.K_RIGHT: (1, 0), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0)} for event in pg.event.get(pg.KEYDOWN): - x, y = deltas.get(event.key, (0, 0)) - rect = rect.move((x*20, y*20)) + dx = (event.key == pg.K_RIGHT) - (event.key == pg.K_LEFT) + dy = (event.key == pg.K_DOWN) - (event.key == pg.K_UP) + rect = rect.move((dx * 20, dy * 20)) screen.fill(pg.Color('black')) pg.draw.rect(screen, pg.Color('white'), rect) pg.display.flip() @@ -3004,13 +3001,13 @@ pg.quit() ```python <Rect> = pg.Rect(x, y, width, height) # Creates Rect object. Truncates passed floats. <int> = <Rect>.x/y/centerx/centery/… # `top/right/bottom/left`. Allows assignments. -<tup.> = <Rect>.topleft/center/… # `topright/bottomright/bottomleft`. Same. -<Rect> = <Rect>.move((delta_x, delta_y)) # Use move_ip() to move in-place. +<tup.> = <Rect>.topleft/center/… # `topright/bottomright/bottomleft/size`. Same. +<Rect> = <Rect>.move((delta_x, delta_y)) # Use move_ip() to move the rectangle in-place. ``` ```python -<bool> = <Rect>.collidepoint((x, y)) # Checks if rectangle contains the point. -<bool> = <Rect>.colliderect(<Rect>) # Checks if the two rectangles overlap. +<bool> = <Rect>.collidepoint((x, y)) # Checks whether rectangle contains the point. +<bool> = <Rect>.colliderect(<Rect>) # Checks whether the two rectangles overlap. <int> = <Rect>.collidelist(<list_of_Rect>) # Returns index of first colliding Rect or -1. <list> = <Rect>.collidelistall(<list_of_Rect>) # Returns indices of all colliding rectangles. ``` @@ -3018,7 +3015,7 @@ pg.quit() ### Surface **Object for representing images.** ```python -<Surf> = pg.display.set_mode((width, height)) # Opens new window and returns its surface. +<Surf> = pg.display.set_mode((width, height)) # Opens a new window and returns its surface. <Surf> = pg.Surface((width, height)) # New RGB surface. RGBA if `flags=pg.SRCALPHA`. <Surf> = pg.image.load(<path/file>) # Loads the image. Format depends on source. <Surf> = pg.surfarray.make_surface(<np_array>) # Also `<np_arr> = surfarray.pixels3d(<Surf>)`. @@ -3032,15 +3029,15 @@ pg.quit() ``` ```python -from pygame.transform import scale, ... -<Surf> = scale(<Surf>, (width, height)) # Returns scaled surface. -<Surf> = rotate(<Surf>, anticlock_degrees) # Returns rotated and scaled surface. -<Surf> = flip(<Surf>, x_bool, y_bool) # Returns flipped surface. +from pygame.transform import scale, rotate # Also: flip, smoothscale, scale_by. +<Surf> = scale(<Surf>, (width, height)) # Scales the surface. `smoothscale()` blurs it. +<Surf> = rotate(<Surf>, angle) # Rotates the surface for counterclock degrees. +<Surf> = flip(<Surf>, flip_x=False) # Mirrors the surface. Also `flip_y=False`. ``` ```python -from pygame.draw import line, ... -line(<Surf>, color, (x1, y1), (x2, y2), width) # Draws a line to the surface. +from pygame.draw import line, arc, rect # Also: ellipse, polygon, circle, aaline. +line(<Surf>, color, (x1, y1), (x2, y2)) # Draws a line to the surface. Also `width=1`. arc(<Surf>, color, <Rect>, from_rad, to_rad) # Also ellipse(<Surf>, color, <Rect>, width=0). rect(<Surf>, color, <Rect>, width=0) # Also polygon(<Surf>, color, points, width=0). ``` diff --git a/index.html b/index.html index 7818c96..75a71a5 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@ <body> <header> - <aside>May 21, 2025</aside> + <aside>May 30, 2025</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </header> @@ -323,8 +323,8 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span> <str> = <str>.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span> <str> = <str>.translate(<table>) <span class="hljs-comment"># Use `str.maketrans(<dict>)` to generate table.</span> </code></pre> -<pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts int to Unicode character.</span> -<int> = ord(<str>) <span class="hljs-comment"># Converts Unicode character to int.</span> +<pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts passed integer to Unicode character.</span> +<int> = ord(<str>) <span class="hljs-comment"># Converts passed Unicode character to integer.</span> </code></pre> <ul> <li><strong>Use <code class="python hljs"><span class="hljs-string">'unicodedata.normalize("NFC", <str>)'</span></code> on strings like <code class="python hljs"><span class="hljs-string">'Motörhead'</span></code> before comparing them to other strings, because <code class="python hljs"><span class="hljs-string">'ö'</span></code> can be stored as one or two characters.</strong></li> @@ -830,26 +830,23 @@ player = Player(point, direction) <span class="hljs-comment"># (<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>, <span class="hljs-string">'MyClass(1)'</span>) </code></pre> <ul> -<li><strong>Return value of str() should be readable and of repr() unambiguous.</strong></li> -<li><strong>If only repr() is defined, it will also be used for str().</strong></li> -<li><strong>Methods decorated with <code class="python hljs"><span class="hljs-string">'@staticmethod'</span></code> do not receive 'self' nor 'cls' as their first argument.</strong></li> +<li><strong>Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand, for example, <code class="python hljs"><span class="hljs-string">'print(a)'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__str__()'</span></code> and <code class="python hljs"><span class="hljs-string">'a + b'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__add__(b)'</span></code>.</strong></li> +<li><strong>Return value of str() special method should be readable and of repr() unambiguous. If only repr() is defined, it will also be used for str().</strong></li> </ul> <div><h4 id="expressionsthatcallthestrmethod">Expressions that call the str() method:</h4><pre><code class="python language-python hljs">print(<obj>) <span class="hljs-string">f'<span class="hljs-subst">{<obj>}</span>'</span> logging.warning(<obj>) csv.writer(<file>).writerow([<obj>]) -<span class="hljs-keyword">raise</span> Exception(<obj>) </code></pre></div> <div><h4 id="expressionsthatcallthereprmethod">Expressions that call the repr() method:</h4><pre><code class="python language-python hljs">print/str/repr([<obj>]) print/str/repr({<obj>: <obj>}) <span class="hljs-string">f'<span class="hljs-subst">{<obj>!r}</span>'</span> Z = make_dataclass(<span class="hljs-string">'Z'</span>, [<span class="hljs-string">'a'</span>]); print/str/repr(Z(<obj>)) -<span class="hljs-meta">>>> </span><obj> </code></pre></div> <div><h3 id="subclass">Subclass</h3><ul> -<li><strong>Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.</strong></li> +<li><strong>Inheritance is a mechanism that enables a class to extend some other class (that is, subclass to extend its parent), and by doing so inherit all its methods and attributes.</strong></li> <li><strong>Subclass can then add its own methods and attributes or override inherited ones by reusing their names.</strong></li> </ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span> @@ -1439,7 +1436,7 @@ args = p.parse_args() <span class="h <pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span> <iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> </code></pre> -<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns path as str. Also <Path>.as_uri().</span> +<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns path as string. Also <Path>.as_uri().</span> <file> = open(<Path>) <span class="hljs-comment"># Also <Path>.read/write_text/bytes(<args>).</span> </code></pre> <div><h2 id="oscommands"><a href="#oscommands" name="oscommands">#</a>OS Commands</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os, shutil, subprocess @@ -2272,7 +2269,7 @@ right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-numb <pre><code class="python language-python hljs"><Image> = Image.new(<span class="hljs-string">'<mode>'</span>, (width, height)) <span class="hljs-comment"># Creates new image. Also `color=<int/tuple>`.</span> <Image> = Image.open(<path>) <span class="hljs-comment"># Identifies format based on file's contents.</span> <Image> = <Image>.convert(<span class="hljs-string">'<mode>'</span>) <span class="hljs-comment"># Converts image to the new mode (see Modes).</span> -<Image>.save(<path>) <span class="hljs-comment"># Selects format based on extension (PNG/JPG…).</span> +<Image>.save(<path>) <span class="hljs-comment"># Accepts `quality=<int>` if extension is jpg.</span> <Image>.show() <span class="hljs-comment"># Displays image in default preview app.</span> </code></pre> <pre><code class="python language-python hljs"><int/tup> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns pixel's value (its color).</span> @@ -2288,8 +2285,8 @@ right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-numb <Image> = Image.fromarray(np.uint8(<array>)) <span class="hljs-comment"># Use <array>.clip(0, 255) to clip the values.</span> </code></pre> <div><h3 id="modes-1">Modes</h3><ul> -<li><strong><code class="python hljs"><span class="hljs-string">'L'</span></code> - Lightness (greyscale image). Each pixel is an int between 0 and 255.</strong></li> -<li><strong><code class="python hljs"><span class="hljs-string">'RGB'</span></code> - Red, green, blue (true color image). Each pixel is a tuple of three ints.</strong></li> +<li><strong><code class="python hljs"><span class="hljs-string">'L'</span></code> - Lightness (greyscale image). Each pixel is an integer between 0 and 255.</strong></li> +<li><strong><code class="python hljs"><span class="hljs-string">'RGB'</span></code> - Red, green, blue (true color image). Each pixel is a tuple of three integers.</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - Hue, saturation, value. Three ints representing color in HSV color space.</strong></li> </ul><div><h3 id="examples">Examples</h3><div><h4 id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><code class="python language-python hljs">WIDTH, HEIGHT = <span class="hljs-number">100</span>, <span class="hljs-number">100</span> @@ -2452,10 +2449,10 @@ pg.init() screen = pg.display.set_mode((<span class="hljs-number">500</span>, <span class="hljs-number">500</span>)) rect = pg.Rect(<span class="hljs-number">240</span>, <span class="hljs-number">240</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>) <span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT): - deltas = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), pg.K_RIGHT: (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), pg.K_LEFT: (<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)} <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN): - x, y = deltas.get(event.key, (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)) - rect = rect.move((x*<span class="hljs-number">20</span>, y*<span class="hljs-number">20</span>)) + dx = (event.key == pg.K_RIGHT) - (event.key == pg.K_LEFT) + dy = (event.key == pg.K_DOWN) - (event.key == pg.K_UP) + rect = rect.move((dx * <span class="hljs-number">20</span>, dy * <span class="hljs-number">20</span>)) screen.fill(pg.Color(<span class="hljs-string">'black'</span>)) pg.draw.rect(screen, pg.Color(<span class="hljs-string">'white'</span>), rect) pg.display.flip() @@ -2465,17 +2462,17 @@ pg.quit() <div><h3 id="rect">Rect</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs"><Rect> = pg.Rect(x, y, width, height) <span class="hljs-comment"># Creates Rect object. Truncates passed floats.</span> <int> = <Rect>.x/y/centerx/centery/… <span class="hljs-comment"># `top/right/bottom/left`. Allows assignments.</span> -<tup.> = <Rect>.topleft/center/… <span class="hljs-comment"># `topright/bottomright/bottomleft`. Same.</span> -<Rect> = <Rect>.move((delta_x, delta_y)) <span class="hljs-comment"># Use move_ip() to move in-place.</span> +<tup.> = <Rect>.topleft/center/… <span class="hljs-comment"># `topright/bottomright/bottomleft/size`. Same.</span> +<Rect> = <Rect>.move((delta_x, delta_y)) <span class="hljs-comment"># Use move_ip() to move the rectangle in-place.</span> </code></pre></div> -<pre><code class="python language-python hljs"><bool> = <Rect>.collidepoint((x, y)) <span class="hljs-comment"># Checks if rectangle contains the point.</span> -<bool> = <Rect>.colliderect(<Rect>) <span class="hljs-comment"># Checks if the two rectangles overlap.</span> +<pre><code class="python language-python hljs"><bool> = <Rect>.collidepoint((x, y)) <span class="hljs-comment"># Checks whether rectangle contains the point.</span> +<bool> = <Rect>.colliderect(<Rect>) <span class="hljs-comment"># Checks whether the two rectangles overlap.</span> <int> = <Rect>.collidelist(<list_of_Rect>) <span class="hljs-comment"># Returns index of first colliding Rect or -1.</span> <list> = <Rect>.collidelistall(<list_of_Rect>) <span class="hljs-comment"># Returns indices of all colliding rectangles.</span> </code></pre> -<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs"><Surf> = pg.display.set_mode((width, height)) <span class="hljs-comment"># Opens new window and returns its surface.</span> +<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs"><Surf> = pg.display.set_mode((width, height)) <span class="hljs-comment"># Opens a new window and returns its surface.</span> <Surf> = pg.Surface((width, height)) <span class="hljs-comment"># New RGB surface. RGBA if `flags=pg.SRCALPHA`.</span> <Surf> = pg.image.load(<path/file>) <span class="hljs-comment"># Loads the image. Format depends on source.</span> <Surf> = pg.surfarray.make_surface(<np_array>) <span class="hljs-comment"># Also `<np_arr> = surfarray.pixels3d(<Surf>)`.</span> @@ -2487,13 +2484,13 @@ pg.quit() <Surf>.set_at((x, y), color) <span class="hljs-comment"># Updates pixel. Also <Surf>.get_at((x, y)).</span> <Surf>.blit(<Surf>, (x, y)) <span class="hljs-comment"># Draws passed surface at specified location.</span> </code></pre> -<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.transform <span class="hljs-keyword">import</span> scale, ... -<Surf> = scale(<Surf>, (width, height)) <span class="hljs-comment"># Returns scaled surface.</span> -<Surf> = rotate(<Surf>, anticlock_degrees) <span class="hljs-comment"># Returns rotated and scaled surface.</span> -<Surf> = flip(<Surf>, x_bool, y_bool) <span class="hljs-comment"># Returns flipped surface.</span> +<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.transform <span class="hljs-keyword">import</span> scale, rotate <span class="hljs-comment"># Also: flip, smoothscale, scale_by.</span> +<Surf> = scale(<Surf>, (width, height)) <span class="hljs-comment"># Scales the surface. `smoothscale()` blurs it.</span> +<Surf> = rotate(<Surf>, angle) <span class="hljs-comment"># Rotates the surface for counterclock degrees.</span> +<Surf> = flip(<Surf>, flip_x=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Mirrors the surface. Also `flip_y=False`.</span> </code></pre> -<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> line, ... -line(<Surf>, color, (x1, y1), (x2, y2), width) <span class="hljs-comment"># Draws a line to the surface.</span> +<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> line, arc, rect <span class="hljs-comment"># Also: ellipse, polygon, circle, aaline.</span> +line(<Surf>, color, (x1, y1), (x2, y2)) <span class="hljs-comment"># Draws a line to the surface. Also `width=1`.</span> arc(<Surf>, color, <Rect>, from_rad, to_rad) <span class="hljs-comment"># Also ellipse(<Surf>, color, <Rect>, width=0).</span> rect(<Surf>, color, <Rect>, width=<span class="hljs-number">0</span>) <span class="hljs-comment"># Also polygon(<Surf>, color, points, width=0).</span> </code></pre> @@ -2942,7 +2939,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active <footer> - <aside>May 21, 2025</aside> + <aside>May 30, 2025</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </footer> diff --git a/parse.js b/parse.js index 3d18669..a54d578 100755 --- a/parse.js +++ b/parse.js @@ -83,8 +83,7 @@ const REPR_USE_CASES = 'print/str/repr([<obj>])\n' + 'print/str/repr({<obj>: <obj>})\n' + '<span class="hljs-string">f\'<span class="hljs-subst">{<obj>!r}</span>\'</span>\n' + - 'Z = make_dataclass(<span class="hljs-string">\'Z\'</span>, [<span class="hljs-string">\'a\'</span>]); print/str/repr(Z(<obj>))\n' + - '<span class="hljs-meta">>>> </span><obj>\n'; + 'Z = make_dataclass(<span class="hljs-string">\'Z\'</span>, [<span class="hljs-string">\'a\'</span>]); print/str/repr(Z(<obj>))\n'; const CONSTRUCTOR_OVERLOADING = '<span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">name</span>>:</span>\n' +