Skip to content

Commit 576b9f4

Browse files
committed
Imports, Duck types, Print, Open
1 parent 76899d5 commit 576b9f4

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,9 @@ import <module> # Imports a built-in or '<module>.py'.
823823
import <package> # Imports a built-in or '<package>/__init__.py'.
824824
import <package>.<module> # Imports a built-in or '<package>/<module>.py'.
825825
```
826-
* **Package is a collection of modules, but it can also define its own objects.**
826+
* **Package is a collection of modules, but it can also define its own objects, classes, etc.**
827827
* **On a filesystem this corresponds to a directory of Python files with an optional init script.**
828-
* **Running `'import <package>'` does not automatically provide access to the package's modules unless they are explicitly imported in its init script.**
828+
* **Running `'import <package>'` does not automatically provide access to the package's modules unless they are explicitly imported in the `'<package>/__init__.py'` script.**
829829
* **Directory of the file that is passed to python command serves as a root of local imports.**
830830
* **For relative imports use `'from .[…][<pkg/module>[.…]] import <obj>'`.**
831831

@@ -1178,7 +1178,7 @@ class Counter:
11781178
* **File objects returned by the [open()](#open) function, etc.**
11791179

11801180
### Callable
1181-
* **All functions and classes have a call() method, hence are callable.**
1181+
* **All functions and classes have a call() method that is executed when they are called.**
11821182
* **Use `'callable(<obj>)'` or `'isinstance(<obj>, collections.abc.Callable)'` to check if object is callable. You can also just call the object and check if it raised TypeError.**
11831183
* **When this cheatsheet uses `'<function>'` as an argument, it means `'<callable>'`.**
11841184
```python
@@ -1396,7 +1396,7 @@ finally:
13961396
```
13971397
* **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.**
13981398
* **Code inside the `'finally'` block will always be executed (unless a signal is received).**
1399-
* **All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only a function block delimits scope).**
1399+
* **All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only the function block delimits scope).**
14001400
* **To catch signals use `'signal.signal(signal_number, handler_function)'`.**
14011401

14021402
### Catching Exceptions
@@ -1505,7 +1505,7 @@ Print
15051505
```python
15061506
print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
15071507
```
1508-
* **Use `'file=sys.stderr'` for messages about errors.**
1508+
* **Use `'file=sys.stderr'` for messages about errors so they can be processed separately.**
15091509
* **Stdout and stderr streams hold output in a buffer until they receive a string containing '\n' or '\r', buffer reaches 4096 characters, `'flush=True'` is used, or program exits.**
15101510

15111511
### Pretty Print
@@ -1549,7 +1549,7 @@ args = p.parse_args() # Exits on par
15491549
```
15501550

15511551
* **Use `'help=<str>'` to set argument description that will be displayed in help message.**
1552-
* **Use `'default=<obj>'` to set option's or optional argument's default value.**
1552+
* **Use `'default=<obj>'` to override None as option's or optional argument's default value.**
15531553
* **Use `'type=FileType(<mode>)'` for files. Accepts 'encoding', but 'newline' is None.**
15541554

15551555

