forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentence.doctest
More file actions
75 lines (64 loc) · 1.32 KB
/
sentence.doctest
File metadata and controls
75 lines (64 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
>>> from sentence import Sentence
>>> s = Sentence('The time has come')
>>> s
Sentence('The time has come')
>>> s[0]
'The'
>>> list(s)
['The', 'time', 'has', 'come']
>>> s = Sentence('"The time has come," the Walrus said,')
>>> s
Sentence('"The time ha... Walrus said,')
>>> s[0]
'The'
>>> s[1]
'time'
>>> for word in s:
... print(word)
The
time
has
come
the
Walrus
said
>>> list(s)
['The', 'time', 'has', 'come', 'the', 'Walrus', 'said']
>>> s[-2]
'Walrus'
>>> s[2:4]
['has', 'come']
>>> s[:4]
['The', 'time', 'has', 'come']
>>> s[4:]
['the', 'Walrus', 'said']
>>> s3 = Sentence('Pig and Pepper')
>>> it = iter(s3)
>>> it # doctest: +ELLIPSIS
<iterator object at 0x...>
>>> next(it)
'Pig'
>>> next(it)
'and'
>>> next(it)
'Pepper'
>>> next(it)
Traceback (most recent call last):
...
StopIteration
>>> list(it)
[]
>>> list(iter(s3))
['Pig', 'and', 'Pepper']
>>> s = Sentence('''The right of the people to be secure in
... their persons, houses, papers, and effects, against
... unreasonable searches and seizures, shall not be violated,''')
>>> s
Sentence('The right of... be violated,')
>>> list(s) # doctest: +ELLIPSIS
['The', 'right', 'of', 'the', 'people', ... 'not', 'be', 'violated']
>>> s = Sentence('Agora vou-me. Ou me vão?')
>>> s
Sentence('Agora vou-me. Ou me vão?')
>>> list(s)
['Agora', 'vou', 'me', 'Ou', 'me', 'vão']