Skip to content

Commit 9a163f8

Browse files
committedOct 1, 2020
typing for when gazpacho 1.x.x is released
1 parent 0a69814 commit 9a163f8

File tree

6 files changed

+51
-37
lines changed

6 files changed

+51
-37
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# custom
22
sandbox.py
3+
*.sqlite3
4+
out
35

46
# Byte-compiled / optimized / DLL files
57
__pycache__/

‎quote/cli.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
import itertools
33
import random
44
import threading
5+
from textwrap import shorten, wrap
56
import time
67

78
from .quote import quote
89
from .spinner import Spinner
910

10-
from textwrap import shorten, wrap
11-
1211

13-
def random_search(search):
14-
"""Return one random quote result from a search
12+
def random_search(search: str) -> str:
13+
"""\
14+
Return one random quote result from a search
1515
1616
Params:
1717
18-
- search (str): Search term
18+
- search: term
1919
"""
2020
results = quote(search)
2121
random_quote = random.choice(results)["quote"]
@@ -25,14 +25,17 @@ def random_search(search):
2525
return wrapped_quote
2626

2727

28-
def colour(string):
29-
"""Paint it green!"""
28+
def colour(string: str) -> str:
29+
"""\
30+
Paint it green!
31+
"""
3032
string = f"\033[32m{string}\033[0m"
3133
return string
3234

3335

34-
def cli():
35-
"""Retrieve a random quote from Goodreads
36+
def cli() -> str:
37+
"""\
38+
Retrieve a random quote from Goodreads
3639
3740
Examples:
3841

‎quote/py.typed

Whitespace-only changes.

‎quote/quote.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
import re
2-
from gazpacho import get, Soup
2+
from typing import Dict, List
3+
4+
from gazpacho import Soup
5+
36

47
URL = "https://www.goodreads.com/quotes/search"
58

69

7-
def _make_soup(query, page=1):
10+
def _make_soup(query: str, page: int = 1) -> Soup:
811
params = {"q": query, "commit": "Search", "page": page}
9-
html = get(URL, params)
10-
soup = Soup(html)
12+
soup = Soup.get(URL, params)
1113
return soup
1214

1315

14-
def _parse_quote(quote_text):
15-
try:
16-
book = quote_text.find("a", {"class": "authorOrTitle"}).text
17-
except AttributeError:
18-
book = None
19-
author = quote_text.find("span", {"class": "authorOrTitle"}).text.replace(",", "")
20-
quote = re.search("(?<=“)(.*?)(?=”)", quote_text.strip()).group(0)
21-
return {"author": author, "book": book, "quote": quote}
16+
def _parse_quote(quote_text: Soup) -> Dict[str, str]:
17+
b = quote_text.find("a", {"class": "authorOrTitle"}, mode="first")
18+
a = quote_text.find("span", {"class": "authorOrTitle"}, mode="first")
19+
q = re.search("(?<=“)(.*?)(?=”)", quote_text.strip())
20+
return {
21+
"author": "" if not isinstance(a, Soup) else a.text.replace(",", ""),
22+
"book": "" if not isinstance(b, Soup) else b.text,
23+
"quote": "" if not q else q.group(0),
24+
}
2225

2326

24-
def _get_page_quotes(soup):
27+
def _get_page_quotes(soup: Soup) -> List[Dict[str, str]]:
2528
quotes = []
26-
for quote_text in soup.find("div", {"class": "quoteText"}):
29+
quote_texts = soup.find("div", {"class": "quoteText"}, mode="all")
30+
assert isinstance(quote_texts, list)
31+
for quote_text in quote_texts:
2732
quote = _parse_quote(quote_text)
2833
quotes.append(quote)
2934
return quotes
3035

3136

32-
def quote(search, limit=20):
33-
"""Retrieve quotes from Goodreads based on a search term
37+
def quote(search: str, limit: int = 20) -> List[Dict[str, str]]:
38+
"""\
39+
Retrieve quotes from Goodreads
3440
3541
Params:
3642
37-
- search (str): Author or book to search for
38-
- limit (int, default=20): Number of quotes to return
43+
- search: Author and/or book
44+
- limit: Number of quotes to return
3945
4046
Example:
4147
@@ -52,7 +58,7 @@ def quote(search, limit=20):
5258
```
5359
"""
5460
page = 1
55-
quotes = []
61+
quotes: List[Dict[str, str]] = []
5662
while True:
5763
if len(quotes) > limit:
5864
break

‎quote/spinner.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22
import sys
33
from threading import Event, Thread
44
from time import sleep
5+
from typing import Any
56

67

78
class Spinner:
8-
phases = cycle(["⣾", "⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽"])
9+
phases: Any = cycle(["⣾", "⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽"])
910

10-
def __init__(self):
11-
self.stop_running = Event()
12-
self.spin_thread = Thread(target=self.init_spin)
11+
def __init__(self) -> None:
12+
self.stop_running: Any = Event()
13+
self.spin_thread: Any = Thread(target=self.init_spin)
1314

14-
def start(self):
15+
def start(self) -> None:
1516
sys.stdout.write("\033[32m")
1617
self.spin_thread.start()
1718

18-
def stop(self):
19+
def stop(self) -> None:
1920
self.stop_running.set()
2021
self.spin_thread.join()
2122
sys.stdout.write("\033[0m")
2223

23-
def init_spin(self):
24+
def init_spin(self) -> None:
2425
while not self.stop_running.is_set():
2526
sys.stdout.write(next(self.phases))
2627
sys.stdout.flush()

‎setup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
setup(
77
name="quote",
8-
version="2.0.2",
8+
version="2.0.3",
99
author="Max Humber",
1010
author_email="max.humber@gmail.com",
11-
description="quote is a wrapper for the Goodreads Quote API",
11+
description="A wrapper for the Goodreads Quote API",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
url="https://github.com/maxhumber/quote",
1515
packages=find_packages(),
16+
package_data={"quote": ["py.typed"]},
1617
install_requires=["gazpacho>=1.0"],
1718
classifiers=[
1819
"Development Status :: 5 - Production/Stable",
@@ -24,4 +25,5 @@
2425
entry_points={"console_scripts": ["quote=quote.cli:cli"]},
2526
python_requires=">=3.7",
2627
setup_requires=["setuptools>=38.6.0"],
28+
zip_safe=False,
2729
)

0 commit comments

Comments
 (0)
Please sign in to comment.