Python Writing Library Files

PHP also provides a require st at em ent t hat is like include except t hat PHP reads t he file even if t he require occurs inside a cont rol st ruct ure t hat never execut es such as an if block for which t he condit ion is never t rue . PHP 4 adds include_once and require_once st at em ent s. These ar e like include and require except t hat if t he file has already been read, it s cont ent s are not processed again. This is useful for avoiding m ult iple-declarat ion problem s t hat can easily occur in sit uat ions where library files include ot her library files. A way t o sim ulat e single- inclusion behavior under PHP 3 is t o associat e a unique sym bol wit h a library and process it s cont ent s only if t he sym bol is not already defined. For exam ple, a library file, MyLibrary.php, m ight be st ruct ured like t his: ?php MyLibrary.php - illustrate how to simulate single-inclusion behavior in PHP 3 Check whether or not the symbol associated with the file is defined. If not, define the symbol and process the files contents. Otherwise, the file has already been read; skip the remainder of its contents. if defined _MYLIBRARY_PHP_ { define _MYLIBRARY_PHP_, 1; ... put rest of library here ... } end _MYLIBRARY_PHP_ ? Where Should PHP Include Files Be Installed? PHP script s oft en are placed in t he docum ent t ree of your web server, and client s can request t hem direct ly. For PHP library files, I recom m end t hat you place t hem som ewhere out side t he docum ent t ree, especially if like Cookbook.php t hey cont ain nam es and passwords. This is part icularly t rue if you use a different ext ension such as .inc for t he nam es of include files. I f you do t hat and inst all include files in t he docum ent t ree, t hey m ight be request ed direct ly by client s and will be displayed as plain t ext , exposing t heir cont ent s. To prevent t hat from happening, reconfigure Apache so t hat it t reat s files wit h t he .inc ext ension as PHP code t o be processed by t he PHP int erpret er rat her t han being displayed lit erally.

2.4.6 Python

Pyt hon libraries are writ t en as m odules and referenced from script s using import or from st at em ent s. To put t he code for connect ing t o MySQL int o a funct ion, we can writ e a m odule file Cookbook.py: Cookbook.py - library file with utility routine for connecting to MySQL import sys import MySQLdb Establish a connection to the cookbook database, returning a connection object. Dies with a message if the connection cannot be established. def connect : host_name = localhost db_name = cookbook user_name = cbuser password = cbpass try: conn = MySQLdb.connect db = db_name, host = host_name, user = user_name, passwd = password return conn except MySQLdb.Error, e: print Cannot connect to server print Error code:, e.args[0] print Error message:, e.args[1] sys.exit 1 The filenam e basenam e det erm ines t he m odule nam e, so t he m odule is called Cookbook . Module m et hods are accessed t hrough t he m odule nam e, t hus you would invoke t he connect m et hod of t he Cookbook m odule like t his: conn = Cookbook.connect ; The Pyt hon int erpret er searches for m odules in direct ories nam ed in t he sys.path variable. Just as wit h Perls INC ar ray, sys.path is init ialized t o a default set of direct ories. You can find out what t hose direct ories are on your syst em by running Pyt hon int eract ively and ent ering a couple of com m ands: python import sys sys.path I f you put Cookbook.py in one of t he default direct ories, you can reference it from a script using an import st at em ent and Pyt hon will find it aut om at ically: import Cookbook I f you inst all Cookbook.py som ewhere else, you can add t he direct ory where it s inst alled t o t he value of sys.path . Do t his by im port ing t he sys m odule and invoking sys.path.insert . The follow ing t est harness script , harness.py, shows how t o do t his, assum ing t he Cookbook m odule is inst alled in t he usr local apache lib pyt hon direct ory: usrbinpython harness.py - test harness for Cookbook.py library Import sys module and add directory to search path import sys sys.path.insert 0, usrlocalapachelibpython import MySQLdb import Cookbook conn = Cookbook.connect print Connected conn.close print Disconnected sys.exit 0 Anot her way t o t ell Pyt hon where t o find m odule files is t o set t he PYTHONPATH environm ent variable. I f you set t hat variable t o include your m odule direct ory, script s t hat you run need not m odify sys.path . I t s also possible t o im port individual sym bols from a m odule using a from st at em ent : from Cookbook import connect This m akes t he connect rout ine available t o t he script wit hout t he need for t he m odule nam e, so youd use it like t his: conn = connect

2.4.7 Java