Re: [Tutor] using dynamic import statements

2012-07-09 Thread Hugo Arts
On Mon, Jul 9, 2012 at 3:10 AM, Chris Hare ch...@labr.net wrote:


 Here is what I want to do:

 I have a bunch of modules to import. instead of duplicating a lot of code
 for each import, I want to do something like this:

 importList = [ sys, os, imp, stat, re, webbrowser, Image,
  StringIO, shutil, datetime ]

 for object in importList:
 try:
 if debug == ON:
 print Importing module %s % (object)
 exec( import   + object)
 except ImportError as error:
 print %s %s requires the Python %s library.   % (
 appName,

 str(appVersion), object )
 print An error occurred when attempting to load the
 library.  The error was '%s'. % ( error )
 exit()

 Everything appears to run okay, however, when the first piece of code
 that relies upon one of these imported modules is executed, I get an error:

 Traceback (most recent call last):
   File a.py, line 122, in module
 imp.load_module(object,fp,pathName,description)
   File ./Modules/functions.py, line 133, in module
 def special_match(strg, search=re.compile(r'[^a-zA-Z0-9\.\
 \-\#\$\*\@\!\%\^\]').search):
 NameError: name 're' is not defined

 Is is possible to do what I am trying to do, or am I doing something wrong?


 Yes, it is possible, but you are doing something wrong. Don't use exec()
for this, it's unnecessary. You can make this work with the exec function,
but the function you really want is called __import__(). It's documented
here: http://docs.python.org/library/functions.html#__import__

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread Alan Gauld

On 09/07/12 02:10, Chris Hare wrote:


Here is what I want to do:

I have a bunch of modules to import. instead of duplicating

 a lot of code for each import, I want to do something like this:

...lots of code stripped


Why not the more usual:

import sys, os, imp, stat,\
   re, webbrowser, Image, \
   StringIO, shutil, datetime

And let the Python ImportError alert you to what failed?

Or if you must print your own errors just use

try:
   import ...
except ImportError:
   # your code here

I'm not sure what code you think you need to duplicate or why.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread ankur ~ अंकुर
On Mon, Jul 9, 2012 at 6:40 AM, Chris Hare ch...@labr.net wrote:


 Here is what I want to do:

 I have a bunch of modules to import. instead of duplicating a lot of code
 for each import, I want to do something like this:

 importList = [ sys, os, imp, stat, re, webbrowser, Image,
  StringIO, shutil, datetime ]

 for object in importList:
 try:
 if debug == ON:
 print Importing module %s % (object)
 exec( import   + object)
 except ImportError as error:
 print %s %s requires the Python %s library.   % (
 appName,

 str(appVersion), object )
 print An error occurred when attempting to load the
 library.  The error was '%s'. % ( error )
 exit()

 Everything appears to run okay, however, when the first piece of code
 that relies upon one of these imported modules is executed, I get an error:

 Traceback (most recent call last):
   File a.py, line 122, in module
 imp.load_module(object,fp,pathName,description)
   File ./Modules/functions.py, line 133, in module
 def special_match(strg, search=re.compile(r'[^a-zA-Z0-9\.\
 \-\#\$\*\@\!\%\^\]').search):
 NameError: name 're' is not defined

 Is is possible to do what I am trying to do, or am I doing something wrong?

 Thanks

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


Hi,

How about using import imp
http://docs.python.org/library/imp.html

calling run time modules as per your need.

thank you
ankur.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread Kwpolska
Why does this bloody ML want me to respond to the last person instead
of tutor@python.org?
---
On Mon, Jul 9, 2012 at 9:47 AM, Alan Gauld alan.ga...@btinternet.com wrote:
 On 09/07/12 02:10, Chris Hare wrote:


 Here is what I want to do:

 I have a bunch of modules to import. instead of duplicating

 a lot of code for each import, I want to do something like this:

 ...lots of code stripped


 Why not the more usual:

 import sys, os, imp, stat,\
re, webbrowser, Image, \
StringIO, shutil, datetime

 [snip]

Why not the more standard:
import sys
import os
and so on?  http://www.python.org/dev/peps/pep-0008/#imports
-- 
Kwpolska http://kwpolska.tk
stop html mail  | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16   | Arch Linux x86_64, zsh, mutt, vim.
# vim:set textwidth=70:
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread Alan Gauld

On 09/07/12 10:19, Kwpolska wrote:

Why does this bloody ML want me to respond to the last person instead
of tutor@python.org?


