Skip to content

Commit 07ee276

Browse files
3.2.0: More typing hint support
1 parent 650cbe7 commit 07ee276

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ client = Client()
3535
results = client.search("Python Wikipedia")
3636

3737
# Prints first result title
38-
print(results[0]["title"])
38+
print(results[0].title)
3939

4040
# Prints first result URL
41-
print(results[0]["url"])
41+
print(results[0].url)
4242

4343
# Prints first result description
44-
print(results[0]["description"])
44+
print(results[0].description)
4545
```
4646

4747
### We also provide an asynchronous version inside the `AsyncClient` class
@@ -56,13 +56,13 @@ async def get_results():
5656
results = await client.search("Python Wikipedia")
5757

5858
# Prints first result title
59-
print(results[0]["title"])
59+
print(results[0].title)
6060

6161
# Prints first result URL
62-
print(results[0]["url"])
62+
print(results[0].url)
6363

6464
# Prints first result description
65-
print(results[0]["description"])
65+
print(results[0].description)
6666

6767

6868
loop = asyncio.get_event_loop()

duckpy/__init__.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import random
33
import httpx
4+
from .types import ResultDict
5+
from typing import List
46
from warnings import warn
57
from typing import Union
68
from bs4 import BeautifulSoup
@@ -19,7 +21,7 @@ def __init__(self, proxies: Union[list, str] = None, default_user_agents: Union[
1921

2022

2123
class Client(BaseClient):
22-
def search(self, query: str, exact_match: bool = False, **kwargs):
24+
def search(self, query: str, exact_match: bool = False, **kwargs) -> List[ResultDict]:
2325
if exact_match:
2426
query = '"%s"' % query
2527

@@ -49,7 +51,7 @@ def __init__(self, proxies: Union[list, str] = None, default_user_agents: Union[
4951
self.loop = asyncio.get_event_loop()
5052
super().__init__(proxies=proxies, default_user_agents=default_user_agents, random_ua=random_ua)
5153

52-
async def search(self, query: str, exact_match: bool = False, **kwargs):
54+
async def search(self, query: str, exact_match: bool = False, **kwargs) -> List[ResultDict]:
5355
if exact_match:
5456
query = '"%s"' % query
5557

@@ -74,7 +76,7 @@ async def search(self, query: str, exact_match: bool = False, **kwargs):
7476
return await self.loop.run_in_executor(None, parse_page, data)
7577

7678

77-
def parse_page(html: Union[str, bytes]):
79+
def parse_page(html: Union[str, bytes]) -> List[ResultDict]:
7880
soup = BeautifulSoup(html, "html.parser")
7981
results = []
8082
for i in soup.find_all('div', {'class': 'links_main'}):
@@ -84,7 +86,7 @@ def parse_page(html: Union[str, bytes]):
8486
title = i.h2.a.text
8587
description = i.find('a', {'class': 'result__snippet'}).text
8688
url = i.find('a', {'class': 'result__url'}).get('href')
87-
results.append(dict(title=title, description=description, url=url))
89+
results.append(ResultDict(title=title, description=description, url=url))
8890
except AttributeError:
8991
pass
9092
return results

duckpy/types.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ResultDict(dict):
2+
def __init__(self, title: str, description: str, url: str):
3+
self.title = title
4+
self.description = description
5+
self.url = url
6+
super().__init__(title=title, description=description, url=url)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='duckpy',
8-
version='3.1.0',
8+
version='3.2.0-dev',
99

1010
packages=['duckpy', 'duckpy.aio'],
1111
install_requires=['beautifulsoup4>=4.9.1', 'httpx[http2]==0.14.*'],

0 commit comments

Comments
 (0)