Skip to content

Commit 3fd75bc

Browse files
committed
Make code compatible with legacy Python
1 parent 12aa846 commit 3fd75bc

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

reader/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Usage:
44
------
55
6-
$ realpython [options] [<id>]
6+
$ realpython [options] [id] [id ...]
77
88
List the latest tutorials:
99
@@ -44,7 +44,7 @@
4444
from reader import viewer
4545

4646

47-
def main() -> None:
47+
def main(): # type: () -> None
4848
"""Read the Real Python article feed"""
4949
args = [a for a in sys.argv[1:] if not a.startswith("-")]
5050
opts = [o for o in sys.argv[1:] if o.startswith("-")]
@@ -55,7 +55,7 @@ def main() -> None:
5555
return
5656

5757
# Should links be shown in the text
58-
show_links = ("-l" in opts or "--show-links" in opts)
58+
show_links = "-l" in opts or "--show-links" in opts
5959

6060
# An article ID is given, show article
6161
if args:

reader/feed.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
"""Interact with the Real Python feed"""
22
# Standard library imports
3-
from typing import Dict, List
3+
from typing import Dict, List # noqa
44

55
# Third party imports
66
import feedparser
77
import html2text
88

99
# Reader imports
1010
from reader import URL
11-
_CACHED_FEEDS: Dict[str, feedparser.FeedParserDict] = dict()
1211

12+
_CACHED_FEEDS = dict() # type: Dict[str, feedparser.FeedParserDict]
1313

14-
def _feed() -> feedparser.FeedParserDict:
14+
15+
def _feed(): # type: () -> feedparser.FeedParserDict
1516
"""Cache contents of the feed, so it's only read once"""
1617
if URL not in _CACHED_FEEDS:
1718
_CACHED_FEEDS[URL] = feedparser.parse(URL)
1819
return _CACHED_FEEDS[URL]
1920

2021

21-
def get_site() -> str:
22+
def get_site(): # type: () -> str
2223
"""Get name and link to web site of the feed"""
2324
info = _feed().feed
24-
return f"{info.title} ({info.link})"
25+
return "{info.title} ({info.link})".format(info=info)
2526

2627

27-
def get_article(article_id: str, links: bool = False) -> str:
28+
def get_article(article_id, links=False): # type: (str, bool) -> str
2829
"""Get article from feed with the given ID"""
2930
articles = _feed().entries
3031
try:
3132
article = articles[int(article_id)]
3233
except (IndexError, ValueError):
3334
max_id = len(articles) - 1
34-
msg = f"Unknown article ID, use ID from 0 to {max_id}"
35-
raise SystemExit(f"Error: {msg}")
35+
msg = "Unknown article ID, use ID from 0 to {}".format(max_id)
36+
raise SystemExit("Error: {}".format(msg))
3637

3738
# Get article as HTML
3839
try:
@@ -45,10 +46,10 @@ def get_article(article_id: str, links: bool = False) -> str:
4546
to_text.ignore_links = not links
4647
text = to_text.handle(html)
4748

48-
return f"# {article.title}\n\n{text}"
49+
return u"# {}\n\n{}".format(article.title, text)
4950

5051

51-
def get_titles() -> List[str]:
52+
def get_titles(): # type: () -> List[str]
5253
"""List titles in feed"""
5354
articles = _feed().entries
5455
return [a.title for a in articles]

reader/viewer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
"""Functions for displaying the Real Python feed"""
22

3+
# Support Python 2
4+
from __future__ import print_function
5+
36
# Standard library imports
4-
from typing import List
7+
from typing import List # noqa
58

69

7-
def show(article: str) -> None:
10+
def show(article): # type: (str) -> None
811
"""Show one article"""
912
print(article)
1013

1114

12-
def show_list(site: str, titles: List[str]) -> None:
15+
def show_list(site, titles): # type: (str, List[str]) -> None
1316
"""Show list of articles"""
14-
print(f"The latest tutorials from {site}")
17+
print("The latest tutorials from {}".format(site))
1518
for article_id, title in enumerate(titles):
16-
print(f"{article_id:>3} {title}")
19+
print("{:>3} {}".format(article_id, title))

setup.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@
2424
classifiers=[
2525
"License :: OSI Approved :: MIT License",
2626
"Programming Language :: Python",
27+
"Programming Language :: Python :: 2",
2728
"Programming Language :: Python :: 3",
2829
],
2930
packages=["reader"],
30-
install_requires=["feedparser", "html2text"],
31-
entry_points={
32-
"console_scripts": [
33-
"realpython=reader.__main__:main",
34-
]
35-
},
31+
install_requires=["feedparser", "html2text", "typing"],
32+
entry_points={"console_scripts": ["realpython=reader.__main__:main"]},
3633
)

tests/test_feed.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for the reader.feed module"""
22
# Standard library imports
3-
import pathlib
3+
import os.path
44

55
# Third party imports
66
import pytest
@@ -9,21 +9,21 @@
99
from reader import feed
1010

1111
# Current directory
12-
HERE = pathlib.Path(__file__).parent
12+
HERE = os.path.dirname(__file__)
1313

1414

1515
@pytest.fixture
1616
def monkeypatch_feed(monkeypatch):
1717
"""Use local file instead of downloading feed from web"""
18-
local_path = HERE / "realpython_20180919.xml"
18+
local_path = os.path.join(HERE, "realpython_20180919.xml")
1919
monkeypatch.setattr(feed, "URL", local_path)
2020
return local_path
2121

2222

2323
@pytest.fixture
2424
def monkeypatch_summary_feed(monkeypatch):
2525
"""Use local file instead of downloading feed from web"""
26-
local_path = HERE / "realpython_descriptions_20180919.xml"
26+
local_path = os.path.join(HERE, "realpython_descriptions_20180919.xml")
2727
monkeypatch.setattr(feed, "URL", local_path)
2828
return local_path
2929

tests/test_viewer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def test_show_list(capsys):
3030
assert stderr == ""
3131

3232
# Site name is shown in header
33-
header, *lines = stdout.split("\n")
34-
assert site in header
33+
lines = stdout.split("\n")
34+
assert site in lines[0]
3535

3636
# Each thing is listed preceded by a number
37-
for thing, line in zip(things, lines):
38-
line_id, *_ = line.split()
39-
assert line_id.isnumeric()
37+
for thing, line in zip(things, lines[1:]):
38+
line_parts = line.split()
39+
assert line_parts[0].isnumeric()
4040
assert thing in line

0 commit comments

Comments
 (0)