need design suggestions: local namespace problem
Ignacio Vazquez-Abrams
ignacio at openservices.net
Tue Oct 2 18:30:29 EDT 2001
On Tue, 2 Oct 2001, Jason R. Mastaler wrote:
> I'm writing a mail application that needs to have different values for
> variables depending on whether the app is running under qmail or
> Sendmail.
>
> Currently I have those variables in a module called "Defaults.py".
> So, within the program main, I do things like:
>
> import Defaults
> sys.exit(Defaults.ERR_HARD)
>
> Some of these variables need to conditionally have different values as
> I mentioned. For example, exit status codes. Under qmail,
> `ERR_HARD = 100', while under Sendmail, `ERR_HARD = 75'.
>
> I was thinking of splitting Defaults.py up into multiple files
> (Qmail.py and Sendmail.py), and then using local namespace trickery so
> that within the program main I can do things like:
>
> if qmail:
> import Qmail as mta
> elif sendmail:
> import Sendmail as mta
>
> sys.exit(mta.ERR_HARD)
>
> However, the "import module as name" syntax is a Python2-ism, and I'd
> still like to support Python-1.5.2 if I can.
How about this:
sendmail.py
---
class mta:
ERR_HARD=75
...
---
qmail.py
---
class mta:
ERR_HARD=100
...
---
main.py
---
...
if issendmail:
from sendmail import mta
elif isqmail:
from sendmail import mta
...
sys.exit(mta.ERR_HARD)
...
---
--
Ignacio Vazquez-Abrams <ignacio at openservices.net>
More information about the Python-list
mailing list