python tutorial part 2

50
Python Tutorial Haitham El-Ghareeb, Ph.D. May, 2012 Twitter: @helghareeb

Upload: haitham-el-ghareeb

Post on 19-May-2015

829 views

Category:

Technology


2 download

TRANSCRIPT

  • 1. Python TutorialHaitham El-Ghareeb, Ph.D.May, 2012Twitter: @helghareeb

2. Modules A module is a file containing Pythondefinitions and statements. The file name is the module name with thesuffix .py appended. Within a module, the modules name (as astring) is available as the value of the globalvariable __name__. 3. Assign Local Name 4. Compiled Python Files As an important speed-up of the start-up time forshort programs that use a lot of standardmodules, if a file called spam.pyc exists in the directorywhere spam.py is found, this is assumed tocontain an already-byte-compiled version ofthe module spam. The modification time of the version of spam.pyused to create spam.pyc is recorded in spam.pyc,and the .pyc file is ignored if these dont match. 5. Compiled Python Files Normally, you dont need to do anything to create thespam.pyc file. Whenever spam.py is successfully compiled, anattempt is made to write the compiled version tospam.pyc. It is not an error if this attempt fails; if for any reasonthe file is not written completely, the resultingspam.pyc file will be recognized as invalid and thusignored later. The contents of the spam.pyc file are platformindependent, so a Python module directory can beshared by machines of different architectures. 6. Some Tips for Experts When the Python interpreter is invoked withthe -O flag, optimized code is generated andstored in .pyo files. The optimizer currently doesnt help much; itonly removes assert statements. When -O is used, all bytecode is optimized;.pyc files are ignored and .py files arecompiled to optimized bytecode. 7. Some Tips for Experts Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to performoptimizations that could in some rare cases resultin malfunctioning programs. Currently only __doc__ strings are removed fromthe bytecode, resulting in more compact .pyofiles. Since some programs may rely on having theseavailable, you should only use this option if youknow what youre doing. 8. Some Tips for Experts A program doesnt run any faster when it isread from a .pyc or .pyo file than when it isread from a .py file; the only thing thats fasterabout .pyc or .pyo files is the speed withwhich they are loaded. 9. Some Tips for Experts When a script is run by giving its name on thecommand line, the bytecode for the script isnever written to a .pyc or .pyo file. Thus, the startup time of a script may bereduced by moving most of its code to amodule and having a small bootstrap scriptthat imports that module. It is also possible toname a .pyc or .pyo file directly on thecommand line. 10. Some Tips for Experts It is possible to have a file called spam.pyc (orspam.pyo when -O is used) without a filespam.py for the same module. This can be used to distribute a library ofPython code in a form that is moderately hardto reverse engineer. 11. Some Tips for Experts The module compileall can create .pyc files (or.pyo files when -O is used) for all modules in adirectory. 12. Standard Modules 13. Dir() The built-in function dir() is used to find outwhich names a module defines. It returns asorted list of strings. 14. Packages Packages are a way of structuring Pythons modulenamespace by using dotted module names. For example, the module name A.B designates asubmodule named B in a package named A. Just like the use of modules saves the authors ofdifferent modules from having to worry about eachothers global variable names, the use of dottedmodule names saves the authors of multi-modulepackages like NumPy or the Python Imaging Libraryfrom having to worry about each others modulenames. 15. Input and Output There are several ways to present the outputof a program; data can be printed in a human-readable form, or written to a file for futureuse. This chapter will discuss some of thepossibilities. How do you convert values to strings? 16. Representation The str() function is meant to returnrepresentations of values which are fairlyhuman-readable. repr() is meant to generate representationswhich can be read by the interpreter (or willforce a SyntaxError if there is not equivalentsyntax). 17. Str() and Repr() 18. Formatting Output 19. Formatting Output 20. I dont think so!MORE ON STRING FORMATTING? 21. Reading and Writing Files Open() returns a file object 22. Reading and Writing Files 23. Alternative Method 24. Write to File 25. Close 26. Pickle Module 27. Errors and Exceptions Syntax Errors 28. Exceptions 29. Handling Exceptions 30. Else?! 31. Another Exception 32. Raise Exception 33. Catching it! 34. User Defined Exception 35. Clean Up Actions 36. Classes 37. Class Definition Syntax 38. Class Objects 39. Inheritance 40. Multiple Inheritance 41. Iterators 42. Style of Access 43. Make your Class Iterable 44. Len() Implement __len__ 45. Unit Testing Hopefully!