|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>January 27, 2022</aside> |
| 57 | + <aside>January 29, 2022</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
886 | 886 |
|
887 | 887 | <ul>
|
888 | 888 | <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> |
890 | 890 | <li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'<attr_name>: 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>
|
891 | 891 | <li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
|
892 | 892 | </ul>
|
|
2023 | 2023 | <li><strong><code class="python hljs"><span class="hljs-string">'<str>'</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>
|
2024 | 2024 | </ul>
|
2025 | 2025 | <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 |
2027 | 2027 |
|
2028 | 2028 | WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span>
|
2029 | 2029 | <span class="hljs-keyword">try</span>:
|
|
2034 | 2034 | version = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Stable release'</span>).next_sibling.strings.__next__()
|
2035 | 2035 | logo_url = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>]
|
2036 | 2036 | 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: |
2038 | 2039 | 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>) |
2040 | 2041 | <span class="hljs-keyword">except</span> requests.exceptions.ConnectionError:
|
2041 | 2042 | print(<span class="hljs-string">"You've got problems with connection."</span>, file=sys.stderr)
|
2042 | 2043 | </code></pre></div></div>
|
|
2881 | 2882 |
|
2882 | 2883 |
|
2883 | 2884 | <footer>
|
2884 |
| - <aside>January 27, 2022</aside> |
| 2885 | + <aside>January 29, 2022</aside> |
2885 | 2886 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2886 | 2887 | </footer>
|
2887 | 2888 |
|
|
0 commit comments