|
56 | 56 |
|
57 | 57 | <body> |
58 | 58 | <header> |
59 | | - <aside>July 27, 2025</aside> |
| 59 | + <aside>July 28, 2025</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
61 | 61 | </header> |
62 | 62 |
|
|
704 | 704 |
|
705 | 705 |
|
706 | 706 | <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> |
708 | 708 | <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 <package>'</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 <package>'</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">'<package>/__init__.py'</span></code> script.</strong></li> |
710 | 710 | <li><strong>Directory of the file that is passed to python command serves as a root of local imports.</strong></li> |
711 | 711 | <li><strong>For relative imports use <code class="python hljs"><span class="hljs-string">'from .[…][<pkg/module>[.…]] import <obj>'</span></code>.</strong></li> |
712 | 712 | </ul> |
|
1007 | 1007 | <li><strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehensions">generator expressions</a>.</strong></li> |
1008 | 1008 | <li><strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong></li> |
1009 | 1009 | </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> |
1011 | 1011 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'callable(<obj>)'</span></code> or <code class="python hljs"><span class="hljs-string">'isinstance(<obj>, 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> |
1012 | 1012 | <li><strong>When this cheatsheet uses <code class="python hljs"><span class="hljs-string">'<function>'</span></code> as an argument, it means <code class="python hljs"><span class="hljs-string">'<callable>'</span></code>.</strong></li> |
1013 | 1013 | </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 | 1201 | <ul> |
1202 | 1202 | <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> |
1203 | 1203 | <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> |
1205 | 1205 | <li><strong>To catch signals use <code class="python hljs"><span class="hljs-string">'signal.signal(signal_number, handler_function)'</span></code>.</strong></li> |
1206 | 1206 | </ul> |
1207 | 1207 | <div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception>: ... |
|
1290 | 1290 | </code></pre></div> |
1291 | 1291 |
|
1292 | 1292 | <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> |
1294 | 1294 | <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> |
1295 | 1295 | </ul> |
1296 | 1296 | <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 | 1327 |
|
1328 | 1328 | <ul> |
1329 | 1329 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'help=<str>'</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=<obj>'</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=<obj>'</span></code> to override None as option's or optional argument's default value.</strong></li> |
1331 | 1331 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(<mode>)'</span></code> for files. Accepts 'encoding', but 'newline' is None.</strong></li> |
1332 | 1332 | </ul> |
1333 | 1333 | <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"><file> = open(<path>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>) |
1334 | 1334 | </code></pre></div> |
1335 | 1335 |
|
1336 | 1336 |
|
1337 | 1337 | <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> |
1339 | 1339 | <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> |
1340 | 1340 | <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> |
1341 | 1341 | </ul> |
|
1520 | 1520 | </code></pre> |
1521 | 1521 | <ul> |
1522 | 1522 | <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> |
1524 | 1524 | <li><strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#fileformats">Pandas</a> library.</strong></li> |
1525 | 1525 | <li><strong>Reader accepts any iterator (or collection) of strings, not just files.</strong></li> |
1526 | 1526 | </ul> |
@@ -2936,7 +2936,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
2936 | 2936 |
|
2937 | 2937 |
|
2938 | 2938 | <footer> |
2939 | | - <aside>July 27, 2025</aside> |
| 2939 | + <aside>July 28, 2025</aside> |
2940 | 2940 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
2941 | 2941 | </footer> |
2942 | 2942 |
|
|
0 commit comments