|
965 | 965 | <div><h3 id="sortable">Sortable</h3><ul> |
966 | 966 | <li><strong>With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods (used by <, >, <=, >=) and the rest will be automatically generated.</strong></li> |
967 | 967 | <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> |
968 | | -<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> |
| 968 | +<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> |
969 | 969 | <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> |
970 | 970 | </ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> total_ordering |
971 | 971 |
|
|
1316 | 1316 | arguments = sys.argv[<span class="hljs-number">1</span>:] |
1317 | 1317 | </code></pre></div> |
1318 | 1318 |
|
1319 | | -<div><h3 id="argumentparser">Argument Parser</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> argparse <span class="hljs-keyword">import</span> ArgumentParser, FileType |
| 1319 | +<div><h3 id="argumentparser">Argument Parser</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> argparse <span class="hljs-keyword">import</span> ArgumentParser |
1320 | 1320 | p = ArgumentParser(description=<str>) <span class="hljs-comment"># Returns a parser object.</span> |
1321 | 1321 | p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span class="hljs-string">'--<name>'</span>, action=<span class="hljs-string">'store_true'</span>) <span class="hljs-comment"># Flag (defaults to False).</span> |
1322 | 1322 | p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span class="hljs-string">'--<name>'</span>, type=<type>) <span class="hljs-comment"># Option (defaults to None).</span> |
|
1330 | 1330 | <ul> |
1331 | 1331 | <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> |
1332 | 1332 | <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> |
1333 | | -<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(<mode>)'</span></code> for files. It accepts 'encoding', but 'newline' is None.</strong></li> |
1334 | 1333 | </ul> |
1335 | 1334 | <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>) |
1336 | 1335 | </code></pre></div> |
|
1339 | 1338 | <ul> |
1340 | 1339 | <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> |
1341 | 1340 | <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> |
1342 | | -<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 | +<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() on every '\n', '\r' and '\r\n'. Passing <code class="python hljs"><span class="hljs-string">'newline="\n"'</span></code> breaks input only on '\n'.</strong></li> |
| 1342 | +<li><strong><code class="python hljs"><span class="hljs-string">'newline="\r\n"'</span></code> converts every '\n' to '\r\n' on write and breaks input on every '\r\n'.</strong></li> |
1343 | 1343 | </ul> |
1344 | 1344 | <div><h3 id="modes">Modes</h3><ul> |
1345 | | -<li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read. Used by default.</strong></li> |
1346 | | -<li><strong><code class="python hljs"><span class="hljs-string">'w'</span></code> - Write. Deletes existing contents.</strong></li> |
1347 | | -<li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - Write or fail if the file already exists.</strong></li> |
| 1345 | +<li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read text from the file. Is used by default.</strong></li> |
| 1346 | +<li><strong><code class="python hljs"><span class="hljs-string">'w'</span></code> - Write to the file. Deletes existing contents.</strong></li> |
| 1347 | +<li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - Write or raise FileExistsError if file exists.</strong></li> |
1348 | 1348 | <li><strong><code class="python hljs"><span class="hljs-string">'a'</span></code> - Append. Creates new file if it doesn't exist.</strong></li> |
1349 | 1349 | <li><strong><code class="python hljs"><span class="hljs-string">'w+'</span></code> - Read and write. Deletes existing contents.</strong></li> |
1350 | | -<li><strong><code class="python hljs"><span class="hljs-string">'r+'</span></code> - Read and write from the start.</strong></li> |
1351 | | -<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li> |
1352 | | -<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode (<code class="python hljs"><span class="hljs-string">'rb'</span></code>, <code class="python hljs"><span class="hljs-string">'wb'</span></code>, <code class="python hljs"><span class="hljs-string">'xb'</span></code>, …).</strong></li> |
| 1350 | +<li><strong><code class="python hljs"><span class="hljs-string">'r+'</span></code> - Read and write from the start of the file.</strong></li> |
| 1351 | +<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end of the file.</strong></li> |
| 1352 | +<li><strong><code class="python hljs"><span class="hljs-string">'rb'</span></code> - Read bytes from the file. Also <code class="python hljs"><span class="hljs-string">'wb'</span></code>, etc.</strong></li> |
1353 | 1353 | </ul><div><h3 id="exceptions-1">Exceptions</h3><ul> |
1354 | 1354 | <li><strong><code class="python hljs"><span class="hljs-string">'FileNotFoundError'</span></code> can be raised when reading with <code class="python hljs"><span class="hljs-string">'r'</span></code> or <code class="python hljs"><span class="hljs-string">'r+'</span></code>.</strong></li> |
1355 | | -<li><strong><code class="python hljs"><span class="hljs-string">'FileExistsError'</span></code> can be raised when writing with <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li> |
| 1355 | +<li><strong><code class="python hljs"><span class="hljs-string">'FileExistsError'</span></code> exception can be raised when writing with <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li> |
1356 | 1356 | <li><strong><code class="python hljs"><span class="hljs-string">'IsADirectoryError'</span></code> and <code class="python hljs"><span class="hljs-string">'PermissionError'</span></code> can be raised by any.</strong></li> |
1357 | 1357 | <li><strong><code class="python hljs"><span class="hljs-string">'OSError'</span></code> is the parent class of all listed exceptions.</strong></li> |
1358 | 1358 | </ul><div><h3 id="fileobject">File Object</h3><pre><code class="python language-python hljs"><file>.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves current position to the start of file.</span> |
|
0 commit comments