DOC PREVIEW
DREXEL CS 265 - Python's Modules

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Python’s ModulesWhat is a module?Module’s Cont’d… # Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print b, a, b = b, a+b def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return resultModule’s cont’d… >>> import fibo >>> fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> fibo.__name__ 'fibo' >>> fib = fibo.fib >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377Module’s Cont’d…The Module Search Path“Compiled” Python files“Compiled” Python files cont’dThe dir() FunctionThe dir() Function cont’d…PackagesSlide 12Packages cont’d…Importing * From a PackageQ U E S T I O N SPython’s Modulesby E. Esin GOKGOZWhat is a module? a file containing Python definitions and statementsdefinitions from a module can be imported into other modules or into the main moduleThe file name is the module name with the suffix .py appendedModule’s Cont’d…# Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print b, a, b = b, a+b def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return resultModule’s cont’d…>>> import fibo >>> fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> fibo.__name__ 'fibo' >>> fib = fibo.fib >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377Module’s Cont’d…Modules can import other modules>>> from fibo import fib, fib2 >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377>>> from fibo import * >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377The Module Search PathWhen a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATHWhen PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually .:/usr/local/lib/python“Compiled” Python filesif a file called spam.pyc exists in the directory where spam.py is found, this is assumed to contain an already-“byte-compiled” version of the module spam.“Compiled” Python files cont’dWhen the Python interpreter is invoked with the -O flag, optimized code is generated and stored in .pyo files. A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loadedThe module compileall can create .pyc files for all modules in a directory.The dir() FunctionWithout arguments, dir() lists the names you have defined currently:>>> a = [1, 2, 3, 4, 5] >>> import fibo >>> fib = fibo.fib>>> dir() ['__builtins__', '__doc__', '__file__', '__name__', 'a', 'fib', 'fibo', 'sys']The dir() Function cont’d…standard module __builtin__:>>> import __builtin__ >>> dir(__builtin__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'IOError', 'ImportError', 'IndentationError', 'IndexError',…]PackagesPackages are a way of structuring Python’s module namespace by using “dotted module names”. e.g. the module name A.B designates a submodule named B in a package named ASound / Top-level package__init__.py Initialize the sound package formats/ Subpackage for file format conversions __init__.py wavread.pywavwrite.py aiffread.py aiffwrite.py auread.py ... effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ...filters/ Subpackage for filters __init__.py equalizer.py vocoder.py ...Packages cont’d…import sound.effects.echo sound.effects.echo.echofilter(input, output, delay=0.7, atten=4) from sound.effects import echo echo.echofilter(input, output, delay=0.7, atten=4) from sound.effects.echo import echofilterImporting * From a Packagefrom sound.effects import *!!! must have __all____all__ = ["echo", "surround", "reverse"]Q U E S T I O N S? ? ? ?? ? ???


View Full Document

DREXEL CS 265 - Python's Modules

Download Python's Modules
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Python's Modules and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Python's Modules 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?