Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3fb44d

Browse files
committed
Enum, exceptions
1 parent f35b6c9 commit b3fb44d

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ class MyOpen():
11801180
def __enter__(self):
11811181
self.file = open(self.filename)
11821182
return self.file
1183-
def __exit__(self, exc_type, exc_value, traceback):
1183+
def __exit__(self, exc_type, exception, traceback):
11841184
self.file.close()
11851185
```
11861186

@@ -1329,13 +1329,13 @@ Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon'])
13291329
Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
13301330
```
13311331

1332-
#### Functions can not be values, so they must be wrapped:
1332+
#### User-defined functions can not be values, so they must be wrapped:
13331333
```python
13341334
from functools import partial
13351335
LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
13361336
'OR' : partial(lambda l, r: l or r)})
13371337
```
1338-
* **Another solution in this particular case, is to use `'and_'` and `'or_'` functions from module [operator](#operator).**
1338+
* **Another solution in this particular case is to use built-in functions `'and_'` and `'or_'` from the module [operator](#operator).**
13391339

13401340

13411341
Exceptions
@@ -1381,16 +1381,19 @@ raise <exception>(<el> [, ...])
13811381

13821382
#### Re-raising caught exception:
13831383
```python
1384-
except <exception>:
1385-
<code>
1384+
except <exception> as <name>:
1385+
...
13861386
raise
13871387
```
13881388

1389-
#### Useful built-in exceptions:
1389+
### Attributes
13901390
```python
1391-
raise TypeError('Argument is of wrong type!')
1392-
raise ValueError('Argument is of right type but inappropriate value!')
1393-
raise RuntimeError('None of above!')
1391+
arguments = <name>.args
1392+
line_num = <name>.__traceback__.tb_lineno
1393+
func_name = <name>.__traceback__.tb_frame.f_code.co_name
1394+
filename = <name>.__traceback__.tb_frame.f_code.co_filename
1395+
line = linecache.getline(filename, line_num)
1396+
error_msg = traceback.format_exc()
13941397
```
13951398

13961399
### Common Built-in Exceptions
@@ -1417,6 +1420,13 @@ BaseException
14171420
+-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
14181421
```
14191422

1423+
#### Useful built-in exceptions:
1424+
```python
1425+
raise TypeError('Argument is of wrong type!')
1426+
raise ValueError('Argument is of right type but inappropriate value!')
1427+
raise RuntimeError('None of above!')
1428+
```
1429+
14201430
#### Collections and their exceptions:
14211431
```text
14221432
+-----------+------------+------------+------------+

index.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@
11231123
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__enter__</span><span class="hljs-params">(self)</span>:</span>
11241124
self.file = open(self.filename)
11251125
<span class="hljs-keyword">return</span> self.file
1126-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__exit__</span><span class="hljs-params">(self, exc_type, exc_value, traceback)</span>:</span>
1126+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__exit__</span><span class="hljs-params">(self, exc_type, exception, traceback)</span>:</span>
11271127
self.file.close()
11281128
</code></pre></div>
11291129

@@ -1256,13 +1256,13 @@
12561256
Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, {<span class="hljs-string">'fork'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'knife'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'spoon'</span>: <span class="hljs-number">3</span>})
12571257
</code></pre></div>
12581258

1259-
<div><h4 id="functionscannotbevaluessotheymustbewrapped">Functions can not be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
1259+
<div><h4 id="userdefinedfunctionscannotbevaluessotheymustbewrapped">User-defined functions can not be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
12601260
LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">and</span> r),
12611261
<span class="hljs-string">'OR'</span> : partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
12621262
</code></pre></div>
12631263

12641264
<ul>
1265-
<li><strong>Another solution in this particular case, is to use <code class="python hljs"><span class="hljs-string">'and_'</span></code> and <code class="python hljs"><span class="hljs-string">'or_'</span></code> functions from module <a href="#operator">operator</a>.</strong></li>
1265+
<li><strong>Another solution in this particular case is to use built-in functions <code class="python hljs"><span class="hljs-string">'and_'</span></code> and <code class="python hljs"><span class="hljs-string">'or_'</span></code> from the module <a href="#operator">operator</a>.</strong></li>
12661266
</ul>
12671267
<div><h2 id="exceptions"><a href="#exceptions" name="exceptions">#</a>Exceptions</h2><div><h3 id="basicexample">Basic Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
12681268
&lt;code&gt;
@@ -1297,14 +1297,17 @@
12971297
<span class="hljs-keyword">raise</span> &lt;exception&gt;(&lt;el&gt; [, ...])
12981298
</code></pre></div>
12991299

1300-
<div><h4 id="reraisingcaughtexception">Re-raising caught exception:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;:
1301-
&lt;code&gt;
1300+
<div><h4 id="reraisingcaughtexception">Re-raising caught exception:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt; <span class="hljs-keyword">as</span> &lt;name&gt;:
1301+
...
13021302
<span class="hljs-keyword">raise</span>
13031303
</code></pre></div>
13041304

1305-
<div><h4 id="usefulbuiltinexceptions">Useful built-in exceptions:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">'Argument is of wrong type!'</span>)
1306-
<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'Argument is of right type but inappropriate value!'</span>)
1307-
<span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">'None of above!'</span>)
1305+
<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs">arguments = &lt;name&gt;.args
1306+
line_num = &lt;name&gt;.__traceback__.tb_lineno
1307+
func_name = &lt;name&gt;.__traceback__.tb_frame.f_code.co_name
1308+
filename = &lt;name&gt;.__traceback__.tb_frame.f_code.co_filename
1309+
line = linecache.getline(filename, line_num)
1310+
error_msg = traceback.format_exc()
13081311
</code></pre></div>
13091312

13101313
<div><h3 id="commonbuiltinexceptions">Common Built-in Exceptions</h3><pre><code class="text language-text">BaseException
@@ -1329,6 +1332,11 @@
13291332
+-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
13301333
</code></pre></div>
13311334

1335+
<div><h4 id="usefulbuiltinexceptions">Useful built-in exceptions:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">'Argument is of wrong type!'</span>)
1336+
<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'Argument is of right type but inappropriate value!'</span>)
1337+
<span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">'None of above!'</span>)
1338+
</code></pre></div>
1339+
13321340
<div><h4 id="collectionsandtheirexceptions">Collections and their exceptions:</h4><pre><code class="text language-text">+-----------+------------+------------+------------+
13331341
| | list | dict | set |
13341342
+-----------+------------+------------+------------+
@@ -1854,7 +1862,7 @@
18541862

18551863

18561864

1857-
<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir(&lt;object&gt;) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span>
1865+
<div><h3 id="attributes-2">Attributes</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir(&lt;object&gt;) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span>
18581866
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Returns dict of object's fields. Also &lt;object&gt;.__dict__.</span>
18591867
</code></pre></div>
18601868

0 commit comments

Comments
 (0)