L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019...

18
Lecture 14: Modules and the Web Craig Zilles (Computer Science) December 6, 2019 https://go.illinois.edu/cs105fa19 CS 105

Transcript of L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019...

Page 1: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Lecture 14: Modules and the Web

Craig Zilles (Computer Science)

December 6, 2019

https://go.illinois.edu/cs105fa19

CS 105

Page 2: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Today1. Modules

• scripts, importing, paths2. Larger Software Development

• Stubs• Unit Testing

3. HTTP / HTML4. Generating HTML dynamically5. Programatically collecting data from the internet6. Closing thoughts7. ICES

2

Page 3: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Modules• One of the main uses of computing is automation

• Often you want to do similar things repeatedly

• Write a piece of code once and use it again and again• Put the code into a file (e.g., useful.py) • Import that file as a module

• import useful• useful.py must be in Python's path

• Path is set of directories where Python will look for file• Most easily done by running Python where the file is

3

Page 4: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

useful.py defines useful_function

After:import usefulHow to call useful_function with "hi" as argument

A) function("hi")B) useful_function("hi")C) useful.function("hi")D) useful.useful_function("hi")E) None of the above

4

Page 5: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

If execute.py includes:useful_function("during loading")

if __name__ == "__main__":

useful_function("WELCOME TO THE MACHINE")

What is printed if I run: import execute

A) NothingB) Just *** during loading ***C) Just *** WELCOME TO THE MACHINE ***D) Both *** during loading *** and

*** WELCOME TO THE MACHINE ***E) An error occurs

5

Page 6: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

If execute.py includes:useful_function("during loading")if __name__ == "__main__":

useful_function("WELCOME TO THE MACHINE")

What is printed if I run: python execute.py

A) NothingB) Just *** during loading ***C) Just *** WELCOME TO THE MACHINE ***D) *** during loading *** then

*** WELCOME TO THE MACHINE ***E) *** WELCOME TO THE MACHINE *** then

*** during loading ***

6

Page 7: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Larger software development• When building larger programs …

• Can't build it all at once: use modular design• Break it up into logical pieces

• Those become modules, objects• Manages complexity

• Test each piece individually• Using unit tests

• Assemble pieces into more and more complete systems

7

Page 8: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Function Stubs• Writing a larger programs, can't write everything at once

• Outline!

def solve_climate_change(world_status):raise NotImplementedError

8

Page 9: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Unit testing• "If it hasn't been tested, then it doesn't work"• Very hard to prove code works in every circumstance

• Pick a cases that are representative of all cases• What inputs should be tested for this code?def compare(x, y):

"return True if x is at least 5 greater than y, False otherwise"

9

Page 10: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Actual test cases for PL questionsclass Test(unittest.TestCase):

@points(1)

@name("Testing with 'hi' and 'there'")

def test_one(self):

r = concatenate_two_strings('hi', 'there')

self.assertEqual(r, 'hithere')

10

Page 11: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Annoucements• Quiz 4 live now through Saturday (12/7)

• Mix of Excel and Python content• Lab next week: Final Exam Preparation• Final Exam

• Almost all Python (can't run Excel in CBTF)• Hiring Course Assistants for Spring: (see prev. mass mail)

• https://bit.ly/2DOag8P• Looking for a video editor – email me

11

Page 12: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Internet Muddiest Points• I confused by the difference between HTTP and HTML• What is the difference between http and https?• I am curious what someone could do if they have

someone else's IP address? Is that potentially dangerous?

• Why are three languages are used for websites• I am confused about the nested writing and why you

need to essentially "open and close" everything that you're writing.

• kinda confused about nesting in html• Do I have to do something like Figure 18.5.1 to create a

website? Is that the normal procedure?

12

Page 13: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

The Internet• Is kind of important• I'm hoping you get the big picture

• Hostname -> name lookup -> IP address• Top-level domains

• You can buy your own domain• URL structure: (three parts: scheme, hostname, path)https://illinois.edu/assets/img/navigation/submenu_about.jpg

• HyperText Transport Protocol (HTTP)

13

Page 14: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

HTML5 = HTML, CSS, Javascript• Separation of Concerns

• HTML = content, CSS = styling, JS = interactivity• HTML documents are hierarchical, cause web pages are

• Elements have begin/end tags• Elements can be nested in other elements

• CSS consist of:• Selectors that specify which elements • Attribute : value pairs

• Like a Python dictionary• Javascript is a lot like Python but looks pretty different

• variables, expressions, functions, conditionals, loops14

Page 15: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Generating Web Pages Dynamically• Amazon.com doesn't have people write web pages for

each product• They are generated on the fly by a computer program

1. Templates: basically like Python format stringsWrite a Python statement that assigns the variable {{params.varname}} to the value {{params.value}}.

2. Looping through collections• To make lists, etc.

15

Page 16: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Reading data from the internet• From a Python program

• urllib module• Given a URL, returns the document at that URL

16

Page 17: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Closing Thoughts• I tried to give you an introduction to computing:

1. Understanding of the precision required by computers2. Practice with the basic concepts of programs:

• variables, expressions, functions, conditionals, loops• numbers, strings, lists, dictionaries

3. Exposure to the mindset of computer scientists• nesting, managing complexity, trouble-shooting

4. Exposure to important domains:• Spreadsheets, internet/HTML

17

Page 18: L14 Finale - University Of Illinois€¦ · L14_Finale Author: Craig Zilles Created Date: 12/6/2019 11:30:26 PM ...

Learning Objectives• Given a small (10-15 line) Python program, students should be

able to execute the program in their head and correctly predict the outcome.

• Given a small (5-10 line) Python function, students should be able to provide a natural language description of what the function does.

• Students should be able to write short (10-20 line) Python programs, including the use of variables, expressions, conditionals, loops, and functions.

• Students should be able to use built-in Python data structures including strings, lists, and dictionaries.

• Students should be aware of intermediate programming concepts including recursion, exception handling, user-defined types (classes)

• Students should be capable of basic spreadsheet modifications and writing simple HTML documents

18