1 / 33

Day 6: Exit Interview

Day 6: Exit Interview. http://www.dilbert.com. Google App Engine SDK. From TechRepublic article (Jun ’08). Value-returning functions & Modules. Modules. Library. Built-in (System). Modules. Library. Built-in (System). Define and call a function. Syntax:

brinly
Télécharger la présentation

Day 6: Exit Interview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 6: Exit Interview http://www.dilbert.com

  2. Google App Engine SDK From TechRepublic article (Jun ’08)

  3. Value-returning functions & Modules Modules Library Built-in (System)

  4. Modules Library Built-in (System)

  5. Define and call a function Syntax: def FunctionName([params]): blockOfCode [return someObject] blockOfCode is one or more Python statements. All function statements must be indented and by the same number of spaces, tabs, etc. [params] is an optional comma-separated list of objects What about len?

  6. IPO and Functions Input(10 in this example) Processing Output Python print evaluates each expression to itsright before printing. In this case, evaluation involves a call to addOne with an argument of 10. The output of addOne gets returned to here and print sends it to stdout.

  7. Use clear function and variable names Most function names should be verb phrases not noun phrases.Variable names should convey what their value is. Boolean functions (return bool)should ask a yes/no question.

  8. Incremental development Start with a simple test and function skeleton. Run test repeatedly as you debug function under test.

  9. Return as short-circuit for function If return statement is executedin a function, the interpreterreturns to the next statementafter the function call. No otherlines in the function are execurted.

  10. Modules Library Built-in (System)

  11. Structuring a Python program a.py Top-level module that controls program flow. Imports b. b.py c.py b and c import each other …and may import one or more of the standard library modules Std Library modules

  12. What are Modules? • Modules are most typically .py or pyc files but can be DLL, etc • Many modules and packages (groups of modules) shipped with Python (e.g. sys, os, math, turtle, etc.) • Local modules (in same folder as running .py) override other modules in folders. • Example: arcgisscripting.py in local folder will override C:\Program Files\ArcGIS\bin\arcgisscripting.dll  this is NOT a good thing.

  13. How: Module contents and structure • Most modules have one or more of the attributes shown below - If you run a module as a program, the built-in __name__ attribute contains __main__. - If you import, __name__ contains the name of the module (no .py extension). - Convention is to put this block at the end of the module.

  14. Why use Modules? • Code reuse • Saves code permanently to disk • Can be referenced (imported) by multiple client modules • Define data and methods that can be used in programs • Namespace partitioning • Seals up names to avoid name clashes

  15. Namespaces

  16. Scope rules • Python searches for names in LEGB order and stops at the first name it finds (i.e. Local x takes precedence over EGB • Local: in a function and not declared global (e.g. global x) • Enclosing functions: nested functions • Not covered in this course • Global (module): names at top-level of module • Built-in: names in Python built-in names module (e.g. open, range, etc.)

  17. import steps • Find the module’s file • Search folders in following order: • Folder containing the program • PYTHONPATH directories (if it is set) • Standard library directories • Compile it to byte code (maybe) • If .py newer than .pyc and if file is imported • Run the module’s code to build the objects it defines }

  18. import <module> / from <module> import … • Import is only run once / module / Python session • If you edit the module file with external editor and save, and you are in the same Python session, then the updated file WILL NOT BE IMPORTED! • Using from <module> import <name(s)> means you do not have to qualify the names with the module name B.py run twice … del () removes namesfrom program

  19. reload(module) • The reload() function forces a module to reload – run its code. • Python / PythonWin is not aware of updates in imported modules • Really useful during program development • Should not be used with stable modules

  20. Modules Library Built-in (System)

  21. Built-in operators and functions • Expression operators: +, -, *, /, **, >>, etc. • Built-in functions: str(), abs(), etc. • Type conversions: int(), float(), long(), str(), etc. • Logical operators: ==, !=, <, <=, or, and, not, etc.

  22. Modules Library Built-in (System)

  23. Built-in & Standard Python library modules • Built-in functions can be called without importing any module • Standard Python Library modules must be imported to gain access to functions, etc.

  24. Some useful Python Modules glob math os os.path shutil sys zipfile

  25. sys module & command line args (sys.argv) • Command line args are stored in sys.argv Also has exit method to exit/stop a Python program e.g. sys.exit(0)

  26. os and os.path module • Access to operating system properties / operations • Get/Set environment variables, get current directory, creating/deleting directories, path/file info, split/join paths Three ways to specify pathsin Python:

  27. os.path module (splitting/joining paths)

  28. glob - Filename globbing utility • The glob module finds all the pathnames matching a specified pattern

  29. shutil – file/dir tree copy/move/delete(rm) • The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. Implementation of shutil.copytree

  30. Other modules • zipfile module – working with .zip files (read, write, get info, etc.) • The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP)

  31. __main__ and main() • In many Python scripts you will find the following at the bottom of modules • In plain English … If the module is running as a program (i.e. not imported), then run the main() function defined in the module.

  32. Done, done …

More Related