Skip to content

Commit 37c7ee1

Browse files
committed
Whitespace fixes from Py2 version applied to Py3.
This makes the Python 3 version of about_regex.py identical to the Python 2 version. Most of the whitespace and grammar fixes were made to only the Python 2 version on Fri 10 Feb 2012 by gregmalcolm, as part of a larger commit: Merged generators fix from engored and pep8 fixes from sietsebb via bitbucket mirror gregmalcolm@d65e690#diff-612bda3dc05c74b8fdd16c0df2dd28c8 (Only about a half-dozen minor changes have been made since then.)
1 parent 53df61e commit 37c7ee1

1 file changed

Lines changed: 65 additions & 41 deletions

File tree

python3/koans/about_regex.py

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,76 @@
22
# -*- coding: utf-8 -*-
33

44
from runner.koan import *
5+
56
import re
7+
8+
69
class AboutRegex(Koan):
710
"""
8-
These koans are based on the Ben's book: Regular Expressions in 10 minutes.
9-
I found this books very useful so I decided to write a koans in order to practice everything I had learned from it.
11+
These koans are based on Ben's book: Regular Expressions in 10
12+
minutes. I found this book very useful, so I decided to write
13+
a koan file in order to practice everything it taught me.
1014
http://www.forta.com/books/0672325667/
1115
"""
1216

1317
def test_matching_literal_text(self):
1418
"""
1519
Lesson 1 Matching Literal String
1620
"""
17-
string = "Hello, my name is Felix and this koans are based on the Ben's book: Regular Expressions in 10 minutes."
21+
string = "Hello, my name is Felix and these koans are based " + \
22+
"on Ben's book: Regular Expressions in 10 minutes."
1823
m = re.search(__, string)
19-
self.assertTrue(m and m.group(0) and m.group(0)== 'Felix', "I want my name")
24+
self.assertTrue(
25+
m and m.group(0) and
26+
m.group(0) == 'Felix',
27+
"I want my name")
2028

2129
def test_matching_literal_text_how_many(self):
2230
"""
23-
Lesson 1 How many matches?
24-
25-
The default behaviour of most regular expression engines is to return just the first match.
26-
In python you have the next options:
27-
28-
match() --> Determine if the RE matches at the beginning of the string.
29-
search() --> Scan through a string, looking for any location where this RE matches.
30-
findall() --> Find all substrings where the RE matches, and returns them as a list.
31-
finditer() --> Find all substrings where the RE matches, and returns them as an iterator.
32-
31+
Lesson 1 -- How many matches?
32+
33+
The default behaviour of most regular expression engines is
34+
to return just the first match. In python you have the
35+
following options:
36+
37+
match() --> Determine if the RE matches at the
38+
beginning of the string.
39+
search() --> Scan through a string, looking for any
40+
location where this RE matches.
41+
findall() --> Find all substrings where the RE
42+
matches, and return them as a list.
43+
finditer() --> Find all substrings where the RE
44+
matches, and return them as an iterator.
3345
"""
34-
string = "Hello, my name is Felix and this koans are based on the Ben's book: Regular Expressions in 10 minutes. Repeat My name is Felix"
35-
m = re.match('Felix', string) #TIP: Maybe match it's not the best option
46+
string = ("Hello, my name is Felix and these koans are based " +
47+
"on Ben's book: Regular Expressions in 10 minutes. " +
48+
"Repeat My name is Felix")
49+
m = re.match('Felix', string) # TIP: match may not be the best option
3650

37-
# I want to know how many times appears my name
51+
# I want to know how many times my name appears
3852
self.assertEqual(m, __)
3953

