Browse Source

String, Duck types, Enum

pull/188/head
Jure Šorn 7 months ago
parent
commit
cc63249eee
2 changed files with 12 additions and 10 deletions
  1. 9
      README.md
  2. 13
      index.html

9
README.md

@ -319,7 +319,7 @@ String
<bool> = <sub_str> in <str> # Checks if string contains the substring.
<bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
<int> = <str>.find(<sub_str>) # Returns start index of the first match or -1.
<int> = <str>.index(<sub_str>) # Same, but raises ValueError if missing.
<int> = <str>.index(<sub_str>) # Same, but raises ValueError if there's no match.
```
```python
@ -1136,7 +1136,7 @@ class MyHashable:
* **With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.**
* **Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.**
* **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.**
* **For proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.**
* **To sort collection of strings in proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.**
```python
from functools import total_ordering
@ -1184,7 +1184,8 @@ class Counter:
### Callable
* **All functions and classes have a call() method, hence are callable.**
* **When this cheatsheet uses `'<function>'` as an argument, it actually means `'<callable>'`.**
* **To check if object is callable use `'callable(<obj>)'`, `'isinstance(<obj>, collections.abc.Callable)'`, or `'isinstance(<obj>, typing.Callable)'`.**
* **When this cheatsheet uses `'<function>'` as an argument, it means `'<callable>'`.**
```python
class Counter:
def __init__(self):
@ -1337,7 +1338,7 @@ from enum import Enum, auto
class <enum_name>(Enum):
<member_name> = auto() # Increment of the last numeric value or 1.
<member_name> = <value> # Values don't have to be hashable.
<member_name> = <value>, <value> # Values can be collections (like this tuple).
<member_name> = <el_1>, <el_2> # Values can be collections (this is a tuple).
```
* **Methods receive the member they were called on as the 'self' argument.**
* **Accessing a member named after a reserved keyword causes SyntaxError.**

13
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>July 7, 2024</aside>
<aside>July 11, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -305,7 +305,7 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;sub_str&gt; <span class="hljs-keyword">in</span> &lt;str&gt; <span class="hljs-comment"># Checks if string contains the substring.</span>
&lt;bool&gt; = &lt;str&gt;.startswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of the first match or -1.</span>
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same, but raises ValueError if missing.</span>
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same, but raises ValueError if there's no match.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.lower() <span class="hljs-comment"># Changes the case. Also upper/capitalize/title().</span>
&lt;str&gt; = &lt;str&gt;.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
@ -956,7 +956,7 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<li><strong>With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.</strong></li>
<li><strong>Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.</strong></li>
<li><strong>When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.</strong></li>
<li><strong>For proper alphabetical order pass <code class="python hljs"><span class="hljs-string">'key=locale.strxfrm'</span></code> to sorted() after running <code class="python hljs"><span class="hljs-string">'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'</span></code>.</strong></li>
<li><strong>To sort collection of strings in proper alphabetical order pass <code class="python hljs"><span class="hljs-string">'key=locale.strxfrm'</span></code> to sorted() after running <code class="python hljs"><span class="hljs-string">'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'</span></code>.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> total_ordering
<span class="hljs-meta">@total_ordering</span>
@ -1000,7 +1000,8 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<li><strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong></li>
</ul><div><h3 id="callable">Callable</h3><ul>
<li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
<li><strong>When this cheatsheet uses <code class="python hljs"><span class="hljs-string">'&lt;function&gt;'</span></code> as an argument, it actually means <code class="python hljs"><span class="hljs-string">'&lt;callable&gt;'</span></code>.</strong></li>
<li><strong>To check if object is callable use <code class="python hljs"><span class="hljs-string">'callable(&lt;obj&gt;)'</span></code>, <code class="python hljs"><span class="hljs-string">'isinstance(&lt;obj&gt;, collections.abc.Callable)'</span></code>, or <code class="python hljs"><span class="hljs-string">'isinstance(&lt;obj&gt;, typing.Callable)'</span></code>.</strong></li>
<li><strong>When this cheatsheet uses <code class="python hljs"><span class="hljs-string">'&lt;function&gt;'</span></code> as an argument, it means <code class="python hljs"><span class="hljs-string">'&lt;callable&gt;'</span></code>.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
self.i = <span class="hljs-number">0</span>
@ -1142,7 +1143,7 @@ Hello World!
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">enum_name</span>&gt;<span class="hljs-params">(Enum)</span>:</span>
&lt;member_name&gt; = auto() <span class="hljs-comment"># Increment of the last numeric value or 1.</span>
&lt;member_name&gt; = &lt;value&gt; <span class="hljs-comment"># Values don't have to be hashable.</span>
&lt;member_name&gt; = &lt;value&gt;, &lt;value&gt; <span class="hljs-comment"># Values can be collections (like this tuple).</span>
&lt;member_name&gt; = &lt;el_1&gt;, &lt;el_2&gt; <span class="hljs-comment"># Values can be collections (this is a tuple).</span>
</code></pre>
<ul>
<li><strong>Methods receive the member they were called on as the 'self' argument.</strong></li>
@ -2931,7 +2932,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer>
<aside>July 7, 2024</aside>
<aside>July 11, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save