Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dea8327

Browse files
committed
String, Regex, Format, Duck types, Pygame
1 parent 031875e commit dea8327

File tree

3 files changed

+111
-111
lines changed

3 files changed

+111
-111
lines changed

README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ String
308308
```python
309309
<list> = <str>.split() # Splits on one or more whitespace characters.
310310
<list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times.
311-
<list> = <str>.splitlines(keepends=False) # Splits on \n,\r,\r\n. Keeps them if 'keepends'.
311+
<list> = <str>.splitlines(keepends=False) # Splits on [\n\r\f\v\x1c\x1d\x1e\x85] and '\r\n'.
312312
<str> = <str>.join(<coll_of_strings>) # Joins elements using string as a separator.
313313
```
314314

@@ -376,12 +376,13 @@ import re
376376
```
377377

378378
### Special Sequences
379-
* **By default digits, alphanumerics and whitespaces from all alphabets are matched, unless `'flags=re.ASCII'` argument is used.**
379+
* **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.**
380+
* **As shown below, it restricts special sequence matches to `'[\x00-\x7f]'` and prevents `'\s'` from accepting `'[\x1c\x1d\x1e\x1f]'`.**
380381
* **Use a capital letter for negation.**
381382
```python
382-
'\d' == '[0-9]' # Matches any digit.
383-
'\w' == '[a-zA-Z0-9_]' # Matches any alphanumeric.
384-
'\s' == '[ \t\n\r\f\v]' # Matches any whitespace.
383+
'\d' == '[0-9]' # Matches decimal characters.
384+
'\w' == '[a-zA-Z0-9_]' # Matches alphanumerics and underscore.
385+
'\s' == '[ \t\n\r\f\v]' # Matches whitespaces.
385386
```
386387

387388

@@ -440,33 +441,32 @@ Format
440441

441442
#### Comparison of presentation types:
442443
```text
443-
+---------------+-----------------+-----------------+-----------------+-----------------+
444-
| | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |
445-
+---------------+-----------------+-----------------+-----------------+-----------------+
446-
| 0.000056789 | '5.6789e-05' | '0.000057' | '5.678900e-05' | '0.005679%' |
447-
| 0.00056789 | '0.00056789' | '0.000568' | '5.678900e-04' | '0.056789%' |
448-
| 0.0056789 | '0.0056789' | '0.005679' | '5.678900e-03' | '0.567890%' |
449-
| 0.056789 | '0.056789' | '0.056789' | '5.678900e-02' | '5.678900%' |
450-
| 0.56789 | '0.56789' | '0.567890' | '5.678900e-01' | '56.789000%' |
451-
| 5.6789 | '5.6789' | '5.678900' | '5.678900e+00' | '567.890000%' |
452-
| 56.789 | '56.789' | '56.789000' | '5.678900e+01' | '5678.900000%' |
453-
| 567.89 | '567.89' | '567.890000' | '5.678900e+02' | '56789.000000%' |
454-
+---------------+-----------------+-----------------+-----------------+-----------------+
444+
+--------------+----------------+----------------+----------------+----------------+
445+
| | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |
446+
+--------------+----------------+----------------+----------------+----------------+
447+
| 0.000056789 | '5.6789e-05' | '0.000057' | '5.678900e-05' | '0.005679%' |
448+
| 0.00056789 | '0.00056789' | '0.000568' | '5.678900e-04' | '0.056789%' |
449+
| 0.0056789 | '0.0056789' | '0.005679' | '5.678900e-03' | '0.567890%' |
450+
| 0.056789 | '0.056789' | '0.056789' | '5.678900e-02' | '5.678900%' |
451+
| 0.56789 | '0.56789' | '0.567890' | '5.678900e-01' | '56.789000%' |
452+
| 5.6789 | '5.6789' | '5.678900' | '5.678900e+00' | '567.890000%' |
453+
| 56.789 | '56.789' | '56.789000' | '5.678900e+01' | '5678.900000%' |
454+
+--------------+----------------+----------------+----------------+----------------+
455455
```
456456
```text
457-
+---------------+-----------------+-----------------+-----------------+-----------------+
458-
| | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |
459-
+---------------+-----------------+-----------------+-----------------+-----------------+
460-
| 0.000056789 | '5.7e-05' | '0.00' | '5.68e-05' | '0.01%' |
461-
| 0.00056789 | '0.00057' | '0.00' | '5.68e-04' | '0.06%' |
462-
| 0.0056789 | '0.0057' | '0.01' | '5.68e-03' | '0.57%' |
463-
| 0.056789 | '0.057' | '0.06' | '5.68e-02' | '5.68%' |
464-
| 0.56789 | '0.57' | '0.57' | '5.68e-01' | '56.79%' |
465-
| 5.6789 | '5.7' | '5.68' | '5.68e+00' | '567.89%' |
466-
| 56.789 | '5.7e+01' | '56.79' | '5.68e+01' | '5678.90%' |
467-
| 567.89 | '5.7e+02' | '567.89' | '5.68e+02' | '56789.00%' |
468-
+---------------+-----------------+-----------------+-----------------+-----------------+
469-
```
457+
+--------------+----------------+----------------+----------------+----------------+
458+
| | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |
459+
+--------------+----------------+----------------+----------------+----------------+
460+
| 0.000056789 | '5.7e-05' | '0.00' | '5.68e-05' | '0.01%' |
461+
| 0.00056789 | '0.00057' | '0.00' | '5.68e-04' | '0.06%' |
462+
| 0.0056789 | '0.0057' | '0.01' | '5.68e-03' | '0.57%' |
463+
| 0.056789 | '0.057' | '0.06' | '5.68e-02' | '5.68%' |
464+
| 0.56789 | '0.57' | '0.57' | '5.68e-01' | '56.79%' |
465+
| 5.6789 | '5.7' | '5.68' | '5.68e+00' | '567.89%' |
466+
| 56.789 | '5.7e+01' | '56.79' | '5.68e+01' | '5678.90%' |
467+
+--------------+----------------+----------------+----------------+----------------+
468+
```
469+
* **When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes `'{6.5:.0f}'` a `'6'` and `'{7.5:.0f}'` an `'8'`.**
470470

