Package vs. module

Stuart D. Gathman stuart at bmsi.com
Sat Dec 16 10:28:06 EST 2006


On Mon, 11 Dec 2006 20:59:27 -0300, Gabriel Genellina wrote:

> The above code *almost* works, but DNSLookup is a local name inside 
> the function. Use the global statement.
> As an example, see how getpass.py (in the standard library) manages 
> the various getpass implementations.

Ok, I have a working package:

spf/
  __init__.py
  pyspf.py
  pydns.py
  dnspython.py

__init__.py:
from pyspf import *
from pyspf import __author__,__email__,__version__ 

def set_dns_driver(f):
  global DNSLookup
  DNSLookup = f
  pyspf.DNSLookup = f

def DNSLookup(name,qtype,strict=True):
  import pydns
  return DNSLookup(name,qtype,strict)

set_dns_driver(DNSLookup)

Importing a driver module activates that driver.
For instance, in pydns.py:

import DNS    # http://pydns.sourceforge.net
import spf

...
def DNSLookup(...):
  ...

spf.set_dns_driver(DNSLookup)

NOW, this is all very nice and modular. BUT, the original module was a
single file, which could be run as a script as well as imported as a
module. The script features provided useful command line functionality.
(Using if __name__ == '__main__':).  Now that 'spf' is a package, the
command line feature is gone!  Even using -m, I get:

python2.4 -m spf
python2.4: module spf has no associated file

Looking at getpass.py as advised, I see they put all the drivers in the
module.  I could do that with spf.py, I suppose.  But I like how with the
package, the driver code is not loaded unless needed.

One other idea I had was an arrangement like this:

SPF/
  pydns.py
  dnspython.py

spf.py

This would keep the module as a single file usable from the command line,
but still make driver available as separately loaded modules.

So which of the three options,

1) single file module with all drivers, ala getpass
2) package that cannot be run directly from command line
3) single file module with associated driver package

is the most pythonic?


-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.




More information about the Python-list mailing list