Because that's how it, along with many other mailing lists, works.
If it helps, think of it as you receiving a mail from the sender and CCd 
to the list. Therefore hitting reply sends to the person who sent the 
mail and ReplyAll goes to everyone. Seems logical to me! :-)
It allows me to choose to reply to the OP only or to the group, I use 
both depending on the nature of my reply. (About 80% of my replies go to 
the everyone.) But some prefer it differently... :-)



Why not the more usual:

import sys, os, imp, stat,\
re, webbrowser, Image, \
StringIO, shutil, datetime


Why not the more standard:
import sys
import os
and so on?  http://www.python.org/dev/peps/pep-0008/#imports


Indeed but the OP seemed to want to remove duplicate coding so I assumed 
that he included using multiple imports.


Personally I'd probably code the above as:

import sys, os, shutil, stat, datetime
import re, StringIO
import webbrowser,
import Image,
import imp

Which groups things into roughly related categories - system,
strings, other...

OTOH I ignore large chunks of Pep 8 because I find its style harder to 
read than the one I'm used to. But then, I'm not contributing to the 
standard library etc... Style is largely a matter of taste.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread Chris Hare

Thanks all for the ideas.  I wanted to have my own error messages printed for 
the user - something a little more meaningful than the standard error. 

Thanks for the advice - very helpful!

On Jul 9, 2012, at 6:12 AM, Alan Gauld wrote:

 On 09/07/12 10:19, Kwpolska wrote:
 Why does this bloody ML want me to respond to the last person instead
 of tutor@python.org?
 
 Because that's how it, along with many other mailing lists, works.
 If it helps, think of it as you receiving a mail from the sender and CCd to 
 the list. Therefore hitting reply sends to the person who sent the mail and 
 ReplyAll goes to everyone. Seems logical to me! :-)
 It allows me to choose to reply to the OP only or to the group, I use both 
 depending on the nature of my reply. (About 80% of my replies go to the 
 everyone.) But some prefer it differently... :-)
 
 Why not the more usual:
 
 import sys, os, imp, stat,\
re, webbrowser, Image, \
StringIO, shutil, datetime
 
 Why not the more standard:
 import sys
 import os
 and so on?  http://www.python.org/dev/peps/pep-0008/#imports
 
 Indeed but the OP seemed to want to remove duplicate coding so I assumed that 
 he included using multiple imports.
 
 Personally I'd probably code the above as:
 
 import sys, os, shutil, stat, datetime
 import re, StringIO
 import webbrowser,
 import Image,
 import imp
 
 Which groups things into roughly related categories - system,
 strings, other...
 
 OTOH I ignore large chunks of Pep 8 because I find its style harder to read 
 than the one I'm used to. But then, I'm not contributing to the standard 
 library etc... Style is largely a matter of taste.
 
 -- 
 Alan G
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread wolfrage8...@gmail.com
Might I recommend you use the logging module that is part of core
Python rather than rolling your own if Debug. I found the logging
module made growing my logging across multiple applications so easy!
http://docs.python.org/library/logging.html

On Mon, Jul 9, 2012 at 2:10 PM, Chris Hare ch...@labr.net wrote:

 Thanks all for the ideas.  I wanted to have my own error messages printed for 
 the user - something a little more meaningful than the standard error.

 Thanks for the advice - very helpful!

 On Jul 9, 2012, at 6:12 AM, Alan Gauld wrote:

 On 09/07/12 10:19, Kwpolska wrote:
 Why does this bloody ML want me to respond to the last person instead
 of tutor@python.org?

 Because that's how it, along with many other mailing lists, works.
 If it helps, think of it as you receiving a mail from the sender and CCd to 
 the list. Therefore hitting reply sends to the person who sent the mail and 
 ReplyAll goes to everyone. Seems logical to me! :-)
 It allows me to choose to reply to the OP only or to the group, I use both 
 depending on the nature of my reply. (About 80% of my replies go to the 
 everyone.) But some prefer it differently... :-)

 Why not the more usual:

 import sys, os, imp, stat,\
re, webbrowser, Image, \
StringIO, shutil, datetime

 Why not the more standard:
 import sys
 import os
 and so on?  http://www.python.org/dev/peps/pep-0008/#imports

 Indeed but the OP seemed to want to remove duplicate coding so I assumed 
 that he included using multiple imports.

 Personally I'd probably code the above as:

 import sys, os, shutil, stat, datetime
 import re, StringIO
 import webbrowser,
 import Image,
 import imp

 Which groups things into roughly related categories - system,
 strings, other...

 OTOH I ignore large chunks of Pep 8 because I find its style harder to read 
 than the one I'm used to. But then, I'm not contributing to the standard 
 library etc... Style is largely a matter of taste.

 --
 Alan G
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor