Automate the Boring Stuff with Python by Al Sweigart - Ch 8-11

Ace your homework & exams now with Quizwiz!

What is a breakpoint? CH10

A breakpoint is a setting on a line of code that causes the debugger to pause when the program execution reaches the line.

What data structure does a shelf value resemble? CH 8

A shelf value resembles a dictionary value; it has keys and values, along with key() and values() methods that work similarly to the dictionary methods of the same names.

What does an absolute path start with? CH 8

Absolute paths start with the root folder, such as / or C:\

How do you save a Requests response to a file? CH11

After opening the new file on your computer in 'wb' "write binary" mode, use a for loop that iterates over the Response object's iter_content() method to write out chunks to the file. Here's an example: saveFile = open('filename.html', 'wb') for chunk in res.iter_content(100000): saveFile.write(chunk)

After you click Go in the Debug Control window, when will the debugger stop? CH10

After you click Go, the debugger will stop when it has reached the end of the program or a line with a breakpoint.

What happens if an existing file is opened in write mode? CH 8

An existing file opened in write mode is erased and completely overwritten.

In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name? CH 8

C:\bacon\eggs is the dir name, while spam.txt is the base name.

What are the five logging levels? CH10

DEBUG, INFO, WARNING, ERROR, and CRITICAL

What is the keyboard shortcut for opening a browser's developer tools? CH11

F12 brings up the developer tools in Chrome. Pressing CTRL-SHIFT-C (on Windows and Linux) or ⌘-OPTION-C (on OS X) brings up the developer tools in Firefox.

What is a relative path relative to? CH 8

Relative paths are relative to the current working directory.

How can you view (in the developer tools) the HTML of a specific element on a web page? CH11

Right-click the element in the page, and select Inspect Element from the menu.

What are the . and .. folders? CH 8

The . folder is the current folder, and .. is the parent folder.

What are the differences between the Step, Over, and Out buttons in the Debug Control window? CH10

The Step button will move the debugger into a function call. The Over button will quickly execute the function call without stepping into it. The Out button will quickly execute the rest of the code until it steps out of the function it currently is in.

What do the os.getcwd() and os.chdir() funtions do? CH 8

The os.getcwd() function returns the current working directory. The os.chdir() function changes the current working directory.

What Requests method checks that the download worked? CH11

The raise_for_status() method raises an exception if the download had problems and does nothing if the download succeeded.

What is the difference between read() and readlines() methods? CH 8

The read() method returns the file's entire contents as a single string value. The readlines() method returns a list of strings, where each string is a line from the file's contents.

What type of object is returned by request.get()? How can you access the downloaded content as a string value? CH11

The requests.get() function returns a Response object, which has a text attribute that contains the downloaded content as a string.

WHat is the difference between the delete functions in the send2trash and shutil modules? CH9

The send2trash functions will move a file or folder to the recycle bin, while shutil functions will permanently delete files and folders.

What is the difference between shutil.copy() and shutil.copytree()? CH9

The shutil.copy() function will copy a single file, while shutil.copytree() will copy an entire folder, along with all its contents.

What function is used to rename files? CH9

The shutil.move() function is used for renaming files, as well as moving them.

How can you get the HTTP status code of a Requests response? CH11

The status_code attribute of the Response object contains the HTTP status code.

What are the three "mode" arguments that can be passed to the open() function? CH 8

The string 'r' for read mode, 'w' for write mode, and 'a' for append mode.

Briefly describe the difference between the web browser, requests, BeautifulSoup, and selenium modules? CH11

The webbrowser module has an open() method that will launch a web browser to a specific URL, and that's it. The requests module can download files and pages from the Web. The BeautifulSoup module parses HTML. Finally, the selenium module can launch and control a browser.

ZipFile objects have a close() method just like File objects' close() method. What ZipFile method is equivalent to File objects' open() method? CH9

The zipfile.ZipFile() function is equivalent to the open() function; the first argument is the filename, and the second argument is the mode to open the ZIP file in (read, write, or append).

What are the two lines that your program must have in order to be able to call logging.debug()? CH10

To be able to call logging.debug(), you must have these two lines at the start ofyour program: import logging logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

What are the two lines that your program must have in order to have logging.debug() send a logging message to a file named programLog.txt? CH10

To be able to send logging message to a file named programLog.txt with logging.debug(), you must have these two lines at the start of your: import logging >>> logging.basicConfig(filename='programLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

How do you set a breakpoint on a line of code in IDLE? CH10

To set a breakpoint in IDLE, right-click the line and select Set Breakpoint from the context menu.

Why is using logging messages better than upsing print() to display the same message? CH10

You can disable logging messages without removing the logging function calls. You can selectively disable lower-level logging messages. You can create logging messages. Logging messages provides a timestamp.

Write an assert statement that always triggers and AssertionError. CH10

assert False, 'The assertion always triggers.'

Write an assert statement that triggers and AssertionError if the variables eggs and bacon contain strings that are the same as each other, even if their cases are different (that is, 'hello' and 'hello' are considered the same, and 'goodbye' and 'GOODbye' are also considered the same). CH10

assert eggs.lower() != bacon.lower(), 'The eggs and bacon variables are the same!' or assert eggs.upper() != bacon.upper(), 'The eggs and bacon variables are the same!'

Write an assert statement that triggers an AssertionError if the variable spam is an integer less than 10. CH10

assert spam >= 10, 'The spam variable is less than 10.'

What line of code can you add to disable all logging messages in your program? CH10

logging.disable(logging.CRITICAL)


Related study sets

Lesson 13 - Politics, power and violence

View Set

microbiology exam 2 review gene transfer and mutation

View Set

4 - (Questions) Life Insurance Policies - Provisions, Options and Riders

View Set

digestive system (extra questions)

View Set

Peds Exam 3- Intracranial Regulation

View Set

Unit 4: Session 3: Portfolio Management Styles, Strategies, and Techniques

View Set