100%(2)2 out of 2 people found this document helpful
This preview shows page 709 - 712 out of 777 pages.
One can also run Python scripts by writing just the basename of the scriptfile, i.e.,somescriptinstead ofsomescript.py, if the file extension is registeredin thePATHEXTenvironment variable:PATHEXT=%PATHEXT%;.py
Appendix BElements of Software EngineeringThis appendix addresses important topics for creating reliable and reusablesoftware. Although the material is aimed at Python programs in particular,many of the topics and tools are equally relevant for software development inother computer languages. Appendix B.1 explains how to build Python mod-ules and packages. Documentation of Python software, especially via embed-ded doc strings, is the topic of Appendix B.2. The Python coding standardand programming habits used in this book are documented in Appendix B.3,ready to be adopted in the reader’s projects as well. Appendix B.3.2 mayserve as a summary of how Python programming differs from traditionalprogramming in Fortran, C, C++, and Java.Appendix B.4 deals with techniques, first of all regression testing, forverifying that software works as intended. Finally, Appendix B.5 gives a quickintroduction to version control of software (and documentation) using theSubversion (svn) system.B.1Building and Using ModulesYou will soon find yourself writing useful scripting utilities that can be reusedin many contexts. You should then collect such reusable pieces of scripts inthe form of functions or classes and put them in a module (see Chapter 2.5.3for a brief illustration). The module can thereafter can be imported in anyscript, giving you access to a library of your own utilities. We shall on thenext pages explain how to make a module, where to store it, and how toimport it in scripts.B.1.1Single-File ModulesMaking modules in Python is trivial. Just put the code you want in a file,sayMyMod.py. To use the module, simply writeimport MyModor something likefrom MyMod import f1, f2, MyClass1or just
690B. Elements of Software Engineeringfrom MyMod import *However, you need to tell Python where to find your module. This can bedone in three ways, either1. specify paths for your own Python modules in thePYTHONPATHenviron-ment variable,2. modify thesys.pathlist, containing all the directories to search for Pythonmodules, directly in the script, or3. store your module in a directory that is already present inPYTHONPATHorsys.path, e.g., one of the oﬃcial directories for Python libraries1.Suppose you place theMyMod.pyfile in a directory$HOME/my/modules. Addingthe module directory to thePYTHONPATHvariable is done as follows in a shell’sstart-up file. Bash users typically writeexport PYTHONPATH=$HOME/my/modules:$PYTHONPATHin.bashrc.Modifying thesys.pathvariable in the script is done by adding yourmodule library as (preferably) the first item in the list of directories:module_dir = os.path.join(os.environ[’HOME’],’my’,’modules’)sys.path.insert(0, module_dir)# orsys.path[:0] = [module_dir]# orsys.path = [module_dir] + sys.path