Skip to content

Commit

Permalink
update docs/docstrings to consistently use double-quotes (#61)
Browse files Browse the repository at this point in the history
* update docs/docstrings to consistently use double-quotes

* revert quotes on html example
  • Loading branch information
BurnzZ authored Aug 8, 2022
1 parent b539873 commit 554bbfa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 68 deletions.
10 changes: 5 additions & 5 deletions docs/advanced/retries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ supplies to your page object, your page object must raise
class MyPage(ItemWebPage):
def to_item(self) -> dict:
if not self.css('.expected'):
if not self.css(".expected"):
raise Retry
return {}
Expand Down Expand Up @@ -70,18 +70,18 @@ times before giving up:
@retry(stop=stop_after_attempt(3))
async def get_data(self):
request = HttpRequest('https://toscrape.com/')
request = HttpRequest("https://toscrape.com/")
response = await self.http_client.execute(request)
if not response.css('.expected'):
if not response.css(".expected"):
raise ValueError
return response.css('.data').get()
return response.css(".data").get()
async def to_item(self) -> dict:
try:
data = await self.get_data()
except ValueError:
return {}
return {'data': data}
return {"data": data}
If the reason your additional request fails is outdated or missing data from
page object input, do not try to reproduce the request for that input as an
Expand Down
72 changes: 36 additions & 36 deletions docs/intro/from-ground-up.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ a book web page from `books.toscrape.com <http://books.toscrape.com/>`_:
resp = requests.get(url)
sel = parsel.Selector(resp)
return {
'url': resp.url,
'title': sel.css('h1').get(),
'description': sel.css('#product_description+ p').get().strip(),
"url": resp.url,
"title": sel.css("h1").get(),
"description": sel.css("#product_description+ p").get().strip(),
# ...
}
Expand All @@ -57,9 +57,9 @@ from the extraction:
"""
sel = parsel.Selector(response.text)
return {
'url': response.url,
'title': sel.css('h1').get(),
'description': sel.css('#product_description+ p').get().strip(),
"url": response.url,
"title": sel.css("h1").get(),
"description": sel.css("#product_description+ p").get().strip(),
# ...
}
Expand Down Expand Up @@ -102,29 +102,29 @@ No problem, let's refactor it further. You may end up with something like that:
"""
sel = parsel.Selector(text)
return {
'url': url,
'title': sel.css('h1').get(),
'description': sel.css('#product_description+ p').get().strip(),
"url": url,
"title": sel.css("h1").get(),
"description": sel.css("#product_description+ p").get().strip(),
# ...
}
# === Framework-specific I/O code
def download_sync(url):
resp = requests.get(url)
return {'url': resp.url, 'text': resp.text}
return {"url": resp.url, "text": resp.text}
async def download_async(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
text = await response.text()
return {'url': url, 'text': text}
return {"url": url, "text": text}
# === Usage example
# the way to get resp_data depends on an HTTP client
resp_data = download_sync("http://books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html")
# but after we got resp_data, usage is the same
item = extract_book(url=resp_data['url'], text=resp_data['text'])
item = extract_book(url=resp_data["url"], text=resp_data["text"])
``extract_book`` function now has all the desired properties: it is
Expand Down Expand Up @@ -152,9 +152,9 @@ The same, but using web-poet
"""
def extract_book(self):
return {
'url': self.url,
'title': self.css('h1').get(),
'description': self.css('#product_description+ p').get().strip(),
"url": self.url,
"title": self.css("h1").get(),
"description": self.css("#product_description+ p").get().strip(),
# ...
}
Expand Down Expand Up @@ -233,9 +233,9 @@ is implemented. Let's change the code to follow this standard:
"""
def to_item(self):
return {
'url': self.url,
'title': self.css('h1').get(),
'description': self.css('#product_description+ p').get().strip(),
"url": self.url,
"title": self.css("h1").get(),
"description": self.css("#product_description+ p").get().strip(),
# ...
}
Expand Down Expand Up @@ -283,17 +283,17 @@ For example, we can extract logic for different attributes into properties:
@property
def title(self):
return self.css('h1').get()
return self.css("h1").get()
@property
def description(self):
return self.css('#product_description+ p').get().strip()
return self.css("#product_description+ p").get().strip()
def to_item(self):
return {
'url': self.url,
'title': self.title,
'description': self.description,
"url": self.url,
"title": self.title,
"description": self.description,
# ...
}
Expand All @@ -314,8 +314,8 @@ nothing prevents us from having a DSL like this:
.. code-block:: python
class BookPage(ItemWebPage):
title = Css('h1')
description = Css('#product_description+ p') | Strip()
title = Css("h1")
description = Css("#product_description+ p") | Strip()
url = TakeUrl()
Another reason to consider classes for the extraction code is that sometimes
Expand Down Expand Up @@ -367,9 +367,9 @@ Let's recall the example we started with:
resp = requests.get(url)
sel = parsel.Selector(resp)
return {
'url': resp.url,
'title': sel.css('h1').get(),
'description': sel.css('#product_description+ p').get().strip(),
"url": resp.url,
"title": sel.css("h1").get(),
"description": sel.css("#product_description+ p").get().strip(),
# ...
}
Expand All @@ -390,9 +390,9 @@ And this is what we ended up with:
"""
def to_item(self):
return {
'url': self.url,
'title': self.css('h1').get(),
'description': self.css('#product_description+ p').get().strip(),
"url": self.url,
"title": self.css("h1").get(),
"description": self.css("#product_description+ p").get().strip(),
# ...
}
Expand Down Expand Up @@ -447,9 +447,9 @@ would only need to write the "extraction" part:
"""
def to_item(self):
return {
'url': self.url,
'title': self.css('h1').get(),
'description': self.css('#product_description+ p').get().strip(),
"url": self.url,
"title": self.css("h1").get(),
"description": self.css("#product_description+ p").get().strip(),
# ...
}
Expand Down Expand Up @@ -510,8 +510,8 @@ For example, a very basic Page Object could look like this:
def to_item(self) -> dict:
return {
'url': str(self.response.url),
'title': self.response.css("h1::text").get()
"url": str(self.response.url),
"title": self.response.css("h1::text").get()
}
There is no *need* to use other base classes and mixins
Expand Down
54 changes: 27 additions & 27 deletions docs/intro/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ list page on `books.toscrape.com <http://books.toscrape.com/>`_.
@property
def links(self):
return self.css('.image_container a::attr(href)').getall()
return self.css(".image_container a::attr(href)").getall()
def to_item(self) -> dict:
return {
'links': self.links,
"links": self.links,
}
Downloading Response
Expand All @@ -46,7 +46,7 @@ let's download the page using ``requests`` library.
import requests
response = requests.get('http://books.toscrape.com')
response = requests.get("http://books.toscrape.com")
Creating Page Input
===================
Expand Down Expand Up @@ -82,15 +82,15 @@ Our simple Python script might look like this:
@property
def links(self):
return self.css('.image_container a::attr(href)').getall()
return self.css(".image_container a::attr(href)").getall()
def to_item(self) -> dict:
return {
'links': self.links,
"links": self.links,
}
response = requests.get('http://books.toscrape.com')
response = requests.get("http://books.toscrape.com")
response_data = HttpResponse(response.url,
body=response.content,
headers=response.headers)
Expand All @@ -104,27 +104,27 @@ And it should output data similar to this:
.. code-block:: python
{
'links': [
'catalogue/a-light-in-the-attic_1000/index.html',
'catalogue/tipping-the-velvet_999/index.html',
'catalogue/soumission_998/index.html',
'catalogue/sharp-objects_997/index.html',
'catalogue/sapiens-a-brief-history-of-humankind_996/index.html',
'catalogue/the-requiem-red_995/index.html',
'catalogue/the-dirty-little-secrets-of-getting-your-dream-job_994/index.html',
'catalogue/the-coming-woman-a-novel-based-on-the-life-of-the-infamous-feminist-victoria-woodhull_993/index.html',
'catalogue/the-boys-in-the-boat-nine-americans-and-their-epic-quest-for-gold-at-the-1936-berlin-olympics_992/index.html',
'catalogue/the-black-maria_991/index.html',
'catalogue/starving-hearts-triangular-trade-trilogy-1_990/index.html',
'catalogue/shakespeares-sonnets_989/index.html',
'catalogue/set-me-free_988/index.html',
'catalogue/scott-pilgrims-precious-little-life-scott-pilgrim-1_987/index.html',
'catalogue/rip-it-up-and-start-again_986/index.html',
'catalogue/our-band-could-be-your-life-scenes-from-the-american-indie-underground-1981-1991_985/index.html',
'catalogue/olio_984/index.html',
'catalogue/mesaerion-the-best-science-fiction-stories-1800-1849_983/index.html',
'catalogue/libertarianism-for-beginners_982/index.html',
'catalogue/its-only-the-himalayas_981/index.html',
"links": [
"catalogue/a-light-in-the-attic_1000/index.html",
"catalogue/tipping-the-velvet_999/index.html",
"catalogue/soumission_998/index.html",
"catalogue/sharp-objects_997/index.html",
"catalogue/sapiens-a-brief-history-of-humankind_996/index.html",
"catalogue/the-requiem-red_995/index.html",
"catalogue/the-dirty-little-secrets-of-getting-your-dream-job_994/index.html",
"catalogue/the-coming-woman-a-novel-based-on-the-life-of-the-infamous-feminist-victoria-woodhull_993/index.html",
"catalogue/the-boys-in-the-boat-nine-americans-and-their-epic-quest-for-gold-at-the-1936-berlin-olympics_992/index.html",
"catalogue/the-black-maria_991/index.html",
"catalogue/starving-hearts-triangular-trade-trilogy-1_990/index.html",
"catalogue/shakespeares-sonnets_989/index.html",
"catalogue/set-me-free_988/index.html",
"catalogue/scott-pilgrims-precious-little-life-scott-pilgrim-1_987/index.html",
"catalogue/rip-it-up-and-start-again_986/index.html",
"catalogue/our-band-could-be-your-life-scenes-from-the-american-indie-underground-1981-1991_985/index.html",
"catalogue/olio_984/index.html",
"catalogue/mesaerion-the-best-science-fiction-stories-1800-1849_983/index.html",
"catalogue/libertarianism-for-beginners_982/index.html",
"catalogue/its-only-the-himalayas_981/index.html",
]
}
Expand Down

0 comments on commit 554bbfa

Please sign in to comment.