From c81aad93248cff318a55b17343d6e600f1a8d1b2 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 12:37:53 +0200 Subject: [PATCH 1/2] add Python 'Error' question + answer + Exception ansers --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f3c886c..c4df85d 100644 --- a/README.md +++ b/README.md @@ -1672,6 +1672,7 @@ The immutable data types are: String Bool Tuple + Frozenset You can usually use the function hash() to check an object mutability, if it is hashable it is immutable, although this does not always work as intended as user defined objects might be mutable and hashable @@ -1760,7 +1761,70 @@ There is a more advanced python feature called MetaClasses that aid the programm
-What is an exception? What types of exceptions are you familiar with?
+ What is an error? What is an exception? What types of exceptions are you familiar with?
+ +``` + +# Note that you generally don't need to know the compiling process but knowing where everything comes from +# and giving complete answers shows that you truly know what you are talking about. + +Generally, every compiling process have a two stops. + - Analysis + - Code Generation. + + Analysis can be broken into: + 1. Lexycal analysis (Tokenizes source code) + 2. Syntactic analysis (Check whether the tokens are legal or not, tldr, if syntax is correct) + + for i in 'foo' + ^ + SyntaxError: invalid syntax + + We missed ':' + + + 3. Semantic analysis (Contextual analysis, legal syntax can still trigger errors, did you try to divide by 0, + hash a mutable object or use an undeclared function?) + + 1/0 + ZeroDivisionError: division by zero + + These three analysis steps are the responsible for error handlings. + + The second step would be responsible for errors, mostly syntax errors, the most common error. + The third step would be responsible for Exceptions. + + As we have seen, Exceptions are semantic errors, there are many builtin Exceptions: + + ImportError + ValueError + KeyError + FileNotFoundError + IndentationError + IndexError + ... + + You can also have user defined Exceptions that have to inherit from the `Exception` class, directly or indirectly. + + Basic example: + + class DividedBy2Error(Exception): + def __init__(self, message): + self.message = message + + + def division(dividend,divisor): + if divisor == 2: + raise DividedBy2Error('I dont want you to divide by 2!') + return dividend / divisor + + division(100, 2) + + >>> __main__.DividedBy2Error: I dont want you to divide by 2! + +``` + +
@@ -2151,7 +2215,7 @@ func main() {
It looks what unicode value is set at 101 and uses it for converting the integer to a string. -If you want to get "101" you should use the package "strconv" and repalce y = string(x) with y = strconv.Itoa(x) +If you want to get "101" you should use the package "strconv" and replace y = string(x) with y = strconv.Itoa(x)
## Mongo From 84ab2b0362cb9a637a28cc2cd78e25c68caa062f Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 13:29:48 +0200 Subject: [PATCH 2/2] Fix typo --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c4df85d..ce4396e 100644 --- a/README.md +++ b/README.md @@ -1768,7 +1768,7 @@ There is a more advanced python feature called MetaClasses that aid the programm # Note that you generally don't need to know the compiling process but knowing where everything comes from # and giving complete answers shows that you truly know what you are talking about. -Generally, every compiling process have a two stops. +Generally, every compiling process have a two steps. - Analysis - Code Generation. @@ -1912,6 +1912,7 @@ def get_food(brothers_menu) -> set: return set(temp) +# One liner way (Using list comprehension) set([food for bro in x for food in bro['food']]) ```