Chapter 9 Flashcards
By default, command line arguments are stored…
The first element of this list is…
…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
What is a usage message?
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
What is a script?
A script is a file of (Python) code that the programmer then passes to the interpreter as input.
What is a module?
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.
Explain what happens step by step when importing a module:
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).
What is true about the value of __name__ when using modules?
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__”
What is the purpose of the reload function and when might you use it?
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.
What is a package?
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 might you organize many different modules when dealing with a large project?
Sort them into packages that group related modules together.
Write a statement that imports a module “barn” from a subpackage “buildings”, which itself is contained in a package “ASCIIArt”.
from ASCIIArt.buildings import barn
This contains various utilities and tools for performing common program behaviors:
The Python standard library, which contains useful modules such as math and datetime.
What does file.read() do?
Returns the contents of a file as a string.
What does file.readlines() do?
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…
Both file.read() and file.readlines() can be given an optional argument that…
…specifies the number of bytes to be read
What does file.readline() do?
Returns the contents of a file one line at a time.
How do you open a file?
What must you specify when calling this function?
open(“filename”, mode)
You must remember to specify the mode.
How do you write to a file?
What can be passed into this function?
file.write(“contents”, mode)
Only strings can be passed to this function.
Integers and floats must be converted using str()
What are the different modes when opening a file?
How can you specify an update mode?
‘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.
What does the flush() function do?
What else functionally achieves this?
Forces the interpreter to flush the output buffer to the disk.
Closing the file has the same effect.
True or false: The write() method immediately writes data to a file.
Output is buffered, thus there may be a delay between calling the write method and writing data to the disk.
What is a key use of the os module we learned in this chapter?
os can be used to directly interact with the operating system and - thus - file directories, which is useful when handling external files.
Portability refers to…
…the ability to easily access items from multiple locations in a program.