sbs5@hornet:~/cs200/www/lectures$ date Mon Nov 4 02:29:56 PM EST 2024 sbs5@hornet:~/cs200/www/lectures$ pwd /home/accts/sbs5/cs200/www/lectures sbs5@hornet:~/cs200/www/lectures$ p Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>> from exceptions import * >>> f1() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 5, in f1 return 1/0 ZeroDivisionError: division by zero >>> f2() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 9, in f2 return a NameError: name 'a' is not defined >>> f3() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 14, in f3 return a[2] IndexError: list index out of range >>> f4() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 18, in f4 return foo() NameError: name 'foo' is not defined >>> f5() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 22, in f5 for x in open("foo"): FileNotFoundError: [Errno 2] No such file or directory: 'foo' >>> f6() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 27, in f6 import foo ModuleNotFoundError: No module named 'foo' >>> f7() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 32, in f7 return d['key'] KeyError: 'key' >>> f8() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 37, in f8 os.mkdir("/hello") PermissionError: [Errno 13] Permission denied: '/hello' >>> f9() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 43, in f9 f9() File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 43, in f9 f9() File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 43, in f9 f9() [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded >>> sys.getrecursionlimit() 1000 >>> f10() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 48, in f10 g.next() AttributeError: 'range' object has no attribute 'next' >>> f = 9 >>> f.dkdkdk Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute 'dkdkdk' >>> f.property Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute 'property' >>> f11() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 53, in f11 next(g) TypeError: 'range' object is not an iterator >>> x 0 >>> f13() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 64, in f13 x += 1 UnboundLocalError: local variable 'x' referenced before assignment >>> f14() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 76, in f14 raise MyError("oops!") exceptions.MyError: 'oops!' >>> f14a() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 80, in f14a raise Exception("oops!") Exception: oops! >>> f15() Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 84, in f15 raise TypeError("this is a mistake") TypeError: this is a mistake >>> f16() You raised an exception >>> f17(1/2) Traceback (most recent call last): File "", line 1, in TypeError: f17() missing 1 required positional argument: 'y' >>> f17(1,2) result is 0.5 executing finally clause >>> f17(1,0) division by zero! executing finally clause >>> f17('1','2') executing finally clause Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 95, in f17 result = x / y TypeError: unsupported operand type(s) for /: 'str' and 'str' >>> sbs5@hornet:~/cs200/www/lectures$ p Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from exceptions import * >>> f17(1,2) result is 0.5 executing finally clause >>> f17('1', '2') Oops! wrong type! executing finally clause >>> f17b(''cnn.com') File "", line 1 f17b(''cnn.com') ^ SyntaxError: unterminated string literal (detected at line 1) >>> f17b('cnn.com') Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 113, in f17b ufile = urllib.request.urlopen(url) File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.10/urllib/request.py", line 503, in open req = Request(fullurl, data) File "/usr/lib/python3.10/urllib/request.py", line 322, in __init__ self.full_url = url File "/usr/lib/python3.10/urllib/request.py", line 348, in full_url self._parse() File "/usr/lib/python3.10/urllib/request.py", line 377, in _parse raise ValueError("unknown url type: %r" % self.full_url) ValueError: unknown url type: 'cnn.com' >>> f17b('https://cnn.com') 3626953 >>> f17b('https://cnn.com.xxx') 'problem reading url:https://cnn.com.xxx' >>> f18() Error: (, ZeroDivisionError('division by zero'), ) >>> f20(0) >>> f20(1) Traceback (most recent call last): File "", line 1, in File "/home/httpd/html/zoo/classes/cs200/lectures/exceptions.py", line 138, in f20 assert(x % 2 == 0) AssertionError >>> f21() Traceback (most recent call last): File "", line 1, in TypeError: f21() missing 1 required positional argument: 'filename' >>> f21('filename') [Errno 2] No such file or directory: 'filename' File not found FileNotFoundError(2, 'No such file or directory') >>> sbs5@hornet:~/cs200/www/lectures$ exit Process shell finished