471471
### Ints
472472
```python
@@ -1140,7 +1140,7 @@ class Counter:
11401140
```
11411141

11421142
#### Python has many different iterator objects:
1143-
* **Iterators returned by the [iter()](#iterator) function, such as list\_iterator and set\_iterator.**
1143+
* **Sequence iterators returned by the [iter()](#iterator) function, such as list\_iterator and set\_iterator.**
11441144
* **Objects returned by the [itertools](#itertools) module, such as count, repeat and cycle.**
11451145
* **Generators returned by the [generator functions](#generator) and [generator expressions](#comprehensions).**
11461146
* **File objects returned by the [open()](#open) function, etc.**
@@ -2506,7 +2506,7 @@ def odds_handler(sport):
25062506
# $ pip3 install requests
25072507
>>> import threading, requests
25082508
>>> threading.Thread(target=run, daemon=True).start()
2509-
>>> url = 'http://localhost:8080/odds/football'
2509+
>>> url = 'http://localhost:8080/odds/football'
25102510
>>> data = {'team': 'arsenal f.c.'}
25112511
>>> response = requests.post(url, data=data)
25122512
>>> response.json()
@@ -2899,7 +2899,7 @@ get_pause = lambda seconds: repeat(0, int(seconds * F))
28992899
sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
29002900
get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
29012901
get_hz = lambda key: 8.176 * 2 ** (int(key) / 12)
2902-
parse_note = lambda note: (get_hz(note[:-1]), 1/4 if '' in note else 1/8)
2902+
parse_note = lambda note: (get_hz(note[:2]), 1/4 if '' in note else 1/8)
29032903
get_samples = lambda note: get_wave(*parse_note(note)) if note else get_pause(1/8)
29042904
samples_f = chain.from_iterable(get_samples(n) for n in f'{P1}{P1}{P2}'.split(','))
29052905
samples_b = b''.join(struct.pack('<h', int(f * 30000)) for f in samples_f)
@@ -2957,14 +2957,14 @@ while all(event.type != pg.QUIT for event in pg.event.get()):
29572957
```
29582958

29592959
```python
2960-
from pygame.transform import *
2960+
from pygame.transform import scale, …
29612961
<Surf> = scale(<Surf>, (width, height)) # Returns scaled surface.
29622962
<Surf> = rotate(<Surf>, degrees) # Returns rotated and scaled surface.
29632963
<Surf> = flip(<Surf>, x_bool, y_bool) # Returns flipped surface.
29642964
```
29652965

29662966
```python
2967-
from pygame.draw import *
2967+
from pygame.draw import line, arc, rect
29682968
line(<Surf>, color, (x1, y1), (x2, y2), width) # Draws a line to the surface.
29692969
arc(<Surf>, color, <Rect>, from_rad, to_rad) # Also: ellipse(<Surf>, color, <Rect>)
29702970
rect(<Surf>, color, <Rect>) # Also: polygon(<Surf>, color, points)
@@ -3029,12 +3029,12 @@ def update_speed(mario, tiles, pressed):
30293029
mario.spd = P(*[max(-limit, min(limit, s)) for limit, s in zip(MAX_SPEED, P(x, y))])
30303030

30313031
def update_position(mario, tiles):
3032-
p = mario.rect.topleft
3033-
larger_speed = max(abs(s) for s in mario.spd)
3034-
for _ in range(larger_speed):
3032+
x, y = mario.rect.topleft
3033+
n_steps = max(abs(s) for s in mario.spd)
3034+
for _ in range(n_steps):
30353035
mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))
3036-
p = P(*[a + s/larger_speed for a, s in zip(p, mario.spd)])
3037-
mario.rect.topleft = p
3036+
x, y = x + mario.spd.x/n_steps, y + mario.spd.y/n_steps
3037+
mario.rect.topleft = x, y
30383038

30393039
def get_boundaries(rect, tiles):
30403040
deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)}

0 commit comments

Comments
 (0)