Skip to content

Commit 32e730e

Browse files
committed
String, Class, Image, Pygame
1 parent edb7680 commit 32e730e

File tree

3 files changed

+49
-56
lines changed

3 files changed

+49
-56
lines changed

README.md

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ String
337337
```
338338

339339
```python
340-
<str> = chr(<int>) # Converts int to Unicode character.
341-
<int> = ord(<str>) # Converts Unicode character to int.
340+
<str> = chr(<int>) # Converts passed integer to Unicode character.
341+
<int> = ord(<str>) # Converts passed Unicode character to integer.
342342
```
343343
* **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.**
344344
* **`'NFC'` converts such characters to a single character, while `'NFD'` converts them to two.**
@@ -972,17 +972,15 @@ class MyClass:
972972
>>> obj.a, str(obj), repr(obj)
973973
(1, '1', 'MyClass(1)')
974974
```
975-
* **Return value of str() should be readable and of repr() unambiguous.**
976-
* **If only repr() is defined, it will also be used for str().**
977-
* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first argument.**
975+
* **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)'`.**
976+
* **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().**
978977

979978
#### Expressions that call the str() method:
980979
```python
981980
print(<obj>)
982981
f'{<obj>}'
983982
logging.warning(<obj>)
984983
csv.writer(<file>).writerow([<obj>])
985-
raise Exception(<obj>)
986984
```
987985

988986
#### Expressions that call the repr() method:
@@ -991,11 +989,10 @@ print/str/repr([<obj>])
991989
print/str/repr({<obj>: <obj>})
992990
f'{<obj>!r}'
993991
Z = make_dataclass('Z', ['a']); print/str/repr(Z(<obj>))
994-
>>> <obj>
995992
```
996993

997994
### Subclass
998-
* **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.**
995+
* **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.**
999996
* **Subclass can then add its own methods and attributes or override inherited ones by reusing their names.**
1000997

1001998
```python
@@ -1694,7 +1691,7 @@ from pathlib import Path
16941691
```
16951692

16961693
```python
1697-
<str> = str(<Path>) # Returns path as str. Also <Path>.as_uri().
1694+
<str> = str(<Path>) # Returns path as string. Also <Path>.as_uri().
16981695
<file> = open(<Path>) # Also <Path>.read/write_text/bytes(<args>).
16991696
```
17001697

@@ -2765,7 +2762,7 @@ from PIL import Image
27652762
<Image> = Image.new('<mode>', (width, height)) # Creates new image. Also `color=<int/tuple>`.
27662763
<Image> = Image.open(<path>) # Identifies format based on file's contents.
27672764
<Image> = <Image>.convert('<mode>') # Converts image to the new mode (see Modes).
2768-
<Image>.save(<path>) # Selects format based on extension (PNG/JPG…).
2765+
<Image>.save(<path>) # Accepts `quality=<int>` if extension is jpg.
27692766
<Image>.show() # Displays image in default preview app.
27702767
```
27712768

