Skip to content

Commit

Permalink
deploy: 74452b0
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Aug 10, 2024
1 parent ce92b31 commit 6bb2fca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
39 changes: 39 additions & 0 deletions _sources/book/pythontricks/utility.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,45 @@
"my_func()\n",
"# UnsupportedWarning: my_func is unsupported as of 1.0. Use my_func2 instead"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Case Insensitive Dictionaries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Are you annoyed by case-sensitive dictionaries in Python?\n",
"\n",
"Try 𝐂𝐚𝐬𝐞𝐈𝐧𝐬𝐞𝐧𝐬𝐢𝐭𝐢𝐯𝐞𝐃𝐢𝐜𝐭𝐬.\n",
"\n",
"As the name says, you can access values by the key without paying attention to lowercase or uppercase.\n",
"\n",
"It's useful for everyone who is scraping API data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from requests.structures import CaseInsensitiveDict\n",
"\n",
"data: dict[str, str | int] = CaseInsensitiveDict(\n",
" {\n",
" \"accept\": \"application/json\",\n",
" \"content-type\": \"application/json\",\n",
" \"User-Agent\": \"Mozilla/5.0\",\n",
" }\n",
")\n",
"\n",
"print(data.get(\"Accept\"))"
]
}
],
"metadata": {
Expand Down
26 changes: 26 additions & 0 deletions book/pythontricks/utility.html
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#powerful-dictionaries-with-python-benedict">10.2.6. Powerful Dictionaries with <code class="docutils literal notranslate"><span class="pre">python-benedict</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#clear-exception-output-with-pretty-errors">10.2.7. Clear Exception Output with <code class="docutils literal notranslate"><span class="pre">pretty_errors</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#deprecate-functions-with-deprecated">10.2.8. Deprecate Functions with <code class="docutils literal notranslate"><span class="pre">deprecated</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#case-insensitive-dictionaries">10.2.9. Case Insensitive Dictionaries</a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -670,6 +671,30 @@ <h2><span class="section-number">10.2.8. </span>Deprecate Functions with <code c
</div>
</div>
</section>
<section id="case-insensitive-dictionaries">
<h2><span class="section-number">10.2.9. </span>Case Insensitive Dictionaries<a class="headerlink" href="#case-insensitive-dictionaries" title="Permalink to this heading">#</a></h2>
<p>Are you annoyed by case-sensitive dictionaries in Python?</p>
<p>Try 𝐂𝐚𝐬𝐞𝐈𝐧𝐬𝐞𝐧𝐬𝐢𝐭𝐢𝐯𝐞𝐃𝐢𝐜𝐭𝐬.</p>
<p>As the name says, you can access values by the key without paying attention to lowercase or uppercase.</p>
<p>It’s useful for everyone who is scraping API data.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">requests.structures</span> <span class="kn">import</span> <span class="n">CaseInsensitiveDict</span>

<span class="n">data</span><span class="p">:</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span> <span class="o">|</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="n">CaseInsensitiveDict</span><span class="p">(</span>
<span class="p">{</span>
<span class="s2">&quot;accept&quot;</span><span class="p">:</span> <span class="s2">&quot;application/json&quot;</span><span class="p">,</span>
<span class="s2">&quot;content-type&quot;</span><span class="p">:</span> <span class="s2">&quot;application/json&quot;</span><span class="p">,</span>
<span class="s2">&quot;User-Agent&quot;</span><span class="p">:</span> <span class="s2">&quot;Mozilla/5.0&quot;</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;Accept&quot;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -747,6 +772,7 @@ <h2><span class="section-number">10.2.8. </span>Deprecate Functions with <code c
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#powerful-dictionaries-with-python-benedict">10.2.6. Powerful Dictionaries with <code class="docutils literal notranslate"><span class="pre">python-benedict</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#clear-exception-output-with-pretty-errors">10.2.7. Clear Exception Output with <code class="docutils literal notranslate"><span class="pre">pretty_errors</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#deprecate-functions-with-deprecated">10.2.8. Deprecate Functions with <code class="docutils literal notranslate"><span class="pre">deprecated</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#case-insensitive-dictionaries">10.2.9. Case Insensitive Dictionaries</a></li>
</ul>
</nav></div>

Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

0 comments on commit 6bb2fca

Please sign in to comment.