Re: language war - Mailing list pgsql-general

From Jay O'Connor
Subject Re: language war
Date
Msg-id 3FCCC19B.7080802@cybermesa.com
Whole thread Raw
In response to language war  (Jonathan Bartlett <johnnyb@eskimo.com>)
Responses Re: language war
List pgsql-general
 >>My favorite feature is the fact that I can include
 > >arbitrary files, using a line of code:
 > >include "$next_file.php";

 >Dynamically importing modules based on variables like you show above is
 >certainly possible in Python.

Here's an elaborate example:  I have two modules (A.py and B.py) that
both define the function test().  My main driver prompts the user for
input and based on the input will import the module and call test() in
the imported module, which will print either "module A" or "module B"
depending on what was imported, or an error message if the input didn't
matcha  module.  This illustrates a complete example, but you can
simplify it really to "mymodule = __import__ (next_file)" where
next_file is a variable name

====================
--A.py
def test ():
        print "module A"


--B.py
def test ():
        print "module B"
--dynamicimport.py
print "Test Dynamic Import"

request = raw_input ("enter 'a' or 'b'")
modName = request.upper()
mod = None

try:
    mod = __import__(modName)
except:
    print "unable to import %s" % modName

if mod:
    mod.test()
else:
    print "Sorry, no appropriate module"
====================


pgsql-general by date:

Previous
From: "Jay O'Connor"
Date:
Subject: Re: language war
Next
From: Richard Huxton
Date:
Subject: Re: how many quotes?