@@ -2788,8 +2785,8 @@ from PIL import Image
27882785
```
27892786

27902787
### Modes
2791-
* **`'L'` - Lightness (greyscale image). Each pixel is an int between 0 and 255.**
2792-
* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three ints.**
2788+
* **`'L'` - Lightness (greyscale image). Each pixel is an integer between 0 and 255.**
2789+
* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three integers.**
27932790
* **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.**
27942791
* **`'HSV'` - Hue, saturation, value. Three ints representing color in HSV color space.**
27952792

@@ -2989,10 +2986,10 @@ pg.init()
29892986
screen = pg.display.set_mode((500, 500))
29902987
rect = pg.Rect(240, 240, 20, 20)
29912988
while not pg.event.get(pg.QUIT):
2992-
deltas = {pg.K_UP: (0, -1), pg.K_RIGHT: (1, 0), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0)}
29932989
for event in pg.event.get(pg.KEYDOWN):
2994-
x, y = deltas.get(event.key, (0, 0))
2995-
rect = rect.move((x*20, y*20))
2990+
dx = (event.key == pg.K_RIGHT) - (event.key == pg.K_LEFT)
2991+
dy = (event.key == pg.K_DOWN) - (event.key == pg.K_UP)
2992+
rect = rect.move((dx * 20, dy * 20))
29962993
screen.fill(pg.Color('black'))
29972994
pg.draw.rect(screen, pg.Color('white'), rect)
29982995
pg.display.flip()
@@ -3004,21 +3001,21 @@ pg.quit()
30043001
```python
30053002
<Rect> = pg.Rect(x, y, width, height) # Creates Rect object. Truncates passed floats.
30063003
<int> = <Rect>.x/y/centerx/centery/# `top/right/bottom/left`. Allows assignments.
3007-
<tup.> = <Rect>.topleft/center/# `topright/bottomright/bottomleft`. Same.
3008-
<Rect> = <Rect>.move((delta_x, delta_y)) # Use move_ip() to move in-place.
3004+
<tup.> = <Rect>.topleft/center/# `topright/bottomright/bottomleft/size`. Same.
3005+
<Rect> = <Rect>.move((delta_x, delta_y)) # Use move_ip() to move the rectangle in-place.
30093006
```
30103007

30113008
```python
3012-
<bool> = <Rect>.collidepoint((x, y)) # Checks if rectangle contains the point.
3013-
<bool> = <Rect>.colliderect(<Rect>) # Checks if the two rectangles overlap.
3009+
<bool> = <Rect>.collidepoint((x, y)) # Checks whether rectangle contains the point.
3010+
<bool> = <Rect>.colliderect(<Rect>) # Checks whether the two rectangles overlap.
30143011
<int> = <Rect>.collidelist(<list_of_Rect>) # Returns index of first colliding Rect or -1.
30153012
<list> = <Rect>.collidelistall(<list_of_Rect>) # Returns indices of all colliding rectangles.
30163013
```
30173014

30183015
### Surface
30193016
**Object for representing images.**
30203017
```python
3021-
<Surf> = pg.display.set_mode((width, height)) # Opens new window and returns its surface.
3018+
<Surf> = pg.display.set_mode((width, height)) # Opens a new window and returns its surface.
30223019
<Surf> = pg.Surface((width, height)) # New RGB surface. RGBA if `flags=pg.SRCALPHA`.
30233020
<Surf> = pg.image.load(<path/file>) # Loads the image. Format depends on source.
30243021
<Surf> = pg.surfarray.make_surface(<np_array>) # Also `<np_arr> = surfarray.pixels3d(<Surf>)`.
@@ -3032,15 +3029,15 @@ pg.quit()
30323029
```
30333030

30343031
```python
3035-
from pygame.transform import scale, ...
3036-
<Surf> = scale(<Surf>, (width, height)) # Returns scaled surface.
3037-
<Surf> = rotate(<Surf>, anticlock_degrees) # Returns rotated and scaled surface.
3038-
<Surf> = flip(<Surf>, x_bool, y_bool) # Returns flipped surface.
3032+
from pygame.transform import scale, rotate # Also: flip, smoothscale, scale_by.
3033+
<Surf> = scale(<Surf>, (width, height)) # Scales the surface. `smoothscale()` blurs it.
3034+
<Surf> = rotate(<Surf>, angle) # Rotates the surface for counterclock degrees.
3035+
<Surf> = flip(<Surf>, flip_x=False) # Mirrors the surface. Also `flip_y=False`.
30393036
```
30403037

30413038
```python
3042-
from pygame.draw import line, ...
3043-
line(<Surf>, color, (x1, y1), (x2, y2), width) # Draws a line to the surface.
3039+
from pygame.draw import line, arc, rect # Also: ellipse, polygon, circle, aaline.
3040+
line(<Surf>, color, (x1, y1), (x2, y2)) # Draws a line to the surface. Also `width=1`.
30443041
arc(<Surf>, color, <Rect>, from_rad, to_rad) # Also ellipse(<Surf>, color, <Rect>, width=0).
30453042
rect(<Surf>, color, <Rect>, width=0) # Also polygon(<Surf>, color, points, width=0).
30463043
```

0 commit comments

Comments
 (0)