@@ -1560,7 +1560,7 @@ Open
15601560
```python
15611561
<file> = open(<path>, mode='r', encoding=None, newline=None)
15621562
```
1563-
* **`'encoding=None'` means that the default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
1563+
* **`'encoding=None'` means that the default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` until it becomes the default (Python 3.15).**
15641564
* **`'newline=None'` means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.**
15651565
* **`'newline=""'` means no conversions take place, but input is still broken into chunks by readline() and readlines() on every '\n', '\r' and '\r\n'.**
15661566

@@ -1817,7 +1817,7 @@ import csv
18171817
<list> = list(<reader>) # Returns a list of all remaining rows.
18181818
```
18191819
* **Without the `'newline=""'` argument, every '\r\n' sequence that is embedded inside a quoted field will get converted to '\n'! For details about newline argument see [Open](#open).**
1820-
* **To print the spreadsheet to the console use [Tabulate](#table) library.**
1820+
* **To print the spreadsheet to the console use [Tabulate](#table) or PrettyTable library.**
18211821
* **For XML and binary Excel files (xlsx, xlsm and xlsb) use [Pandas](#fileformats) library.**
18221822
* **Reader accepts any iterator (or collection) of strings, not just files.**
18231823

index.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<body>
5858
<header>
59-
<aside>July 27, 2025</aside>
59+
<aside>July 28, 2025</aside>
6060
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6161
</header>
6262

@@ -704,9 +704,9 @@
704704

705705

706706
<ul>
707-
<li><strong>Package is a collection of modules, but it can also define its own objects.</strong></li>
707+
<li><strong>Package is a collection of modules, but it can also define its own objects, classes, etc.</strong></li>
708708
<li><strong>On a filesystem this corresponds to a directory of Python files with an optional init script.</strong></li>
709-
<li><strong>Running <code class="python hljs"><span class="hljs-string">'import &lt;package&gt;'</span></code> does not automatically provide access to the package's modules unless they are explicitly imported in its init script.</strong></li>
709+
<li><strong>Running <code class="python hljs"><span class="hljs-string">'import &lt;package&gt;'</span></code> does not automatically provide access to the package's modules unless they are explicitly imported in the <code class="python hljs"><span class="hljs-string">'&lt;package&gt;/__init__.py'</span></code> script.</strong></li>
710710
<li><strong>Directory of the file that is passed to python command serves as a root of local imports.</strong></li>
711711
<li><strong>For relative imports use <code class="python hljs"><span class="hljs-string">'from .[…][&lt;pkg/module&gt;[.…]] import &lt;obj&gt;'</span></code>.</strong></li>
712712
</ul>
@@ -1007,7 +1007,7 @@
10071007
<li><strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehensions">generator expressions</a>.</strong></li>
10081008
<li><strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong></li>
10091009
</ul><div><h3 id="callable">Callable</h3><ul>
1010-
<li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
1010+
<li><strong>All functions and classes have a call() method that is executed when they are called.</strong></li>
10111011
<li><strong>Use <code class="python hljs"><span class="hljs-string">'callable(&lt;obj&gt;)'</span></code> or <code class="python hljs"><span class="hljs-string">'isinstance(&lt;obj&gt;, collections.abc.Callable)'</span></code> to check if object is callable. You can also just call the object and check if it raised TypeError.</strong></li>
10121012
<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>
10131013
</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>
@@ -1201,7 +1201,7 @@
12011201
<ul>
12021202
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'else'</span></code> block will only be executed if <code class="python hljs"><span class="hljs-string">'try'</span></code> block had no exceptions.</strong></li>
12031203
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed (unless a signal is received).</strong></li>
1204-
<li><strong>All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only a function block delimits scope).</strong></li>
1204+
<li><strong>All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only the function block delimits scope).</strong></li>
12051205
<li><strong>To catch signals use <code class="python hljs"><span class="hljs-string">'signal.signal(signal_number, handler_function)'</span></code>.</strong></li>
12061206
</ul>
12071207
<div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;: ...
@@ -1290,7 +1290,7 @@
12901290
</code></pre></div>
12911291

12921292
<ul>
1293-
<li><strong>Use <code class="python hljs"><span class="hljs-string">'file=sys.stderr'</span></code> for messages about errors.</strong></li>
1293+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'file=sys.stderr'</span></code> for messages about errors so they can be processed separately.</strong></li>
12941294
<li><strong>Stdout and stderr streams hold output in a buffer until they receive a string containing '\n' or '\r', buffer reaches 4096 characters, <code class="python hljs"><span class="hljs-string">'flush=True'</span></code> is used, or program exits.</strong></li>
12951295
</ul>
12961296
<div><h3 id="prettyprint">Pretty Print</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pprint <span class="hljs-keyword">import</span> pprint
@@ -1327,15 +1327,15 @@
13271327

13281328
<ul>
13291329
<li><strong>Use <code class="python hljs"><span class="hljs-string">'help=&lt;str&gt;'</span></code> to set argument description that will be displayed in help message.</strong></li>
1330-
<li><strong>Use <code class="python hljs"><span class="hljs-string">'default=&lt;obj&gt;'</span></code> to set option's or optional argument's default value.</strong></li>
1330+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'default=&lt;obj&gt;'</span></code> to override None as option's or optional argument's default value.</strong></li>
13311331
<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(&lt;mode&gt;)'</span></code> for files. Accepts 'encoding', but 'newline' is None.</strong></li>
13321332
</ul>
13331333
<div><h2 id="open"><a href="#open" name="open">#</a>Open</h2><p><strong>Opens a file and returns the corresponding file object.</strong></p><pre><code class="python language-python hljs">&lt;file&gt; = open(&lt;path&gt;, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>)
13341334
</code></pre></div>
13351335

13361336

13371337
<ul>
1338-
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means that the default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li>
1338+
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means that the default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> until it becomes the default (Python 3.15).</strong></li>
13391339
<li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
13401340
<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on every '\n', '\r' and '\r\n'.</strong></li>
13411341
</ul>
@@ -1520,7 +1520,7 @@
15201520
</code></pre>
15211521
<ul>
15221522
<li><strong>Without the <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, every '\r\n' sequence that is embedded inside a quoted field will get converted to '\n'! For details about newline argument see <a href="#open">Open</a>.</strong></li>
1523-
<li><strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong></li>
1523+
<li><strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> or PrettyTable library.</strong></li>
15241524
<li><strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#fileformats">Pandas</a> library.</strong></li>
15251525
<li><strong>Reader accepts any iterator (or collection) of strings, not just files.</strong></li>
15261526
</ul>
@@ -2936,7 +2936,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29362936

29372937

29382938
<footer>
2939-
<aside>July 27, 2025</aside>
2939+
<aside>July 28, 2025</aside>
29402940
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29412941
</footer>
29422942

pdf/remove_links.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
'<strong>Use <code class="python hljs"><span class="hljs-string">\'logging.exception(&lt;str&gt;)\'</span></code> to log the passed message, followed by the full error message of the caught exception. For details about setting up the logger see <a href="#logging">Logging</a>.</strong>': '<strong>Use <code class="python hljs"><span class="hljs-string">\'logging.exception(&lt;str&gt;)\'</span></code> to log the passed message, followed by the full error message of the caught exception. For details about the logger setup see Logging (p. 31).</strong>',
2424
'<strong>Functions report OS related errors by raising OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong>': '<strong>Functions report OS related errors by raising OSError or one of its subclasses (p. 23).</strong>',
2525
'<strong>Without the <code class="python hljs"><span class="hljs-string">\'newline=""\'</span></code> argument, every \'\\r\\n\' sequence that is embedded inside a quoted field will get converted to \'\\n\'! For details about newline argument see <a href="#open">Open</a>.</strong>': '<strong>Without the <code class="python hljs"><span class="hljs-string">\'newline=""\'</span></code> argument, every \'\\r\\n\' sequence that is embedded inside a quoted field will get converted to \'\\n\'! For details about newline arg. see Open (p. 22).</strong>',
26-
'<strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong>': '<strong>To print the spreadsheet to the console use Tabulate library (p. 34).</strong>',
26+
'<strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> or PrettyTable library.</strong>': '<strong>To print the spreadsheet to the console use Tabulate or PrettyTable library (p. 34).</strong>',
2727
'<strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#fileformats">Pandas</a> library.</strong>': '<strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library (p. 46).</strong>',
2828
'<strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong>': '<strong>Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).</strong>',
2929
'<strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <a href="#pickle">pickable</a>, queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">\'if __name__ == "__main__": ...\'</span></code>.</strong>': '<strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be pickable, queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">\'if __name__ == "__main__": ...\'</span></code>.</strong>',

0 commit comments

Comments
 (0)