From b3fb44de1aee4d4c73721ab489c51cde1185d3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 13 Feb 2020 09:13:43 +0100 Subject: [PATCH] Enum, exceptions --- README.md | 28 +++++++++++++++++++--------- index.html | 26 +++++++++++++++++--------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index abbccef..dda418e 100644 --- a/README.md +++ b/README.md @@ -1180,7 +1180,7 @@ class MyOpen(): def __enter__(self): self.file = open(self.filename) return self.file - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exception, traceback): self.file.close() ``` @@ -1329,13 +1329,13 @@ Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon']) Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3}) ``` -#### Functions can not be values, so they must be wrapped: +#### User-defined functions can not be values, so they must be wrapped: ```python from functools import partial LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r), 'OR' : partial(lambda l, r: l or r)}) ``` -* **Another solution in this particular case, is to use `'and_'` and `'or_'` functions from module [operator](#operator).** +* **Another solution in this particular case is to use built-in functions `'and_'` and `'or_'` from the module [operator](#operator).** Exceptions @@ -1381,16 +1381,19 @@ raise ( [, ...]) #### Re-raising caught exception: ```python -except : - +except as : + ... raise ``` -#### Useful built-in exceptions: +### Attributes ```python -raise TypeError('Argument is of wrong type!') -raise ValueError('Argument is of right type but inappropriate value!') -raise RuntimeError('None of above!') +arguments = .args +line_num = .__traceback__.tb_lineno +func_name = .__traceback__.tb_frame.f_code.co_name +filename = .__traceback__.tb_frame.f_code.co_filename +line = linecache.getline(filename, line_num) +error_msg = traceback.format_exc() ``` ### Common Built-in Exceptions @@ -1417,6 +1420,13 @@ BaseException +-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails. ``` +#### Useful built-in exceptions: +```python +raise TypeError('Argument is of wrong type!') +raise ValueError('Argument is of right type but inappropriate value!') +raise RuntimeError('None of above!') +``` + #### Collections and their exceptions: ```text +-----------+------------+------------+------------+ diff --git a/index.html b/index.html index eb4054e..cc6ff4b 100644 --- a/index.html +++ b/index.html @@ -1123,7 +1123,7 @@ Z = dataclasses.make_dataclass('Z', [def __enter__(self): self.file = open(self.filename) return self.file - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exception, traceback): self.file.close() @@ -1256,13 +1256,13 @@ Cutlery = Enum('Cutlery', ['Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3}) -

Functions can not be values, so they must be wrapped:

from functools import partial
+

User-defined functions can not be values, so they must be wrapped:

from functools import partial
 LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
                            'OR' : partial(lambda l, r: l or r)})
 
    -
  • Another solution in this particular case, is to use 'and_' and 'or_' functions from module operator.
  • +
  • Another solution in this particular case is to use built-in functions 'and_' and 'or_' from the module operator.

#Exceptions

Basic Example

try:
     <code>
@@ -1297,14 +1297,17 @@ LogicOp = Enum('LogicOp', {raise <exception>(<el> [, ...])
 
-

Re-raising caught exception:

except <exception>:
-    <code>
+

Re-raising caught exception:

except <exception> as <name>:
+    ...
     raise
 
-

Useful built-in exceptions:

raise TypeError('Argument is of wrong type!')
-raise ValueError('Argument is of right type but inappropriate value!')
-raise RuntimeError('None of above!')
+

Attributes

arguments = <name>.args
+line_num  = <name>.__traceback__.tb_lineno
+func_name = <name>.__traceback__.tb_frame.f_code.co_name
+filename  = <name>.__traceback__.tb_frame.f_code.co_filename
+line      = linecache.getline(filename, line_num)
+error_msg = traceback.format_exc()
 

Common Built-in Exceptions

BaseException
@@ -1329,6 +1332,11 @@ LogicOp = Enum('LogicOp', {Useful built-in exceptions:
raise TypeError('Argument is of wrong type!')
+raise ValueError('Argument is of right type but inappropriate value!')
+raise RuntimeError('None of above!')
+
+

Collections and their exceptions:

+-----------+------------+------------+------------+
 |           |    list    |    dict    |    set     |
 +-----------+------------+------------+------------+
@@ -1854,7 +1862,7 @@ last_el          = op.methodcaller('pop')(<l
 
 
 
-

Attributes

<list> = dir(<object>)               # Returns names of object's attributes (incl. methods).
+

Attributes

<list> = dir(<object>)               # Returns names of object's attributes (incl. methods).
 <dict> = vars(<object>)              # Returns dict of object's fields. Also <object>.__dict__.