Skip to content

Commit abdb6bc

Browse files
committed
Regex, libraries examples formating
1 parent 7f31af5 commit abdb6bc

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ import re
270270
<list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to keep the matches.
271271
<Match> = re.search(<regex>, text) # Searches for first occurrence of pattern.
272272
<Match> = re.match(<regex>, text) # Searches only at the beginning of the text.
273-
<iter> = re.finditer(<regex>, text) # Returns iterator of all matches.
273+
<iter> = re.finditer(<regex>, text) # Returns all occurrences as match objects.
274274
```
275275

276276
* **Parameter `'flags=re.IGNORECASE'` can be used with all functions.**
@@ -280,10 +280,11 @@ import re
280280

281281
### Match Object
282282
```python
283-
<str> = <Match>.group() # Whole match.
284-
<str> = <Match>.group(1) # Part in first bracket.
285-
<int> = <Match>.start() # Start index of a match.
286-
<int> = <Match>.end() # Exclusive end index of a match.
283+
<str> = <Match>.group() # Whole match.
284+
<str> = <Match>.group(1) # Part in first bracket.
285+
<tuple> = <Match>.groups() # All bracketed parts.
286+
<int> = <Match>.start() # Start index of a match.
287+
<int> = <Match>.end() # Exclusive end index of a match.
287288
```
288289

289290
### Special Sequences
@@ -1469,7 +1470,7 @@ desc = 'calculate X to the power of Y'
14691470
parser = ArgumentParser(description=desc)
14701471
group = parser.add_mutually_exclusive_group()
14711472
group.add_argument('-v', '--verbose', action='store_true')
1472-
group.add_argument('-q', '--quiet', action='store_true')
1473+
group.add_argument('-q', '--quiet', action='store_true')
14731474
parser.add_argument('x', type=int, help='the base')
14741475
parser.add_argument('y', type=int, help='the exponent')
14751476
args = parser.parse_args()
@@ -1489,12 +1490,13 @@ Table
14891490
#### Prints CSV file as ASCII table:
14901491
```python
14911492
# $ pip3 install tabulate
1492-
from csv import reader
1493+
import csv
14931494
from tabulate import tabulate
14941495
with open(<filename>, encoding='utf-8', newline='') as file:
1495-
reader = reader(file, delimiter=';')
1496-
headers = [a.title() for a in next(reader)]
1497-
print(tabulate(reader, headers))
1496+
lines = csv.reader(file, delimiter=';')
1497+
headers = [a.title() for a in next(lines)]
1498+
table = tabulate(lines, headers)
1499+
print(table)
14981500
```
14991501

15001502

@@ -1527,10 +1529,12 @@ Image
15271529
```python
15281530
# $ pip3 install pillow
15291531
from PIL import Image
1530-
width, height = 100, 100
1531-
img = Image.new('L', (width, height), 'white')
1532+
width = 100
1533+
height = 100
15321534
size = width * height
1533-
pixels = [255 * a/size for a in range(size)]
1535+
pixels = [255 * i/size for i in range(size)]
1536+
1537+
img = Image.new('L', (width, height), 'white')
15341538
img.putdata(pixels)
15351539
img.save('out.png')
15361540
```
@@ -1545,13 +1549,13 @@ img.save('out.png')
15451549

15461550
Audio
15471551
-----
1548-
#### Saves a list of floats with values between 0 and 1 to a WAV file:
1552+
#### Saves a list of floats with values between -1 and 1 to a WAV file:
15491553
```python
15501554
import wave, struct
1551-
samples = [struct.pack('h', int((a-0.5)*60000)) for a in <list>]
1552-
wf = wave.open(<filename>, 'wb')
1555+
samples = [struct.pack('<h', int(a * 30000)) for a in <list>]
1556+
wf = wave.open('test.wav', 'wb')
15531557
wf.setnchannels(1)
1554-
wf.setsampwidth(4)
1558+
wf.setsampwidth(2)
15551559
wf.setframerate(44100)
15561560
wf.writeframes(b''.join(samples))
15571561
wf.close()

0 commit comments

Comments
 (0)