Browse Source

List, Format, Enum, Path, Bytes, Memoryview, Pandas

pull/187/head
Jure Šorn 10 months ago
parent
commit
937f5c1a85
2 changed files with 85 additions and 80 deletions
  1. 79
      README.md
  2. 86
      index.html

79
README.md

@ -29,6 +29,7 @@ if __name__ == '__main__': # Runs main() if file wasn't imported.
List List
---- ----
```python ```python
<el> = <list>[index] # First index is 0. Last -1. Allows assignments.
<list> = <list>[<slice>] # Or: <list>[from_inclusive : to_exclusive : ±step] <list> = <list>[<slice>] # Or: <list>[from_inclusive : to_exclusive : ±step]
``` ```
@ -57,10 +58,11 @@ list_of_chars = list(<str>)
* **Module [operator](#operator) provides functions itemgetter() and mul() that offer the same functionality as [lambda](#lambda) expressions above.** * **Module [operator](#operator) provides functions itemgetter() and mul() that offer the same functionality as [lambda](#lambda) expressions above.**
```python ```python
<list>.insert(<int>, <el>) # Inserts item at index and moves the rest to the right.
<el> = <list>.pop([<int>]) # Removes and returns item at index or from the end.
<int> = <list>.count(<el>) # Returns number of occurrences. Also works on strings.
<int> = len(<list>) # Returns number of items. Also works on other collections.
<int> = <list>.count(<el>) # Returns number of occurrences. Also `if <el> in <coll>: ...`.
<int> = <list>.index(<el>) # Returns index of the first occurrence or raises ValueError. <int> = <list>.index(<el>) # Returns index of the first occurrence or raises ValueError.
<el> = <list>.pop() # Removes and returns item from the end or at index if passed.
<list>.insert(<int>, <el>) # Inserts item at index and moves the rest to the right.
<list>.remove(<el>) # Removes first occurrence of the item or raises ValueError. <list>.remove(<el>) # Removes first occurrence of the item or raises ValueError.
<list>.clear() # Removes all items. Also works on dictionary and set. <list>.clear() # Removes all items. Also works on dictionary and set.
``` ```
@ -477,9 +479,9 @@ Format
### Ints ### Ints
```python ```python
{90:c} # 'Z'
{90:b} # '1011010'
{90:X} # '5A'
{90:c} # 'Z'. Unicode character with value 90.
{90:b} # '1011010'. Number 90 in binary.
{90:X} # '5A'. Number 90 in uppercase hexadecimal.
``` ```
@ -499,7 +501,7 @@ Numbers
### Basic Functions ### Basic Functions
```python ```python
<num> = pow(<num>, <num>) # Or: <num> ** <num>
<num> = pow(<num>, <num>) # Or: <number> ** <number>
<num> = abs(<num>) # <float> = abs(<complex>) <num> = abs(<num>) # <float> = abs(<complex>)
<num> = round(<num> [, ±ndigits]) # `round(126, -1) == 130` <num> = round(<num> [, ±ndigits]) # `round(126, -1) == 130`
``` ```
@ -605,8 +607,8 @@ from dateutil.tz import tzlocal, gettz
### Now ### Now
```python ```python
<D/DTn> = D/DT.today() # Current local date or naive DT. Also DT.now().
<DTa> = DT.now(<tzinfo>) # Aware DT from current time in passed timezone.
<D/DTn> = D/DT.today() # Current local date or naive DT. Also DT.now().
<DTa> = DT.now(<tzinfo>) # Aware DT from current time in passed timezone.
``` ```
* **To extract time use `'<DTn>.time()'`, `'<DTa>.time()'` or `'<DTa>.timetz()'`.** * **To extract time use `'<DTn>.time()'`, `'<DTa>.time()'` or `'<DTa>.timetz()'`.**
@ -1320,23 +1322,24 @@ class MyAbcSequence(abc.Sequence):
+------------+------------+------------+------------+--------------+ +------------+------------+------------+------------+--------------+
``` ```
* **Method iter() is required for `'isinstance(<obj>, abc.Iterable)'` to return True, however any object with getitem() will work with any code expecting an iterable.** * **Method iter() is required for `'isinstance(<obj>, abc.Iterable)'` to return True, however any object with getitem() will work with any code expecting an iterable.**
* **Abstract base classes that generate missing methods when extended are: Sequence, MutableSequence, Set, MutableSet, Mapping and MutableMapping.**
* **Other extendable ABCs: MutableSequence, Set, MutableSet, Mapping, MutableMapping.**
* **Names of their required methods are stored in `'<abc>.__abstractmethods__'`.** * **Names of their required methods are stored in `'<abc>.__abstractmethods__'`.**
Enum Enum
---- ----
**Class of named constants called members.**
```python ```python
from enum import Enum, auto from enum import Enum, auto
``` ```
```python ```python
class <enum_name>(Enum): class <enum_name>(Enum):
<member_name> = auto()
<member_name> = <value>
<member_name> = <value>, <value>
<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> # Tuple can be used for multiple values.
``` ```
* **Function auto() returns an increment of the last numeric value or 1.**
* **Accessing a member named after a reserved keyword causes SyntaxError.** * **Accessing a member named after a reserved keyword causes SyntaxError.**
* **Methods receive the member they were called on as the 'self' argument.** * **Methods receive the member they were called on as the 'self' argument.**
@ -1356,7 +1359,7 @@ class <enum_name>(Enum):
```python ```python
<enum> = type(<member>) # Returns member's enum. <enum> = type(<member>) # Returns member's enum.
<iter> = itertools.cycle(<enum>) # Retruns endless iterator of members.
<iter> = itertools.cycle(<enum>) # Returns endless iterator of members.
<member> = random.choice(list(<enum>)) # Returns a random member. <member> = random.choice(list(<enum>)) # Returns a random member.
``` ```
@ -1630,7 +1633,7 @@ from pathlib import Path
``` ```
```python ```python
<str> = os.getcwd() # Returns the current working directory.
<str> = os.getcwd() # Returns current directory. Same as `$ pwd`.
<str> = os.path.join(<path>, ...) # Joins two or more pathname components. <str> = os.path.join(<path>, ...) # Joins two or more pathname components.
<str> = os.path.realpath(<path>) # Resolves symlinks and calls path.abspath(). <str> = os.path.realpath(<path>) # Resolves symlinks and calls path.abspath().
``` ```
@ -1654,7 +1657,7 @@ from pathlib import Path
```python ```python
<stat> = os.stat(<path>) # Or: <DirEntry/Path>.stat() <stat> = os.stat(<path>) # Or: <DirEntry/Path>.stat()
<real> = <stat>.st_mtime/st_size/… # Modification time, size in bytes, ...
<num> = <stat>.st_mtime/st_size/… # Modification time, size in bytes, ...
``` ```
### DirEntry ### DirEntry
@ -1949,11 +1952,11 @@ with <conn>.begin(): ... # Exits the block with commit or
Bytes Bytes
----- -----
**Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.**
**A bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.**
```python ```python
<bytes> = b'<str>' # Only accepts ASCII characters and \x00-\xff. <bytes> = b'<str>' # Only accepts ASCII characters and \x00-\xff.
<int> = <bytes>[<index>] # Returns an int in range from 0 to 255.
<int> = <bytes>[index] # Returns an int in range from 0 to 255.
<bytes> = <bytes>[<slice>] # Returns bytes even if it has only one element. <bytes> = <bytes>[<slice>] # Returns bytes even if it has only one element.
<bytes> = <bytes>.join(<coll_of_bytes>) # Joins elements using bytes as a separator. <bytes> = <bytes>.join(<coll_of_bytes>) # Joins elements using bytes as a separator.
``` ```
@ -1961,17 +1964,17 @@ Bytes
### Encode ### Encode
```python ```python
<bytes> = bytes(<coll_of_ints>) # Ints must be in range from 0 to 255. <bytes> = bytes(<coll_of_ints>) # Ints must be in range from 0 to 255.
<bytes> = bytes(<str>, 'utf-8') # Or: <str>.encode('utf-8')
<bytes> = <int>.to_bytes(n_bytes, …) # `byteorder='big/little', signed=False`.
<bytes> = bytes(<str>, 'utf-8') # Encodes string. Also <str>.encode('utf-8').
<bytes> = bytes.fromhex('<hex>') # Hex pairs can be separated by whitespaces. <bytes> = bytes.fromhex('<hex>') # Hex pairs can be separated by whitespaces.
<bytes> = <int>.to_bytes(n_bytes, …) # `byteorder='big/little', signed=False`.
``` ```
### Decode ### Decode
```python ```python
<list> = list(<bytes>) # Returns ints in range from 0 to 255. <list> = list(<bytes>) # Returns ints in range from 0 to 255.
<str> = str(<bytes>, 'utf-8') # Or: <bytes>.decode('utf-8')
<str> = str(<bytes>, 'utf-8') # Decodes bytes. Also <bytes>.decode('utf-8').
<str> = <bytes>.hex() # Returns hex pairs. Accepts `sep=<str>`.
<int> = int.from_bytes(<bytes>, …) # `byteorder='big/little', signed=False`. <int> = int.from_bytes(<bytes>, …) # `byteorder='big/little', signed=False`.
'<hex>' = <bytes>.hex() # Returns hex pairs. Accepts `sep=<str>`.
``` ```
### Read Bytes from File ### Read Bytes from File
@ -2013,7 +2016,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03'
* **`'<'` - Little-endian (i.e. least significant byte first).** * **`'<'` - Little-endian (i.e. least significant byte first).**
* **`'>'` - Big-endian (also `'!'`).** * **`'>'` - Big-endian (also `'!'`).**
#### Besides numbers, pack() and unpack() also support bytes objects as part of the sequence:
#### Besides numbers, pack() and unpack() also support bytes objects as a part of the sequence:
* **`'c'` - A bytes object with a single element. For pad byte use `'x'`.** * **`'c'` - A bytes object with a single element. For pad byte use `'x'`.**
* **`'<n>s'` - A bytes object with n elements (not effected by byte order).** * **`'<n>s'` - A bytes object with n elements (not effected by byte order).**
@ -2035,10 +2038,10 @@ Array
```python ```python
from array import array from array import array
<array> = array('<typecode>', <collection>) # Array from collection of numbers.
<array> = array('<typecode>', <coll_of_nums>) # Array from collection of numbers.
<array> = array('<typecode>', <bytes>) # Array from bytes object. <array> = array('<typecode>', <bytes>) # Array from bytes object.
<array> = array('<typecode>', <array>) # Treats array as a sequence of numbers. <array> = array('<typecode>', <array>) # Treats array as a sequence of numbers.
<array>.fromfile(<file>, n_items) # Appends items. Raises EOFError on end.
<array>.fromfile(<file>, n_items) # Appends items. Also frombytes().
<bytes> = bytes(<array>) # Or: <array>.tobytes() <bytes> = bytes(<array>) # Or: <array>.tobytes()
<file>.write(<array>) # Writes array to the binary file. <file>.write(<array>) # Writes array to the binary file.
``` ```
@ -2054,7 +2057,7 @@ Memory View
```python ```python
<mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable. <mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable.
<real> = <mview>[<index>] # Returns an int or a float.
<real> = <mview>[index] # Returns an int or a float.
<mview> = <mview>[<slice>] # Mview with rearranged elements. <mview> = <mview>[<slice>] # Mview with rearranged elements.
<mview> = <mview>.cast('<typecode>') # Casts memoryview to the new format. <mview> = <mview>.cast('<typecode>') # Casts memoryview to the new format.
<mview>.release() # Releases memory buffer of target object. <mview>.release() # Releases memory buffer of target object.
@ -2068,10 +2071,10 @@ Memory View
``` ```
```python ```python
<list> = list(<mview>) # Returns a list of ints or floats.
<str> = str(<mview>, 'utf-8') # Treats mview as a bytes object.
<int> = int.from_bytes(<mview>, …) # `byteorder='big/little', signed=False`.
'<hex>' = <mview>.hex() # Treats mview as a bytes object.
<list> = list(<mview>) # Returns a list of ints or floats.
<str> = str(<mview>, 'utf-8') # Treats mview as a bytes object.
<str> = <mview>.hex() # Returns hex pairs. Accepts `sep=<str>`.
<int> = int.from_bytes(<mview>, …) # `byteorder='big/little', signed=False`.
``` ```
@ -2779,7 +2782,7 @@ from PIL import Image
``` ```
```python ```python
<Image> = <Image>.filter(<Filter>) # `<Filter> = ImageFilter.<name>([<args>])`
<Image> = <Image>.filter(<Filter>) # `<Filter> = ImageFilter.<name>(<args>)`
<Image> = <Enhance>.enhance(<float>) # `<Enhance> = ImageEnhance.<name>(<Image>)` <Image> = <Enhance>.enhance(<float>) # `<Enhance> = ImageEnhance.<name>(<Image>)`
``` ```
@ -3159,13 +3162,13 @@ Name: a, dtype: int64
```python ```python
<el> = <Sr>.loc[key] # Or: <Sr>.iloc[i] <el> = <Sr>.loc[key] # Or: <Sr>.iloc[i]
<Sr> = <Sr>.loc[keys] # Or: <Sr>.iloc[coll_of_i]
<Sr> = <Sr>.loc[from_key:to_key_inc] # Or: <Sr>.iloc[from_i:to_i_exc]
<Sr> = <Sr>.loc[coll_of_keys] # Or: <Sr>.iloc[coll_of_i]
<Sr> = <Sr>.loc[from_key : to_key_inc] # Or: <Sr>.iloc[from_i : to_i_exc]
``` ```
```python ```python
<el> = <Sr>[key/i] # Or: <Sr>.<key> <el> = <Sr>[key/i] # Or: <Sr>.<key>
<Sr> = <Sr>[keys/coll_of_i] # Or: <Sr>[key/i : key/i]
<Sr> = <Sr>[coll_of_keys/coll_of_i] # Or: <Sr>[key/i : key/i]
<Sr> = <Sr>[bools] # Or: <Sr>.loc/iloc[bools] <Sr> = <Sr>[bools] # Or: <Sr>.loc/iloc[bools]
``` ```
@ -3216,7 +3219,7 @@ y 3
| | y 2 | y 2 | y 2 | | | y 2 | y 2 | y 2 |
+---------------+-------------+-------------+---------------+ +---------------+-------------+-------------+---------------+
``` ```
* **Keys/indices/bools can't be tuples because `'obj[x, y]'` is converted to `'obj[(x, y)]'`!**
* **Indexing objects can't be tuples because `'obj[x, y]'` is converted to `'obj[(x, y)]'`!**
* **Methods ffill(), interpolate(), fillna() and dropna() accept `'inplace=True'`.** * **Methods ffill(), interpolate(), fillna() and dropna() accept `'inplace=True'`.**
* **Last result has a hierarchical index. Use `'<Sr>[key_1, key_2]'` to get its values.** * **Last result has a hierarchical index. Use `'<Sr>[key_1, key_2]'` to get its values.**
@ -3354,8 +3357,8 @@ plt.show() # Displays the plot. Also plt.sav
``` ```
```python ```python
<dict> = <DF>.to_dict(['d/l/s/…']) # Returns columns as dicts, lists or series.
<str> = <DF>.to_json/html/csv([<path>]) # Also to_markdown/latex([<path>]).
<dict> = <DF>.to_dict('d/l/s/…') # Returns columns as dicts, lists or series.
<str> = <DF>.to_json/html/csv/latex() # Saves output to file if path is passed.
<DF>.to_pickle/excel(<path>) # Run `$ pip3 install "pandas[excel]" odfpy`. <DF>.to_pickle/excel(<path>) # Run `$ pip3 install "pandas[excel]" odfpy`.
<DF>.to_sql('<table_name>', <connection>) # Also `if_exists='fail/replace/append'`. <DF>.to_sql('<table_name>', <connection>) # Also `if_exists='fail/replace/append'`.
``` ```

86
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>April 28, 2024</aside>
<aside>May 4, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -100,7 +100,8 @@
main() main()
</code></pre></div> </code></pre></div>
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs">&lt;list&gt; = &lt;list&gt;[&lt;slice&gt;] <span class="hljs-comment"># Or: &lt;list&gt;[from_inclusive : to_exclusive : ±step]</span>
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs">&lt;el&gt; = &lt;list&gt;[index] <span class="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
&lt;list&gt; = &lt;list&gt;[&lt;slice&gt;] <span class="hljs-comment"># Or: &lt;list&gt;[from_inclusive : to_exclusive : ±step]</span>
</code></pre></div> </code></pre></div>
<pre><code class="python language-python hljs">&lt;list&gt;.append(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;list&gt; += [&lt;el&gt;]</span> <pre><code class="python language-python hljs">&lt;list&gt;.append(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;list&gt; += [&lt;el&gt;]</span>
@ -123,10 +124,11 @@ list_of_chars = list(&lt;str&gt;)
<li><strong>For details about sorted(), min() and max() see <a href="#sortable">sortable</a>.</strong></li> <li><strong>For details about sorted(), min() and max() see <a href="#sortable">sortable</a>.</strong></li>
<li><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></li> <li><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></li>
</ul> </ul>
<pre><code class="python language-python hljs">&lt;list&gt;.insert(&lt;int&gt;, &lt;el&gt;) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
&lt;el&gt; = &lt;list&gt;.pop([&lt;int&gt;]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span>
&lt;int&gt; = &lt;list&gt;.count(&lt;el&gt;) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span>
<pre><code class="python language-python hljs">&lt;int&gt; = len(&lt;list&gt;) <span class="hljs-comment"># Returns number of items. Also works on other collections.</span>
&lt;int&gt; = &lt;list&gt;.count(&lt;el&gt;) <span class="hljs-comment"># Returns number of occurrences. Also `if &lt;el&gt; in &lt;coll&gt;: ...`.</span>
&lt;int&gt; = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span> &lt;int&gt; = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span>
&lt;el&gt; = &lt;list&gt;.pop() <span class="hljs-comment"># Removes and returns item from the end or at index if passed.</span>
&lt;list&gt;.insert(&lt;int&gt;, &lt;el&gt;) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span> &lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
&lt;list&gt;.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span> &lt;list&gt;.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span>
</code></pre> </code></pre>
@ -435,9 +437,9 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<li><strong>When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes <code class="python hljs"><span class="hljs-string">'{6.5:.0f}'</span></code> a <code class="python hljs"><span class="hljs-string">'6'</span></code> and <code class="python hljs"><span class="hljs-string">'{7.5:.0f}'</span></code> an <code class="python hljs"><span class="hljs-string">'8'</span></code>.</strong></li> <li><strong>When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes <code class="python hljs"><span class="hljs-string">'{6.5:.0f}'</span></code> a <code class="python hljs"><span class="hljs-string">'6'</span></code> and <code class="python hljs"><span class="hljs-string">'{7.5:.0f}'</span></code> an <code class="python hljs"><span class="hljs-string">'8'</span></code>.</strong></li>
<li><strong>This rule only effects numbers that can be represented exactly by a float (<code class="python hljs"><span class="hljs-number">.5</span></code>, <code class="python hljs"><span class="hljs-number">.25</span></code>, …).</strong></li> <li><strong>This rule only effects numbers that can be represented exactly by a float (<code class="python hljs"><span class="hljs-number">.5</span></code>, <code class="python hljs"><span class="hljs-number">.25</span></code>, …).</strong></li>
</ul> </ul>
<div><h3 id="ints">Ints</h3><pre><code class="python language-python hljs">{<span class="hljs-number">90</span>:c} <span class="hljs-comment"># 'Z'</span>
{<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'</span>
{<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'</span>
<div><h3 id="ints">Ints</h3><pre><code class="python language-python hljs">{<span class="hljs-number">90</span>:c} <span class="hljs-comment"># 'Z'. Unicode character with value 90.</span>
{<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'. Number 90 in binary.</span>
{<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'. Number 90 in uppercase hexadecimal.</span>
</code></pre></div> </code></pre></div>
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span> <div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
@ -453,7 +455,7 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<li><strong>Floats can be compared with: <code class="python hljs"><span class="hljs-string">'math.isclose(&lt;float&gt;, &lt;float&gt;)'</span></code>.</strong></li> <li><strong>Floats can be compared with: <code class="python hljs"><span class="hljs-string">'math.isclose(&lt;float&gt;, &lt;float&gt;)'</span></code>.</strong></li>
<li><strong>Precision of decimal operations is set with: <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = &lt;int&gt;'</span></code>.</strong></li> <li><strong>Precision of decimal operations is set with: <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = &lt;int&gt;'</span></code>.</strong></li>
</ul> </ul>
<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span>
<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;number&gt; ** &lt;number&gt;</span>
&lt;num&gt; = abs(&lt;num&gt;) <span class="hljs-comment"># &lt;float&gt; = abs(&lt;complex&gt;)</span> &lt;num&gt; = abs(&lt;num&gt;) <span class="hljs-comment"># &lt;float&gt; = abs(&lt;complex&gt;)</span>
&lt;num&gt; = round(&lt;num&gt; [, ±ndigits]) <span class="hljs-comment"># `round(126, -1) == 130`</span> &lt;num&gt; = round(&lt;num&gt; [, ±ndigits]) <span class="hljs-comment"># `round(126, -1) == 130`</span>
</code></pre></div> </code></pre></div>
@ -528,8 +530,8 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<li><strong>Timedelta normalizes arguments to ±days, seconds (&lt; 86 400) and microseconds (&lt; 1M).</strong></li> <li><strong>Timedelta normalizes arguments to ±days, seconds (&lt; 86 400) and microseconds (&lt; 1M).</strong></li>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'&lt;D/DT&gt;.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li> <li><strong>Use <code class="python hljs"><span class="hljs-string">'&lt;D/DT&gt;.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
</ul> </ul>
<div><h3 id="now">Now</h3><pre><code class="python language-python hljs">&lt;D/DTn&gt; = D/DT.today() <span class="hljs-comment"># Current local date or naive DT. Also DT.now().</span>
&lt;DTa&gt; = DT.now(&lt;tzinfo&gt;) <span class="hljs-comment"># Aware DT from current time in passed timezone.</span>
<div><h3 id="now">Now</h3><pre><code class="python language-python hljs">&lt;D/DTn&gt; = D/DT.today() <span class="hljs-comment"># Current local date or naive DT. Also DT.now().</span>
&lt;DTa&gt; = DT.now(&lt;tzinfo&gt;) <span class="hljs-comment"># Aware DT from current time in passed timezone.</span>
</code></pre></div> </code></pre></div>
<ul> <ul>
@ -1130,19 +1132,19 @@ Hello World!
<ul> <ul>
<li><strong>Method iter() is required for <code class="python hljs"><span class="hljs-string">'isinstance(&lt;obj&gt;, abc.Iterable)'</span></code> to return True, however any object with getitem() will work with any code expecting an iterable.</strong></li> <li><strong>Method iter() is required for <code class="python hljs"><span class="hljs-string">'isinstance(&lt;obj&gt;, abc.Iterable)'</span></code> to return True, however any object with getitem() will work with any code expecting an iterable.</strong></li>
<li><strong>Abstract base classes that generate missing methods when extended are: Sequence, MutableSequence, Set, MutableSet, Mapping and MutableMapping.</strong></li>
<li><strong>Other extendable ABCs: MutableSequence, Set, MutableSet, Mapping, MutableMapping.</strong></li>
<li><strong>Names of their required methods are stored in <code class="python hljs"><span class="hljs-string">'&lt;abc&gt;.__abstractmethods__'</span></code>.</strong></li> <li><strong>Names of their required methods are stored in <code class="python hljs"><span class="hljs-string">'&lt;abc&gt;.__abstractmethods__'</span></code>.</strong></li>
</ul> </ul>
<div><h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto
<div><h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2><p><strong>Class of named constants called members.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto
</code></pre></div> </code></pre></div>
<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> <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()
&lt;member_name&gt; = &lt;value&gt;
&lt;member_name&gt; = &lt;value&gt;, &lt;value&gt;
&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"># Tuple can be used for multiple values.</span>
</code></pre> </code></pre>
<ul> <ul>
<li><strong>Function auto() returns an increment of the last numeric value or 1.</strong></li>
<li><strong>Accessing a member named after a reserved keyword causes SyntaxError.</strong></li> <li><strong>Accessing a member named after a reserved keyword causes SyntaxError.</strong></li>
<li><strong>Methods receive the member they were called on as the 'self' argument.</strong></li> <li><strong>Methods receive the member they were called on as the 'self' argument.</strong></li>
</ul> </ul>
@ -1157,7 +1159,7 @@ Hello World!
&lt;list&gt; = [a.value <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> &lt;enum&gt;] <span class="hljs-comment"># Returns enum's member values.</span> &lt;list&gt; = [a.value <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> &lt;enum&gt;] <span class="hljs-comment"># Returns enum's member values.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;enum&gt; = type(&lt;member&gt;) <span class="hljs-comment"># Returns member's enum.</span> <pre><code class="python language-python hljs">&lt;enum&gt; = type(&lt;member&gt;) <span class="hljs-comment"># Returns member's enum.</span>
&lt;iter&gt; = itertools.cycle(&lt;enum&gt;) <span class="hljs-comment"># Retruns endless iterator of members.</span>
&lt;iter&gt; = itertools.cycle(&lt;enum&gt;) <span class="hljs-comment"># Returns endless iterator of members.</span>
&lt;member&gt; = random.choice(list(&lt;enum&gt;)) <span class="hljs-comment"># Returns a random member.</span> &lt;member&gt; = random.choice(list(&lt;enum&gt;)) <span class="hljs-comment"># Returns a random member.</span>
</code></pre> </code></pre>
<div><h3 id="inline-1">Inline</h3><pre><code class="python language-python hljs">Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, <span class="hljs-string">'FORK KNIFE SPOON'</span>) <div><h3 id="inline-1">Inline</h3><pre><code class="python language-python hljs">Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, <span class="hljs-string">'FORK KNIFE SPOON'</span>)
@ -1381,7 +1383,7 @@ p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path <span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
</code></pre></div> </code></pre></div>
<pre><code class="python language-python hljs">&lt;str&gt; = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
<pre><code class="python language-python hljs">&lt;str&gt; = os.getcwd() <span class="hljs-comment"># Returns current directory. Same as `$ pwd`.</span>
&lt;str&gt; = os.path.join(&lt;path&gt;, ...) <span class="hljs-comment"># Joins two or more pathname components.</span> &lt;str&gt; = os.path.join(&lt;path&gt;, ...) <span class="hljs-comment"># Joins two or more pathname components.</span>
&lt;str&gt; = os.path.realpath(&lt;path&gt;) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span> &lt;str&gt; = os.path.realpath(&lt;path&gt;) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span>
</code></pre> </code></pre>
@ -1397,7 +1399,7 @@ p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt
&lt;bool&gt; = os.path.isdir(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.is_dir()</span> &lt;bool&gt; = os.path.isdir(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.is_dir()</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;stat&gt; = os.stat(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.stat()</span> <pre><code class="python language-python hljs">&lt;stat&gt; = os.stat(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.stat()</span>
&lt;real&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span>
&lt;num&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span>
</code></pre> </code></pre>
<div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.</strong></p><pre><code class="python language-python hljs">&lt;iter&gt; = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span> <div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.</strong></p><pre><code class="python language-python hljs">&lt;iter&gt; = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span>
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns the whole path as a string.</span> &lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns the whole path as a string.</span>
@ -1614,23 +1616,23 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
┃ oracle │ oracledb │ oracledb │ www.pypi.org/project/oracledb ┃ ┃ oracle │ oracledb │ oracledb │ www.pypi.org/project/oracledb ┃
┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
</code></pre> </code></pre>
<div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.</strong></p><pre><code class="python language-python hljs">&lt;bytes&gt; = <span class="hljs-string">b'&lt;str&gt;'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00-\xff.</span>
&lt;int&gt; = &lt;bytes&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int in range from 0 to 255.</span>
<div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>A bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.</strong></p><pre><code class="python language-python hljs">&lt;bytes&gt; = <span class="hljs-string">b'&lt;str&gt;'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00-\xff.</span>
&lt;int&gt; = &lt;bytes&gt;[index] <span class="hljs-comment"># Returns an int in range from 0 to 255.</span>
&lt;bytes&gt; = &lt;bytes&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span> &lt;bytes&gt; = &lt;bytes&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
&lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_bytes&gt;) <span class="hljs-comment"># Joins elements using bytes as a separator.</span> &lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_bytes&gt;) <span class="hljs-comment"># Joins elements using bytes as a separator.</span>
</code></pre></div> </code></pre></div>
<div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs">&lt;bytes&gt; = bytes(&lt;coll_of_ints&gt;) <span class="hljs-comment"># Ints must be in range from 0 to 255.</span> <div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs">&lt;bytes&gt; = bytes(&lt;coll_of_ints&gt;) <span class="hljs-comment"># Ints must be in range from 0 to 255.</span>
&lt;bytes&gt; = bytes(&lt;str&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: &lt;str&gt;.encode('utf-8')</span>
&lt;bytes&gt; = &lt;int&gt;.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
&lt;bytes&gt; = bytes(&lt;str&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Encodes string. Also &lt;str&gt;.encode('utf-8').</span>
&lt;bytes&gt; = bytes.fromhex(<span class="hljs-string">'&lt;hex&gt;'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespaces.</span> &lt;bytes&gt; = bytes.fromhex(<span class="hljs-string">'&lt;hex&gt;'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespaces.</span>
&lt;bytes&gt; = &lt;int&gt;.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
</code></pre></div> </code></pre></div>
<div><h3 id="decode-1">Decode</h3><pre><code class="python language-python hljs">&lt;list&gt; = list(&lt;bytes&gt;) <span class="hljs-comment"># Returns ints in range from 0 to 255.</span> <div><h3 id="decode-1">Decode</h3><pre><code class="python language-python hljs">&lt;list&gt; = list(&lt;bytes&gt;) <span class="hljs-comment"># Returns ints in range from 0 to 255.</span>
&lt;str&gt; = str(&lt;bytes&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: &lt;bytes&gt;.decode('utf-8')</span>
&lt;str&gt; = str(&lt;bytes&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Decodes bytes. Also &lt;bytes&gt;.decode('utf-8').</span>
&lt;str&gt; = &lt;bytes&gt;.hex() <span class="hljs-comment"># Returns hex pairs. Accepts `sep=&lt;str&gt;`.</span>
&lt;int&gt; = int.from_bytes(&lt;bytes&gt;, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span> &lt;int&gt; = int.from_bytes(&lt;bytes&gt;, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;bytes&gt;.hex() <span class="hljs-comment"># Returns hex pairs. Accepts `sep=&lt;str&gt;`.</span>
</code></pre></div> </code></pre></div>
<div><h3 id="readbytesfromfile">Read Bytes from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_bytes</span><span class="hljs-params">(filename)</span>:</span> <div><h3 id="readbytesfromfile">Read Bytes from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_bytes</span><span class="hljs-params">(filename)</span>:</span>
@ -1661,7 +1663,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - System's byte order (usually little-endian).</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - System's byte order (usually little-endian).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;'</span></code> - Little-endian (i.e. least significant byte first).</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'&lt;'</span></code> - Little-endian (i.e. least significant byte first).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'&gt;'</span></code> - Big-endian (also <code class="python hljs"><span class="hljs-string">'!'</span></code>).</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'&gt;'</span></code> - Big-endian (also <code class="python hljs"><span class="hljs-string">'!'</span></code>).</strong></li>
</ul><div><h4 id="besidesnumberspackandunpackalsosupportbytesobjectsaspartofthesequence">Besides numbers, pack() and unpack() also support bytes objects as part of the sequence:</h4><ul>
</ul><div><h4 id="besidesnumberspackandunpackalsosupportbytesobjectsasapartofthesequence">Besides numbers, pack() and unpack() also support bytes objects as a part of the sequence:</h4><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'c'</span></code> - A bytes object with a single element. For pad byte use <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'c'</span></code> - A bytes object with a single element. For pad byte use <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li>
<li><strong><code class="apache hljs"><span class="hljs-section">'&lt;n&gt;s'</span><span class="hljs-attribute"></span></code> - A bytes object with n elements (not effected by byte order).</strong></li> <li><strong><code class="apache hljs"><span class="hljs-section">'&lt;n&gt;s'</span><span class="hljs-attribute"></span></code> - A bytes object with n elements (not effected by byte order).</strong></li>
</ul></div></div><div><div><h4 id="integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:</h4><ul> </ul></div></div><div><div><h4 id="integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:</h4><ul>
@ -1683,10 +1685,10 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be swapped with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array <div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be swapped with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;collection&gt;) <span class="hljs-comment"># Array from collection of numbers.</span>
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;coll_of_nums&gt;) <span class="hljs-comment"># Array from collection of numbers.</span>
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;bytes&gt;) <span class="hljs-comment"># Array from bytes object.</span> &lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;bytes&gt;) <span class="hljs-comment"># Array from bytes object.</span>
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;array&gt;) <span class="hljs-comment"># Treats array as a sequence of numbers.</span> &lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;array&gt;) <span class="hljs-comment"># Treats array as a sequence of numbers.</span>
&lt;array&gt;.fromfile(&lt;file&gt;, n_items) <span class="hljs-comment"># Appends items. Raises EOFError on end.</span>
&lt;array&gt;.fromfile(&lt;file&gt;, n_items) <span class="hljs-comment"># Appends items. Also frombytes().</span>
&lt;bytes&gt; = bytes(&lt;array&gt;) <span class="hljs-comment"># Or: &lt;array&gt;.tobytes()</span> &lt;bytes&gt; = bytes(&lt;array&gt;) <span class="hljs-comment"># Or: &lt;array&gt;.tobytes()</span>
&lt;file&gt;.write(&lt;array&gt;) <span class="hljs-comment"># Writes array to the binary file.</span> &lt;file&gt;.write(&lt;array&gt;) <span class="hljs-comment"># Writes array to the binary file.</span>
</code></pre></div> </code></pre></div>
@ -1699,7 +1701,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<li><strong>Casting only works between char and other types and uses system's sizes.</strong></li> <li><strong>Casting only works between char and other types and uses system's sizes.</strong></li>
<li><strong>Byte order is always determined by the system.</strong></li> <li><strong>Byte order is always determined by the system.</strong></li>
</ul><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-comment"># Immutable if bytes, else mutable.</span> </ul><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-comment"># Immutable if bytes, else mutable.</span>
&lt;real&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int or a float.</span>
&lt;real&gt; = &lt;mview&gt;[index] <span class="hljs-comment"># Returns an int or a float.</span>
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Mview with rearranged elements.</span> &lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Mview with rearranged elements.</span>
&lt;mview&gt; = &lt;mview&gt;.cast(<span class="hljs-string">'&lt;typecode&gt;'</span>) <span class="hljs-comment"># Casts memoryview to the new format.</span> &lt;mview&gt; = &lt;mview&gt;.cast(<span class="hljs-string">'&lt;typecode&gt;'</span>) <span class="hljs-comment"># Casts memoryview to the new format.</span>
&lt;mview&gt;.release() <span class="hljs-comment"># Releases memory buffer of target object.</span> &lt;mview&gt;.release() <span class="hljs-comment"># Releases memory buffer of target object.</span>
@ -1711,10 +1713,10 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;mview&gt;) <span class="hljs-comment"># Treats mview as a sequence of numbers.</span> &lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;mview&gt;) <span class="hljs-comment"># Treats mview as a sequence of numbers.</span>
&lt;file&gt;.write(&lt;mview&gt;) <span class="hljs-comment"># Writes mview to the binary file.</span> &lt;file&gt;.write(&lt;mview&gt;) <span class="hljs-comment"># Writes mview to the binary file.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns a list of ints or floats.</span>
&lt;str&gt; = str(&lt;mview&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Treats mview as a bytes object.</span>
&lt;int&gt; = int.from_bytes(&lt;mview&gt;, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;mview&gt;.hex() <span class="hljs-comment"># Treats mview as a bytes object.</span>
<pre><code class="python language-python hljs">&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns a list of ints or floats.</span>
&lt;str&gt; = str(&lt;mview&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Treats mview as a bytes object.</span>
&lt;str&gt; = &lt;mview&gt;.hex() <span class="hljs-comment"># Returns hex pairs. Accepts `sep=&lt;str&gt;`.</span>
&lt;int&gt; = int.from_bytes(&lt;mview&gt;, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
</code></pre> </code></pre>
<div><h2 id="deque"><a href="#deque" name="deque">#</a>Deque</h2><p><strong>A thread-safe list with efficient appends and pops from either side. Pronounced "deck".</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> deque <div><h2 id="deque"><a href="#deque" name="deque">#</a>Deque</h2><p><strong>A thread-safe list with efficient appends and pops from either side. Pronounced "deck".</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> deque
&lt;deque&gt; = deque(&lt;collection&gt;) <span class="hljs-comment"># Also `maxlen=None`.</span> &lt;deque&gt; = deque(&lt;collection&gt;) <span class="hljs-comment"># Also `maxlen=None`.</span>
@ -2276,7 +2278,7 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</
&lt;Image&gt;.putdata(&lt;list/ImagingCore&gt;) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span> &lt;Image&gt;.putdata(&lt;list/ImagingCore&gt;) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span>
&lt;Image&gt;.paste(&lt;Image&gt;, (x, y)) <span class="hljs-comment"># Draws passed image at specified location.</span> &lt;Image&gt;.paste(&lt;Image&gt;, (x, y)) <span class="hljs-comment"># Draws passed image at specified location.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;Image&gt; = &lt;Image&gt;.filter(&lt;Filter&gt;) <span class="hljs-comment"># `&lt;Filter&gt; = ImageFilter.&lt;name&gt;([&lt;args&gt;])`</span>
<pre><code class="python language-python hljs">&lt;Image&gt; = &lt;Image&gt;.filter(&lt;Filter&gt;) <span class="hljs-comment"># `&lt;Filter&gt; = ImageFilter.&lt;name&gt;(&lt;args&gt;)`</span>
&lt;Image&gt; = &lt;Enhance&gt;.enhance(&lt;float&gt;) <span class="hljs-comment"># `&lt;Enhance&gt; = ImageEnhance.&lt;name&gt;(&lt;Image&gt;)`</span> &lt;Image&gt; = &lt;Enhance&gt;.enhance(&lt;float&gt;) <span class="hljs-comment"># `&lt;Enhance&gt; = ImageEnhance.&lt;name&gt;(&lt;Image&gt;)`</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;array&gt; = np.array(&lt;Image&gt;) <span class="hljs-comment"># Creates a 2d/3d NumPy array from the image.</span> <pre><code class="python language-python hljs">&lt;array&gt; = np.array(&lt;Image&gt;) <span class="hljs-comment"># Creates a 2d/3d NumPy array from the image.</span>
@ -2590,11 +2592,11 @@ Name: a, dtype: int64
&lt;Sr&gt; = pd.Series(&lt;dict/Series&gt;, index=&lt;list&gt;) <span class="hljs-comment"># Only keeps items with keys specified in index.</span> &lt;Sr&gt; = pd.Series(&lt;dict/Series&gt;, index=&lt;list&gt;) <span class="hljs-comment"># Only keeps items with keys specified in index.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;Sr&gt;.loc[key] <span class="hljs-comment"># Or: &lt;Sr&gt;.iloc[i]</span> <pre><code class="python language-python hljs">&lt;el&gt; = &lt;Sr&gt;.loc[key] <span class="hljs-comment"># Or: &lt;Sr&gt;.iloc[i]</span>
&lt;Sr&gt; = &lt;Sr&gt;.loc[keys] <span class="hljs-comment"># Or: &lt;Sr&gt;.iloc[coll_of_i]</span>
&lt;Sr&gt; = &lt;Sr&gt;.loc[from_key:to_key_inc] <span class="hljs-comment"># Or: &lt;Sr&gt;.iloc[from_i:to_i_exc]</span>
&lt;Sr&gt; = &lt;Sr&gt;.loc[coll_of_keys] <span class="hljs-comment"># Or: &lt;Sr&gt;.iloc[coll_of_i]</span>
&lt;Sr&gt; = &lt;Sr&gt;.loc[from_key : to_key_inc] <span class="hljs-comment"># Or: &lt;Sr&gt;.iloc[from_i : to_i_exc]</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;Sr&gt;[key/i] <span class="hljs-comment"># Or: &lt;Sr&gt;.&lt;key&gt;</span> <pre><code class="python language-python hljs">&lt;el&gt; = &lt;Sr&gt;[key/i] <span class="hljs-comment"># Or: &lt;Sr&gt;.&lt;key&gt;</span>
&lt;Sr&gt; = &lt;Sr&gt;[keys/coll_of_i] <span class="hljs-comment"># Or: &lt;Sr&gt;[key/i : key/i]</span>
&lt;Sr&gt; = &lt;Sr&gt;[coll_of_keys/coll_of_i] <span class="hljs-comment"># Or: &lt;Sr&gt;[key/i : key/i]</span>
&lt;Sr&gt; = &lt;Sr&gt;[bools] <span class="hljs-comment"># Or: &lt;Sr&gt;.loc/iloc[bools]</span> &lt;Sr&gt; = &lt;Sr&gt;[bools] <span class="hljs-comment"># Or: &lt;Sr&gt;.loc/iloc[bools]</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;Sr&gt; = &lt;Sr&gt; &gt; &lt;el/Sr&gt; <span class="hljs-comment"># Returns a Series of bools.</span> <pre><code class="python language-python hljs">&lt;Sr&gt; = &lt;Sr&gt; &gt; &lt;el/Sr&gt; <span class="hljs-comment"># Returns a Series of bools.</span>
@ -2633,7 +2635,7 @@ y <span class="hljs-number">3</span>
</code></pre> </code></pre>
<ul> <ul>
<li><strong>Keys/indices/bools can't be tuples because <code class="python hljs"><span class="hljs-string">'obj[x, y]'</span></code> is converted to <code class="python hljs"><span class="hljs-string">'obj[(x, y)]'</span></code>!</strong></li>
<li><strong>Indexing objects can't be tuples because <code class="python hljs"><span class="hljs-string">'obj[x, y]'</span></code> is converted to <code class="python hljs"><span class="hljs-string">'obj[(x, y)]'</span></code>!</strong></li>
<li><strong>Methods ffill(), interpolate(), fillna() and dropna() accept <code class="python hljs"><span class="hljs-string">'inplace=True'</span></code>.</strong></li> <li><strong>Methods ffill(), interpolate(), fillna() and dropna() accept <code class="python hljs"><span class="hljs-string">'inplace=True'</span></code>.</strong></li>
<li><strong>Last result has a hierarchical index. Use <code class="python hljs"><span class="hljs-string">'&lt;Sr&gt;[key_1, key_2]'</span></code> to get its values.</strong></li> <li><strong>Last result has a hierarchical index. Use <code class="python hljs"><span class="hljs-string">'&lt;Sr&gt;[key_1, key_2]'</span></code> to get its values.</strong></li>
</ul> </ul>
@ -2746,8 +2748,8 @@ plt.show() <span class="hljs-comment"># Disp
&lt;DF&gt; = pd.read_pickle/excel(<span class="hljs-string">'&lt;path/url&gt;'</span>) <span class="hljs-comment"># Use `sheet_name=None` to get all Excel sheets.</span> &lt;DF&gt; = pd.read_pickle/excel(<span class="hljs-string">'&lt;path/url&gt;'</span>) <span class="hljs-comment"># Use `sheet_name=None` to get all Excel sheets.</span>
&lt;DF&gt; = pd.read_sql(<span class="hljs-string">'&lt;table/query&gt;'</span>, &lt;conn.&gt;) <span class="hljs-comment"># SQLite3/SQLAlchemy connection (see #SQLite).</span> &lt;DF&gt; = pd.read_sql(<span class="hljs-string">'&lt;table/query&gt;'</span>, &lt;conn.&gt;) <span class="hljs-comment"># SQLite3/SQLAlchemy connection (see #SQLite).</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;dict&gt; = &lt;DF&gt;.to_dict([<span class="hljs-string">'d/l/s/…'</span>]) <span class="hljs-comment"># Returns columns as dicts, lists or series.</span>
&lt;str&gt; = &lt;DF&gt;.to_json/html/csv([&lt;path&gt;]) <span class="hljs-comment"># Also to_markdown/latex([&lt;path&gt;]).</span>
<pre><code class="python language-python hljs">&lt;dict&gt; = &lt;DF&gt;.to_dict(<span class="hljs-string">'d/l/s/…'</span>) <span class="hljs-comment"># Returns columns as dicts, lists or series.</span>
&lt;str&gt; = &lt;DF&gt;.to_json/html/csv/latex() <span class="hljs-comment"># Saves output to file if path is passed.</span>
&lt;DF&gt;.to_pickle/excel(&lt;path&gt;) <span class="hljs-comment"># Run `$ pip3 install "pandas[excel]" odfpy`.</span> &lt;DF&gt;.to_pickle/excel(&lt;path&gt;) <span class="hljs-comment"># Run `$ pip3 install "pandas[excel]" odfpy`.</span>
&lt;DF&gt;.to_sql(<span class="hljs-string">'&lt;table_name&gt;'</span>, &lt;connection&gt;) <span class="hljs-comment"># Also `if_exists='fail/replace/append'`.</span> &lt;DF&gt;.to_sql(<span class="hljs-string">'&lt;table_name&gt;'</span>, &lt;connection&gt;) <span class="hljs-comment"># Also `if_exists='fail/replace/append'`.</span>
</code></pre> </code></pre>
@ -2934,7 +2936,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer> <footer>
<aside>April 28, 2024</aside>
<aside>May 4, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

Loading…
Cancel
Save