4054
def test_matching_literal_text_not_case_sensitivity(self):
4155
"""
42-
Lesson 1 Matching Literal String non case sensitivity.
43-
Most regex implementations also support matches that are not case sensitive. In python you can use re.IGNORECASE, in
44-
Javascript you can specify the optional i flag.
45-
In Ben's book you can see more languages.
56+
Lesson 1 -- Matching Literal String non case sensitivity.
57+
Most regex implementations also support matches that are not
58+
case sensitive. In python you can use re.IGNORECASE, in
59+
Javascript you can specify the optional i flag. In Ben's
60+
book you can see more languages.
4661
4762
"""
48-
string = "Hello, my name is Felix or felix and this koans is based on the Ben's book: Regular Expressions in 10 minutes."
63+
string = "Hello, my name is Felix or felix and this koan " + \
64+
"is based on Ben's book: Regular Expressions in 10 minutes."
4965

5066
self.assertEqual(re.findall("felix", string), __)
5167
self.assertEqual(re.findall("felix", string, re.IGNORECASE), __)
5268

5369
def test_matching_any_character(self):
5470
"""
55-
Lesson 1 Matching any character
71+
Lesson 1: Matching any character
5672
57-
. matches any character, alphabetic characters, digits and .
73+
`.` matches any character: alphabetic characters, digits,
74+
and punctuation.
5875
"""
5976
string = "pecks.xlx\n" \
6077
+ "orders1.xls\n" \
@@ -63,17 +80,19 @@ def test_matching_any_character(self):
6380
+ "na2.xls\n" \
6481
+ "sa1.xls"
6582

66-
# TIP: remember the name of this lesson
67-
68-
change_this_search_string = 'a..xlx' # <-- I want to find all uses of myArray
69-
self.assertEquals(len(re.findall(change_this_search_string, string)),3)
83+
# I want to find all uses of myArray
84+
change_this_search_string = 'a..xlx'
85+
self.assertEquals(
86+
len(re.findall(change_this_search_string, string)),
87+
3)
7088

7189
def test_matching_set_character(self):
7290
"""
73-
Lesson 2 Matching sets of characters
91+
Lesson 2 -- Matching sets of characters
7492
75-
A set of characters is defined using the metacharacters [ and ]. Everything between them is part of the set and
76-
any one of the set members must match (but not all).
93+
A set of characters is defined using the metacharacters
94+
`[` and `]`. Everything between them is part of the set, and
95+
any single one of the set members will match.
7796
"""
7897
string = "sales.xlx\n" \
7998
+ "sales1.xls\n" \
@@ -84,16 +103,21 @@ def test_matching_set_character(self):
84103
+ "na2.xls\n" \
85104
+ "sa1.xls\n" \
86105
+ "ca1.xls"
87-
# I want to find all files for North America(na) or South America(sa), but not (ca)
88-
# TIP you can use the pattern .a. which matches in above test but in this case matches more than you want
106+
# I want to find all files for North America(na) or South
107+
# America(sa), but not (ca) TIP you can use the pattern .a.
108+
# which matches in above test but in this case matches more than
109+
# you want
89110
change_this_search_string = '[nsc]a[2-9].xls'
90-
self.assertEquals(len(re.findall(change_this_search_string, string)),3)
111+
self.assertEquals(
112+
len(re.findall(change_this_search_string, string)),
113+
3)
91114

92115
def test_anything_but_matching(self):
93116
"""
94-
Lesson 2 Using character set ranges
95-
Occasionally, you'll want a list of characters that you don't want to match.
96-
Character sets can be negated using the ^ metacharacter.
117+
Lesson 2 -- Using character set ranges
118+
Occasionally, you'll have a list of characters that you don't
119+
want to match. Character sets can be negated using the ^
120+
metacharacter.
97121
98122
"""
99123
string = "sales.xlx\n" \
@@ -109,8 +133,8 @@ def test_anything_but_matching(self):
109133
+ "sa1.xls\n" \
110134
+ "ca1.xls"
111135

112-
# I want to find the name sam
136+
# I want to find the name 'sam'
113137
change_this_search_string = '[^nc]am'
114-
self.assertEquals(re.findall(change_this_search_string, string), ['sam.xls'])
115-
116-
138+
self.assertEquals(
139+
re.findall(change_this_search_string, string),
140+
['sam.xls'])

0 commit comments

Comments
 (0)