Skip to content

Commit 92e5d04

Browse files
committed
Command line arguments, Open
1 parent 00189e4 commit 92e5d04

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ class MyHashable:
11291129
### Sortable
11301130
* **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.**
11311131
* **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.**
1132-
* **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.**
1132+
* **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 re&shy;turned. The shorter sequence is considered smaller in case of all values being equal.**
11331133
* **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")'`.**
11341134

11351135
```python
@@ -1536,7 +1536,7 @@ arguments = sys.argv[1:]
15361536

15371537
### Argument Parser
15381538
```python
1539-
from argparse import ArgumentParser, FileType
1539+
from argparse import ArgumentParser
15401540
p = ArgumentParser(description=<str>) # Returns a parser object.
15411541
p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag (defaults to False).
15421542
p.add_argument('-<short_name>', '--<name>', type=<type>) # Option (defaults to None).
@@ -1549,7 +1549,6 @@ args = p.parse_args() # Exits on par
15491549

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

15541553

15551554
Open
@@ -1561,21 +1560,22 @@ Open
15611560
```
15621561
* **`'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).**
15631562
* **`'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.**
1564-
* **`'newline=""'` means no conversions take place, but input is still broken into chunks by readline() and readlines() on every '\n', '\r' and '\r\n'.**
1563+
* **`'newline=""'` means no conversions take place, but input is still broken into chunks by readline() on every '\n', '\r' and '\r\n'. Passing `'newline="\n"'` breaks input only on '\n'.**
1564+
* **`'newline="\r\n"'` converts every '\n' to '\r\n' on write and breaks input on every '\r\n'.**
15651565

15661566
### Modes
1567-
* **`'r'` - Read. Used by default.**
1568-
* **`'w'` - Write. Deletes existing contents.**
1569-
* **`'x'` - Write or fail if the file already exists.**
1567+
* **`'r'` - Read text from the file. Is used by default.**
1568+
* **`'w'` - Write to the file. Deletes existing contents.**
1569+
* **`'x'` - Write or raise FileExistsError if file exists.**
15701570
* **`'a'` - Append. Creates new file if it doesn't exist.**
15711571
* **`'w+'` - Read and write. Deletes existing contents.**
1572-
* **`'r+'` - Read and write from the start.**
1573-
* **`'a+'` - Read and write from the end.**
1574-
* **`'b'` - Binary mode (`'rb'`, `'wb'`, `'xb'`, …).**
1572+
* **`'r+'` - Read and write from the start of the file.**
1573+
* **`'a+'` - Read and write from the end of the file.**
1574+
* **`'rb'` - Read bytes from the file. Also `'wb'`, etc.**
15751575

15761576
### Exceptions
15771577
* **`'FileNotFoundError'` can be raised when reading with `'r'` or `'r+'`.**
1578-
* **`'FileExistsError'` can be raised when writing with `'x'`.**
1578+
* **`'FileExistsError'` exception can be raised when writing with `'x'`.**
15791579
* **`'IsADirectoryError'` and `'PermissionError'` can be raised by any.**
15801580
* **`'OSError'` is the parent class of all listed exceptions.**
15811581

index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@
965965
<div><h3 id="sortable">Sortable</h3><ul>
966966
<li><strong>With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods (used by &lt;, &gt;, &lt;=, &gt;=) and the rest will be automatically generated.</strong></li>
967967
<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 re­turned. The shorter sequence is considered smaller in case of all values being equal.</strong></li>
969969
<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>
970970
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> total_ordering
971971

@@ -1316,7 +1316,7 @@
13161316
arguments = sys.argv[<span class="hljs-number">1</span>:]
13171317
</code></pre></div>
13181318

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
13201320
p = ArgumentParser(description=&lt;str&gt;) <span class="hljs-comment"># Returns a parser object.</span>
13211321
p.add_argument(<span class="hljs-string">'-&lt;short_name&gt;'</span>, <span class="hljs-string">'--&lt;name&gt;'</span>, action=<span class="hljs-string">'store_true'</span>) <span class="hljs-comment"># Flag (defaults to False).</span>
13221322
p.add_argument(<span class="hljs-string">'-&lt;short_name&gt;'</span>, <span class="hljs-string">'--&lt;name&gt;'</span>, type=&lt;type&gt;) <span class="hljs-comment"># Option (defaults to None).</span>
@@ -1330,7 +1330,6 @@
13301330
<ul>
13311331
<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>
13321332
<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>
1333-
<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(&lt;mode&gt;)'</span></code> for files. It accepts 'encoding', but 'newline' is None.</strong></li>
13341333
</ul>
13351334
<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>)
13361335
</code></pre></div>
@@ -1339,20 +1338,21 @@
13391338
<ul>
13401339
<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>
13411340
<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>
13431343
</ul>
13441344
<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>
13481348
<li><strong><code class="python hljs"><span class="hljs-string">'a'</span></code> - Append. Creates new file if it doesn't exist.</strong></li>
13491349
<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>
13531353
</ul><div><h3 id="exceptions-1">Exceptions</h3><ul>
13541354
<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>
13561356
<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>
13571357
<li><strong><code class="python hljs"><span class="hljs-string">'OSError'</span></code> is the parent class of all listed exceptions.</strong></li>
13581358
</ul><div><h3 id="fileobject">File Object</h3><pre><code class="python language-python hljs">&lt;file&gt;.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves current position to the start of file.</span>

0 commit comments

Comments
 (0)