Browse Source

Duck types, Open, Paths, CSV

pull/144/merge
Jure Šorn 6 months ago
parent
commit
d736e7a7ab
2 changed files with 20 additions and 20 deletions
  1. 18
      README.md
  2. 22
      index.html

18
README.md

@ -1275,7 +1275,7 @@ class MyCollection:
* **Only required methods are getitem() and len().**
* **Getitem() should return an item at the passed index or raise IndexError.**
* **Iter() and contains() automatically work on any object that has getitem() defined.**
* **Reversed() automatically works on any object that has getitem() and len() defined.**
* **Reversed() automatically works on any object that has getitem() and len() defined. It returns reversed iterator of object's items.**
```python
class MySequence:
def __init__(self, a):
@ -1583,7 +1583,7 @@ Open
* **`'w+'` - Read and write. Deletes existing contents.**
* **`'r+'` - Read and write from the start.**
* **`'a+'` - Read and write from the end.**
* **`'b'` - Binary mode (`'br'`, `'bw'`, `'bx'`, …)**
* **`'b'` - Binary mode (`'rb'`, `'wb'`, `'xb'`, …)**
### Exceptions
* **`'FileNotFoundError'` can be raised when reading with `'r'` or `'r+'`.**
@ -1682,10 +1682,10 @@ from pathlib import Path
```
```python
<Path> = Path() # Returns relative cwd. Also Path('.').
<Path> = Path.cwd() # Returns absolute cwd. Also Path().resolve().
<Path> = Path() # Returns relative CWD. Also Path('.').
<Path> = Path.cwd() # Returns absolute CWD. Also Path().resolve().
<Path> = Path.home() # Returns user's home directory (absolute).
<Path> = Path(__file__).resolve() # Returns script's path if cwd wasn't changed.
<Path> = Path(__file__).resolve() # Returns script's path if CWD wasn't changed.
```
```python
@ -1868,16 +1868,16 @@ import csv
### Read Rows from CSV File
```python
def read_csv_file(filename, dialect='excel', **params):
def read_csv_file(filename, **csv_params):
with open(filename, encoding='utf-8', newline='') as file:
return list(csv.reader(file, dialect, **params))
return list(csv.reader(file, **csv_params))
```
### Write Rows to CSV File
```python
def write_to_csv_file(filename, rows, mode='w', dialect='excel', **params):
def write_to_csv_file(filename, rows, mode='w', **csv_params):
with open(filename, mode, encoding='utf-8', newline='') as file:
writer = csv.writer(file, dialect, **params)
writer = csv.writer(file, **csv_params)
writer.writerows(rows)
```

22
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>October 9, 2024</aside>
<aside>October 13, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1079,7 +1079,7 @@ Hello World!
<li><strong>Only required methods are getitem() and len().</strong></li>
<li><strong>Getitem() should return an item at the passed index or raise IndexError.</strong></li>
<li><strong>Iter() and contains() automatically work on any object that has getitem() defined.</strong></li>
<li><strong>Reversed() automatically works on any object that has getitem() and len() defined.</strong></li>
<li><strong>Reversed() automatically works on any object that has getitem() and len() defined. It returns reversed iterator of object's items.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MySequence</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
self.a = a
@ -1339,7 +1339,7 @@ args = p.parse_args() <span class="h
<li><strong><code class="python hljs"><span class="hljs-string">'w+'</span></code> - Read and write. Deletes existing contents.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'r+'</span></code> - Read and write from the start.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode (<code class="python hljs"><span class="hljs-string">'br'</span></code>, <code class="python hljs"><span class="hljs-string">'bw'</span></code>, <code class="python hljs"><span class="hljs-string">'bx'</span></code>, …)</strong></li>
<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>
</ul><div><h3 id="exceptions-1">Exceptions</h3><ul>
<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>
<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>
@ -1412,10 +1412,10 @@ args = p.parse_args() <span class="h
&lt;Path&gt; = &lt;Path&gt;.resolve() <span class="hljs-comment"># Returns absolute path with resolved symlinks.</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;Path&gt; = Path() <span class="hljs-comment"># Returns relative cwd. Also Path('.').</span>
&lt;Path&gt; = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Also Path().resolve().</span>
<pre><code class="python language-python hljs">&lt;Path&gt; = Path() <span class="hljs-comment"># Returns relative CWD. Also Path('.').</span>
&lt;Path&gt; = Path.cwd() <span class="hljs-comment"># Returns absolute CWD. Also Path().resolve().</span>
&lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span>
&lt;Path&gt; = Path(__file__).resolve() <span class="hljs-comment"># Returns script's path if cwd wasn't changed.</span>
&lt;Path&gt; = Path(__file__).resolve() <span class="hljs-comment"># Returns script's path if CWD wasn't changed.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without the final component.</span>
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
@ -1549,14 +1549,14 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<div><h3 id="readrowsfromcsvfile">Read Rows from CSV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_csv_file</span><span class="hljs-params">(filename, dialect=<span class="hljs-string">'excel'</span>, **params)</span>:</span>
<div><h3 id="readrowsfromcsvfile">Read Rows from CSV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_csv_file</span><span class="hljs-params">(filename, **csv_params)</span>:</span>
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
<span class="hljs-keyword">return</span> list(csv.reader(file, dialect, **params))
<span class="hljs-keyword">return</span> list(csv.reader(file, **csv_params))
</code></pre></div>
<div><h3 id="writerowstocsvfile">Write Rows to CSV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_csv_file</span><span class="hljs-params">(filename, rows, mode=<span class="hljs-string">'w'</span>, dialect=<span class="hljs-string">'excel'</span>, **params)</span>:</span>
<div><h3 id="writerowstocsvfile">Write Rows to CSV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_csv_file</span><span class="hljs-params">(filename, rows, mode=<span class="hljs-string">'w'</span>, **csv_params)</span>:</span>
<span class="hljs-keyword">with</span> open(filename, mode, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
writer = csv.writer(file, dialect, **params)
writer = csv.writer(file, **csv_params)
writer.writerows(rows)
</code></pre></div>
@ -2929,7 +2929,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>October 9, 2024</aside>
<aside>October 13, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

|||||||
100:0
Loading…
Cancel
Save