Chapter 9 Flashcards

1
Q

By default, command line arguments are stored…

The first element of this list is…

A

…in the list sys.argv, which is stored in the standard library sys module, with one string element for each argument.

…the name of the program executable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a usage message?

A

example

A usage message is implemented by the programmer so that when an error occurs relating to the number of arguments called, an example of the correct format to be used is printed.

if len(sys.argv[] != 3)
#print usage message here
sys.exit(1) #exit w/ error code 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a script?

A

A script is a file of (Python) code that the programmer then passes to the interpreter as input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a module?

A

A module is a file containing Python code that can be imported and used by different files/scripts (i.e.: import math).

The import function executes the module and thus defines its contents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain what happens step by step when importing a module:

A

1). A check is performed to see if the module has already been imported. If it has, the already-imported module will be used.
2). If not already imported, a new module object is created and stored in sys.modules.
3).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is true about the value of __name__ when using modules?

A

The __name__ variable of any imported module contains the name of the module.

The value of the __name__ variable of the executing script is always “__main__”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the reload function and when might you use it?

A

The reload() function is used to refresh the contents of a module, and is best used in situations where information needs to be continuously updated, but it is not desirable to restart the entire program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a package?

A

A package is a directory that, when imported, gives access to all modules located in the directory. This is favorable to importing each module one at a time.

Packages can contain subpackages, which themselves can contain modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How might you organize many different modules when dealing with a large project?

A

Sort them into packages that group related modules together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a statement that imports a module “barn” from a subpackage “buildings”, which itself is contained in a package “ASCIIArt”.

A

from ASCIIArt.buildings import barn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

This contains various utilities and tools for performing common program behaviors:

A

The Python standard library, which contains useful modules such as math and datetime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does file.read() do?

A

Returns the contents of a file as a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does file.readlines() do?

A

Returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Both file.read() and file.readlines() can be given an optional argument that…

A

…specifies the number of bytes to be read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does file.readline() do?

A

Returns the contents of a file one line at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you open a file?
What must you specify when calling this function?

A

open(“filename”, mode)

You must remember to specify the mode.

17
Q

How do you write to a file?
What can be passed into this function?

A

file.write(“contents”, mode)

Only strings can be passed to this function.
Integers and floats must be converted using str()

18
Q

What are the different modes when opening a file?
How can you specify an update mode?

A

‘r’ : open the file for reading.
‘w’ : open the file for writing.
‘a’ : open the file for appending.

You can specify an update mode by adding a ‘+’ suffix to the mode. For example, ‘r+’ or ‘w+’.
This allows for reading and writing of the file at the same time.

19
Q

What does the flush() function do?
What else functionally achieves this?

A

Forces the interpreter to flush the output buffer to the disk.

Closing the file has the same effect.

20
Q

True or false: The write() method immediately writes data to a file.

A

Output is buffered, thus there may be a delay between calling the write method and writing data to the disk.

21
Q

What is a key use of the os module we learned in this chapter?

A

os can be used to directly interact with the operating system and - thus - file directories, which is useful when handling external files.

22
Q
A
23
Q

Portability refers to…

A

…the ability to easily access items from multiple locations in a program.