One can also run Python scripts by writing just the basename of the script file

One can also run python scripts by writing just the

This preview shows page 709 - 712 out of 777 pages.

One can also run Python scripts by writing just the basename of the script file, i.e., somescript instead of somescript.py , if the file extension is registered in the PATHEXT environment variable: PATHEXT=%PATHEXT%;.py
Image of page 709
Appendix B Elements of Software Engineering This appendix addresses important topics for creating reliable and reusable software. Although the material is aimed at Python programs in particular, many of the topics and tools are equally relevant for software development in other 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 standard and 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 may serve as a summary of how Python programming differs from traditional programming in Fortran, C, C++, and Java. Appendix B.4 deals with techniques, first of all regression testing, for verifying that software works as intended. Finally, Appendix B.5 gives a quick introduction to version control of software (and documentation) using the Subversion ( svn ) system. B.1 Building and Using Modules You will soon find yourself writing useful scripting utilities that can be reused in many contexts. You should then collect such reusable pieces of scripts in the form of functions or classes and put them in a module (see Chapter 2.5.3 for a brief illustration). The module can thereafter can be imported in any script, giving you access to a library of your own utilities. We shall on the next pages explain how to make a module, where to store it, and how to import it in scripts. B.1.1 Single-File Modules Making modules in Python is trivial. Just put the code you want in a file, say MyMod.py . To use the module, simply write import MyMod or something like from MyMod import f1, f2, MyClass1 or just
Image of page 710
690 B. Elements of Software Engineering from MyMod import * However, you need to tell Python where to find your module. This can be done in three ways, either 1. specify paths for your own Python modules in the PYTHONPATH environ- ment variable, 2. modify the sys.path list, containing all the directories to search for Python modules, directly in the script, or 3. store your module in a directory that is already present in PYTHONPATH or sys.path , e.g., one of the official directories for Python libraries 1 . Suppose you place the MyMod.py file in a directory $HOME/my/modules . Adding the module directory to the PYTHONPATH variable is done as follows in a shell’s start-up file. Bash users typically write export PYTHONPATH=$HOME/my/modules:$PYTHONPATH in .bashrc . Modifying the sys.path variable in the script is done by adding your module 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) # or sys.path[:0] = [module_dir] # or sys.path = [module_dir] + sys.path
Image of page 711
Image of page 712

You've reached the end of your free preview.

Want to read all 777 pages?

  • Fall '08
  • Keyes
  • The Land, scripting language

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture