Skip to content

Commit 72b0d58

Browse files
cleaned up and updated the testing page(s), including advanced testings (optional for course 1)
1 parent 3370908 commit 72b0d58

13 files changed

Lines changed: 1221 additions & 147 deletions

‎source/examples/testing/test_random_pytest.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_shuffle():
3333
"""
3434
seq = list(range(10))
3535
random.shuffle(seq)
36-
# seq.sort() # If you comment this out, it will fail, so you can see output
36+
seq.sort() # If you comment this out, it will fail, so you can see output
3737
print("seq:", seq) # only see output if it fails
3838
assert seq == list(range(10))
3939

@@ -45,6 +45,7 @@ def test_shuffle_immutable():
4545
with pytest.raises(TypeError):
4646
random.shuffle((1, 2, 3))
4747

48+
4849
def test_sample_too_large():
4950
"""
5051
Trying to sample more than exist should raise an error

‎source/examples/wikidef.zip‎

10.6 KB
Binary file not shown.

‎source/examples/wikidef/api.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Some code for accessing the Wikipedia API
5+
"""
6+
7+
# Really handy third-party module
8+
# ``pip install requests`` if you don't already have it
9+
import requests
10+
11+
12+
class ParseError(Exception):
13+
pass
14+
15+
16+
class MissingArticleError(Exception):
17+
pass
18+
19+
20+
class Wikipedia:
21+
"""
22+
Wikipedia API interface
23+
24+
https://www.mediawiki.org/wiki/API:Main_page
25+
"""
26+
27+
# base url for the english edition of wikipedia
28+
api_endpoint = "http://en.wikipedia.org/w/api.php?"
29+
30+
@classmethod
31+
def get_article(cls, title):
32+
"""
33+
Return contents of article
34+
35+
:param title: title of article
36+
"""
37+
req_params = {'action': 'parse',
38+
'format': 'json',
39+
'prop': 'text',
40+
'page': title}
41+
response = requests.get(cls.api_endpoint, params=req_params)
42+
json_response = response.json()
43+
44+
if "error" in json_response:
45+
print(json_response)
46+
raise MissingArticleError(str(json_response["error"]["info"]))
47+
else:
48+
try:
49+
# limit the output, cause sometimes it is obnoxious
50+
contents = json_response['parse']['text']['*'][:1000]
51+
return contents
52+
except KeyError:
53+
raise ParseError(json_response)

‎source/examples/wikidef/define.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Script to contact Wikipedia and get articles on a specified topic.
5+
python define.py interesting_topic
6+
"""
7+
8+
import sys
9+
from definitions import Definitions
10+
from html2text import html2text
11+
12+
title = len(sys.argv) == 2 and sys.argv[1] or ""
13+
14+
definition = Definitions.article(title)
15+
txt = html2text(definition)
16+
print(txt)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from api import Wikipedia
2+
3+
4+
class Definitions(object):
5+
6+
@staticmethod
7+
def article(title):
8+
return Wikipedia.get_article(title)

0 commit comments

Comments
 (0)