Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed0b0b3

Browse files
committed
Class, Scraping
1 parent 82c90ea commit ed0b0b3

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ class <class_name>:
10431043
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
10441044
```
10451045
* **Objects can be made sortable with `'order=True'` and immutable with `'frozen=True'`.**
1046-
* **For object to be hashable, all attributes must be [hashable](#hashable) and frozen must be True.**
1046+
* **For object to be hashable, all attributes must be hashable and 'frozen' must be True.**
10471047
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances. Its 'default_factory' argument can be any [callable](#callable).**
10481048
* **For attributes of arbitrary type use `'typing.Any'`.**
10491049

@@ -2475,7 +2475,7 @@ Scraping
24752475
#### Scrapes Python's URL, version number and logo from its Wikipedia page:
24762476
```python
24772477
# $ pip3 install requests beautifulsoup4
2478-
import requests, bs4, sys
2478+
import requests, bs4, os, sys
24792479

24802480
WIKI_URL = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
24812481
try:
@@ -2486,9 +2486,10 @@ try:
24862486
version = table.find('th', text='Stable release').next_sibling.strings.__next__()
24872487
logo_url = table.find('img')['src']
24882488
logo = requests.get(f'https:{logo_url}').content
2489-
with open('test.png', 'wb') as file:
2489+
filename = os.path.basename(logo_url)
2490+
with open(filename, 'wb') as file:
24902491
file.write(logo)
2491-
print(python_url, version)
2492+
print(f'{python_url}, {version}, file://{os.path.abspath(filename)}')
24922493
except requests.exceptions.ConnectionError:
24932494
print("You've got problems with connection.", file=sys.stderr)
24942495
```

index.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>January 27, 2022</aside>
57+
<aside>January 29, 2022</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -886,7 +886,7 @@
886886

887887
<ul>
888888
<li><strong>Objects can be made sortable with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li>
889-
<li><strong>For object to be hashable, all attributes must be <a href="#hashable">hashable</a> and frozen must be True.</strong></li>
889+
<li><strong>For object to be hashable, all attributes must be hashable and 'frozen' must be True.</strong></li>
890890
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li>
891891
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
892892
</ul>
@@ -2023,7 +2023,7 @@
20232023
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;str&gt;'</span></code> - Max age as a string: <code class="python hljs"><span class="hljs-string">'1 week, 3 days'</span></code>, <code class="python hljs"><span class="hljs-string">'2 months'</span></code>, …</strong></li>
20242024
</ul>
20252025
<div><h2 id="scraping"><a href="#scraping" name="scraping">#</a>Scraping</h2><div><h4 id="scrapespythonsurlversionnumberandlogofromitswikipediapage">Scrapes Python's URL, version number and logo from its Wikipedia page:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests beautifulsoup4</span>
2026-
<span class="hljs-keyword">import</span> requests, bs4, sys
2026+
<span class="hljs-keyword">import</span> requests, bs4, os, sys
20272027

20282028
WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span>
20292029
<span class="hljs-keyword">try</span>:
@@ -2034,9 +2034,10 @@
20342034
version = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Stable release'</span>).next_sibling.strings.__next__()
20352035
logo_url = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>]
20362036
logo = requests.get(<span class="hljs-string">f'https:<span class="hljs-subst">{logo_url}</span>'</span>).content
2037-
<span class="hljs-keyword">with</span> open(<span class="hljs-string">'test.png'</span>, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
2037+
filename = os.path.basename(logo_url)
2038+
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
20382039
file.write(logo)
2039-
print(python_url, version)
2040+
print(<span class="hljs-string">f'<span class="hljs-subst">{python_url}</span>, <span class="hljs-subst">{version}</span>, file://<span class="hljs-subst">{os.path.abspath(filename)}</span>'</span>)
20402041
<span class="hljs-keyword">except</span> requests.exceptions.ConnectionError:
20412042
print(<span class="hljs-string">"You've got problems with connection."</span>, file=sys.stderr)
20422043
</code></pre></div></div>
@@ -2881,7 +2882,7 @@
28812882

28822883

28832884
<footer>
2884-
<aside>January 27, 2022</aside>
2885+
<aside>January 29, 2022</aside>
28852886
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
28862887
</footer>
28872888

pdf/remove_links.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
'<strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong>': '<strong>Module \'operator\' (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions (p. 11) above.</strong>',
1212
'<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong>': '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its repr() method (p. 14).</strong>',
1313
'<strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong>': '<strong>It can be any callable (p. 17), but is usually implemented as a function that returns a closure (p. 12).</strong>',
14-
'<strong>For object to be hashable, all attributes must be <a href="#hashable">hashable</a> and frozen must be True.</strong>': '<strong>For object to be hashable, all attributes must be hashable (p. 16) and frozen must be True.</strong>',
1514
'<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any <a href="#callable">callable</a>.</strong>': '<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any callable (p. 17).</strong>',
1615
'<strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong>': '<strong>Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p.&nbsp;3).</strong>',
1716
'<strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong>': '<strong>Objects returned by the itertools module, such as count, repeat and cycle (p. 3).</strong>',

0 commit comments

Comments
 (0)