simplifications csci 1101b modules and commonsharmon/static/1101/lecs/1101_lec9.pdf ·...

21
Modules and Common Simplifications CSCI 1101B

Upload: others

Post on 04-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Modules and Common Simplifications

CSCI 1101B

Page 2: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Warm-up Exercise

Classic Wire Dilemma Trope

We need a working function that tells us how to disable the device.

Credit: ARMA 2, cobra4v320

Page 3: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Warm-up: Disable the Hacking Device

def get_advice(red_wire, num_wires) instruction = “” if red_wire = True: instruction = “Cut the red wire!” elsif num_wires = 0 or < 0: instruction = “Press the off button!” else: instruction = “Some days you just can’t get rid of a hacking device!” return instruction

Can you find all of the problems with this function?

Page 4: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Warm-up: Disable the Hacking Device

def get_advice(red_wire, num_wires): instruction = “” if red_wire == True: instruction = “Cut the red wire!” elif num_wires == 0 or num_wires < 0: instruction = “Press the off button!” else: instruction = “Some days you just can’t get rid of a hacking device!” return instruction

Page 5: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Today’s Outline

● Common Simplifications

● Making and Importing Modules

Page 6: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Part I

Common Simplifications

Page 7: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Top Factoring

if test: blah() some_thing()else: blah() some_other_thing()

Page 8: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Top Factoring

blah()

if test: some_thing()else: some_other_thing()

Page 9: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Bottom Factoring

if test: some_thing() blah()else: some_other_thing() blah()

Page 10: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Bottom Factoring

if test: some_thing()else: some_other_thing()blah()

Page 11: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Boolean Assignment

if test:

b = True

else:

b = False

Page 12: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Boolean Assignment

if test:

b = True

else:

b = False

(Just say b = test instead!)

Page 13: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Flattening Nested Ifs (usually!)

if a == 0: print(“Not quadratic”)else: if discriminant >= 0: # Compute roots else: print(“Roots are imaginary”)

Page 14: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Flattening Nested Ifs (usually!)

if a == 0: print(“Not quadratic”)elif discriminant < 0: print(“Roots are imaginary”)else: # Compute roots

Page 15: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Temporary Boolean Variables

● Readability rather than simplification

if not ((row >= 0 and row < 8) and (col >= 0 and col < 8)): print(“Invalid chess location”)

vs.

is_valid_row = row >= 0 and row < 8is_valid_col = col >= 0 and col < 8if not is_valid_row or is not is_valid_col: # De Morgan’s laws help too! print(“Invalid chess location”)

Page 16: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Part III

Making and Importing Modules

Page 17: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Different Ways to Import (Review)

● Can import the entire moduleimport mathprint(math.pi) # imported data valueprint(math.sqrt(9)) # imported function

● OR we can import selected data/functionsfrom math import pi, sqrtprint(pi)print(sqrt(9))

Page 18: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Importing Custom Modules

To import your own files, just write:

import your_filename

Page 19: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Modules - Main Function

if __name__ == “__main__”:

Page 20: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Modules - Main Function

def main(): # Main function code, for example… test_1() test_2()

if __name__ == “__main__”: main()

Page 21: Simplifications CSCI 1101B Modules and Commonsharmon/static/1101/lecs/1101_lec9.pdf · 2020-02-24 · Warm-up Exercise Classic Wire Dilemma Trope We need a working function that tells

Summary

● You can import your own files (modules) now!

● For clean code, simplify your conditional statements and use:

if __name__ == “__main__”: