cs2304: creating modules and packages - virginia...
TRANSCRIPT
CS2304: Python for Java Programmers
Monti 2014
CS2304: Creating Modules and Packages
CS2304: Python for Java Programmers
Monti 2014
Modules and Packages
• At some point, you’ll likely want to create larger programs that span multiple files or provide your own library.
• There are a few way to do that in Python: modules and packages.
• Modules are simply Python files and are very straight forward to use. • Normally, these are in the same directory as your “main”
program. • Built-in modules also exist as part of the Python install.
• Packages are a bit more complex, we’ll just briefly look at how you can use them.
CS2304: Python for Java Programmers
Monti 2014
Using Modules
• Modules are simply .py files, that can contain functions, types (classes), etc.
• Let’s say module.py contains:
• How do we go about using this module’s functions or variables?
MODULEVAR = 10 def get_name(): return "Hello"
CS2304: Python for Java Programmers
Monti 2014
Enter the Import Statement
• The import statement will let us access other modules.
• Both when we are creating our own modules or using Python’s built-in modules.
• Let’s say we want to use the module from last slide in the interactive interpreter:
>>>import module # now we use module functions >>>module.get_name() ‘Hello’ >>> module.MODULEVAR 10
CS2304: Python for Java Programmers
Monti 2014
The Import Statement
• The import statement has several variations. • We can import specific functions, or everything at
once. • Here’s another example, importing just a single
function:
>>>from module import get_name # I don’t need to qualify get_name with module >>>get_name() ‘Hello’ # However, this no longer works: >>> module.MODULEVAR 10
CS2304: Python for Java Programmers
Monti 2014
The Import Statement
• The import statement has several variations. • Here’s another example, this one will import
everything* in the module:
• * There are some exceptions we’ll talk about later.
>>>from module import * # I don’t need to qualify get_name with module >>>get_name() ‘Hello’ # This works again, without qualification : >>> MODULEVAR 10
CS2304: Python for Java Programmers
Monti 2014
Another Variation Using “as”
• You can also rename packages using import and the “as” statement.
• Might do this when a name is too long:
>>> import module as m # Now I can refer module as m. >>>m.get_name() ‘Hello’ # This also works: >>> m.MODULEVAR 10 # Everything together, here I’ve renamed a function. >>> from module import get_name as name >>> name()
CS2304: Python for Java Programmers
Monti 2014
A Final Word On Import
• Which version will we be using?
• The concern here is “flooding” the name space with duplicates.
• Two or more names can collide. • As an example, say there is a get_name() in moduleA and in moduleB? Which one is chosen?
>>> import module >>> module.get_name()
>>> from moduleA import * >>> from moduleB import *
CS2304: Python for Java Programmers
Monti 2014
Creating / Using Packages
• A package is simply a directory filled with Python modules and an additional __init__.py file.
• The __init__.py file can actually be empty, or it can contain Python code.
• __init__.py often contains a list called __all__, this is the list of variables and functions imported when using a “*”.
• Let’s say you have a package Graphics, with a module called Jpeg.py, to import that module:
>>> import Graphics.Jpeg >>> Graphics.Jpeg.somefunction()
CS2304: Python for Java Programmers
Monti 2014
Creating / Using Packages
• Like with just modules you should avoid the *, but it can be used with packages as well:
• You can also rename packages/modules as well:
• These have the same name collision concerns as
modules.
>>> from Graphics import * >>> Jpeg.somefunction()
>>> import Graphics.Jpeg as Jpeg >>> Jpeg.somefunction()
CS2304: Python for Java Programmers
Monti 2014
Standard Library Modules/Packages
• The previous examples were written as if you were importing your own code.
• The process is identical for any packages or modules that come with Python.
• Take a look at Chapter 5 in the book for a tour of some of the stuff in the standard Python install.
• There are built-in modules for: • Handling command line options (optparse, argparse). • Archiving/compressing files, so tar (tarfile), gzip, bz2, zipfile. • Interacting with the file system, (os, os.path). • Regular expressions (re), we’ll talk more about these later. • There’s a large math library, we saw math.pi earlier.
CS2304: Python for Java Programmers
Monti 2014
Back To This If Statement • Remember this:
• __name__ is defined for each module in your
program. • Normally this just the name we imported:
def main(): print("Hello World”) if __name__ == "__main__": main()
>>> import module module.__name__ ‘module’
CS2304: Python for Java Programmers
Monti 2014
Back To This If Statement
• The one exception occurs if you don’t import a module, but rather “run” the module.
• Instead of importing, running the module is what we are doing here:
• I’m treating this module as the start point of my
program, so __name__ is set to “__main__” • So what this is really saying is: when this module
is “run”, do what ever inside of the if statement:
if __name__ == "__main__": print(“I’m running”); main()
python3 myprogram.py
CS2304: Python for Java Programmers
Monti 2014
Back To This If Statement
• When I import the module, the if statement will never run because __name__ is “myprogram”:
• So you can put an if statement like this at the
bottom of every module, and it will only run when you do something like this:
• For example if you wanted to test code, you could
put something inside of the if statement:
if __name__ == "__main__": testFunction1(); testfunction2()
import myprogram
python3 module1
CS2304: Python for Java Programmers
Monti 2014
Primitive Unit Testing With Doctest
if __name__ == "__main__": import doctest doctest.testmod()
def char_at(row, column): """Returns the character at the given position This is really just for debugging. >>> char_at(0, 0) '%' >>> char_at(4, 11) ' ' >>> char_at(32, 24) Traceback (most recent call last): ... RowRangeError """ # function code