(base) bash-5.2$ date Wed Nov 8 02:33:51 PM EST 2023 (base) bash-5.2$ pwd /home/accts/sbs5/cs200/www/lectures (base) bash-5.2$ p Python 3.11.6 (main, Oct 3 2023, 00:00:00) [GCC 12.3.1 20230508 (Red Hat 12.3.1-1)] 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! >>> def f2(): ... return a ... >>> f2() Traceback (most recent call last): File "", line 1, in File "", line 2, in f2 NameError: name 'a' is not defined >>> a = 2 >>> f2() 2 >>> a = [1,2] >>> a [1, 2] >>> a[2] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range >>> x = 0 def f13(): x += 1 return x >>> ... ... ... >>> f13() Traceback (most recent call last): File "", line 1, in File "", line 2, in f13 UnboundLocalError: cannot access local variable 'x' where it is not associated with a value >>> def ff(): ... x+= 1 ... return x ... >>> ff() Traceback (most recent call last): File "", line 1, in File "", line 2, in ff UnboundLocalError: cannot access local variable 'x' where it is not associated with a value >>> def fff(): ... return x ... >>> fff() 0 >>> import dis >>> dir(dis) ['BINARY_OP', 'Bytecode', 'CACHE', 'COMPILER_FLAG_NAMES', 'EXTENDED_ARG', 'FORMAT_VALUE', 'FORMAT_VALUE_CONVERTERS', 'HAVE_ARGUMENT', 'Instruction', 'JUMP_BACKWARD', 'LOAD_CONST', 'LOAD_GLOBAL', 'MAKE_FUNCTION', 'MAKE_FUNCTION_FLAGS', 'Positions', 'UNKNOWN', '_ExceptionTableEntry', '_INT_BITS', '_INT_OVERFLOW', '_Instruction', '_OPARG_WIDTH', '_OPNAME_WIDTH', '_Unknown', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_all_opmap', '_all_opname', '_cache_format', '_deoptop', '_disassemble_bytes', '_disassemble_recursive', '_disassemble_str', '_empty_slot', '_find_imports', '_find_store_names', '_format_code_info', '_get_code_array', '_get_code_object', '_get_const_info', '_get_const_value', '_get_instructions_bytes', '_get_name_info', '_have_code', '_inline_cache_entries', '_is_backward_jump', '_nb_ops', '_parse_exception_table', '_parse_varint', '_specializations', '_specialized_instructions', '_test', '_try_compile', '_unpack_opargs', 'cmp_op', 'code_info', 'collections', 'deoptmap', 'dis', 'disassemble', 'disco', 'distb', 'findlabels', 'findlinestarts', 'get_instructions', 'hascompare', 'hasconst', 'hasfree', 'hasjabs', 'hasjrel', 'haslocal', 'hasname', 'hasnargs', 'io', 'opmap', 'opname', 'pretty_flags', 'show_code', 'spec_op', 'specialized', 'stack_effect', 'sys', 'types'] >>> dis.code_info(f13) 'Name: f13\nFilename: \nArgument count: 0\nPositional-only arguments: 0\nKw-only arguments: 0\nNumber of locals: 1\nStack size: 2\nFlags: OPTIMIZED, NEWLOCALS\nConstants:\n 0: None\n 1: 1\nVariable names:\n 0: x' >>> f13 >>> print (dis.code_info(f13)) Name: f13 Filename: Argument count: 0 Positional-only arguments: 0 Kw-only arguments: 0 Number of locals: 1 Stack size: 2 Flags: OPTIMIZED, NEWLOCALS Constants: 0: None 1: 1 Variable names: 0: x >>> print (dis.code_info(f)) Traceback (most recent call last): File "", line 1, in NameError: name 'f' is not defined. Did you mean: 'f2'? >>> print (dis.code_info(f2)) Name: f2 Filename: Argument count: 0 Positional-only arguments: 0 Kw-only arguments: 0 Number of locals: 0 Stack size: 1 Flags: OPTIMIZED, NEWLOCALS Constants: 0: None Names: 0: a >>> dis.dis(f13) 1 0 RESUME 0 2 2 LOAD_FAST 0 (x) 4 LOAD_CONST 1 (1) 6 BINARY_OP 13 (+=) 10 STORE_FAST 0 (x) 3 12 LOAD_FAST 0 (x) 14 RETURN_VALUE >>> def s3e(): a = 6 b = 3 return (a / b) ... ... ... ... >>> s3e() 2.0 >>> dis.dis(s3e) 1 0 RESUME 0 2 2 LOAD_CONST 1 (6) 4 STORE_FAST 0 (a) 3 6 LOAD_CONST 2 (3) 8 STORE_FAST 1 (b) 4 10 LOAD_FAST 0 (a) 12 LOAD_FAST 1 (b) 14 BINARY_OP 11 (/) 18 RETURN_VALUE >>> def y(): a = 7 if a % 2: return 1 + 3*a else: return a / 2 ... File "", line 2 a = 7 ^ IndentationError: expected an indented block after function definition on line 1 >>> ... File "", line 2 return 1 + 3*a ^ IndentationError: expected an indented block after 'if' statement on line 1 >>> File "", line 1 else: ^^^^ SyntaxError: invalid syntax >>> File "", line 1 SyntaxError: 'return' outside function >>> >>> def y(): ... a = 7 ... if a%2: ... return 1 + 3*a ... else: ... return a / 2 ... >>> dis.dis(y) 1 0 RESUME 0 2 2 LOAD_CONST 1 (7) 4 STORE_FAST 0 (a) 3 6 LOAD_FAST 0 (a) 8 LOAD_CONST 2 (2) 10 BINARY_OP 6 (%) 14 POP_JUMP_FORWARD_IF_FALSE 8 (to 32) 4 16 LOAD_CONST 3 (1) 18 LOAD_CONST 4 (3) 20 LOAD_FAST 0 (a) 22 BINARY_OP 5 (*) 26 BINARY_OP 0 (+) 30 RETURN_VALUE 6 >> 32 LOAD_FAST 0 (a) 34 LOAD_CONST 2 (2) 36 BINARY_OP 11 (/) 40 RETURN_VALUE >>> def x (): ... return [n for n in range(10)] ... >>> x() [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> dis.dis(x) 1 0 RESUME 0 2 2 LOAD_CONST 1 ( at 0x7f1a284c30f0, file "", line 2>) 4 MAKE_FUNCTION 0 6 LOAD_GLOBAL 1 (NULL + range) 18 LOAD_CONST 2 (10) 20 PRECALL 1 24 CALL 1 34 GET_ITER 36 PRECALL 0 40 CALL 0 50 RETURN_VALUE Disassembly of at 0x7f1a284c30f0, file "", line 2>: 2 0 RESUME 0 2 BUILD_LIST 0 4 LOAD_FAST 0 (.0) >> 6 FOR_ITER 4 (to 16) 8 STORE_FAST 1 (n) 10 LOAD_FAST 1 (n) 12 LIST_APPEND 2 14 JUMP_BACKWARD 5 (to 6) >> 16 RETURN_VALUE >>> (base) bash-5.2$ pwd /home/accts/sbs5/cs200/www/lectures (base) bash-5.2$ tr -sc a-zA-Z '\n' < ../UNIX.html | head html head LINK href http zoo cs yale edu (base) bash-5.2$ head ../UNIX.html UNIX for CS 200/201 - Fall 2023.

UNIX for CS 200/201 - Fall 2023.

(base) bash-5.2$ tr -sc a-zA-Z '\n' < ../UNIX.html | sort | head a a a a a a a a a (base) bash-5.2$ tr -sc a-zA-Z '\n' < ../UNIX.html | sort | uniq | head a A about above abrt ABRT access accessed accts (base) bash-5.2$ tr -sc a-zA-Z '\n' < ../UNIX.html | sort | uniq -c | head 1 138 a 9 A 5 about 3 above 4 abrt 1 ABRT 6 access 2 accessed 18 accts (base) bash-5.2$ pwd /home/accts/sbs5/cs200/www/lectures (base) bash-5.2$ p Python 3.11.6 (main, Oct 3 2023, 00:00:00) [GCC 12.3.1 20230508 (Red Hat 12.3.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def do_twice(func): ''' Decorator that executes a function twice and returns the results as a 2-tuple''' @wraps(func) def g(*args): val1 = func(*args) val2 = func(*args) return (val1, val2) return g ... ... ... ... ... ... ... ... >>> @do_twice def add2(n): ''' add 2 to the input''' return n+2 ... ... ... ... Traceback (most recent call last): File "", line 1, in File "", line 3, in do_twice NameError: name 'wraps' is not defined >>> from functools import wraps >>> @do_twice def add2(n): ''' add 2 to the input''' return n+2 ... ... ... ... >>> add2(4) (6, 6) >>> import random def roll_dice(): ''' roll a fair die - a random value between 1 and 6 inclusive.''' return random.randint(1,6) >>> ... ... ... >>> roll_dice() 6 >>> roll_dice() 6 >>> roll_dice() 6 >>> roll_dice() 4 >>> roll_dice() 2 >>> roll_dice() 2 >>> roll_dice() 2 >>> roll_dice() 1 >>> roll_dice() 6 >>> roll_dice() 4 >>> @do_twice def roll_dice(): ''' roll a fair die - a random value between 1 and 6 inclusive.''' return random.randint(1,6) ... ... ... ... >>> roll_dice() (6, 3) >>> roll_dice() (5, 2) >>> roll_dice() (1, 3) >>> roll_dice() (5, 2) >>> def roll_sixes(): number = random.randint(1,6) if number != 6: raise ValueError(number) return number ... ... ... ... ... >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 1 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 4 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 5 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 2 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 4 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 1 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 4 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 1 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 3 >>> roll_sixes() Traceback (most recent call last): File "", line 1, in File "", line 4, in roll_sixes ValueError: 2 >>> roll_sixes() 6 >>> from retry import * >>> roll_sixes() Retrying (4) Retrying (1) Retrying (1) Retrying (3) Retrying (5) Retrying (2) 6 >>> roll_sixes() Retrying (1) Retrying (4) 6 >>> roll_sixes() Retrying (4) Retrying (2) Retrying (2) Retrying (5) Retrying (5) 6 >>> roll_sixes() 6 >>> d={} >>> d[(1,2)] = 7 >>> d {(1, 2): 7} >>> d[(2,1)] = 9 >>> d {(1, 2): 7, (2, 1): 9} >>> (base) bash-5.2$ exit Process shell finished