Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Automatic import PEP

Reply
Thread Tools

Automatic import PEP

 
 
Dan Bishop
Guest
Posts: n/a
 
      10-06-2006
On Sep 22, 10:09 pm, Connelly Barnes <(E-Mail Removed)> wrote:
> Hi,
>
> I wrote the 'autoimp' module [1], which allows you to import lazy modules:
>
> from autoimp import * (Import lazy wrapper objects around all modules; "lazy
> modules" will turn into normal modules when an attribute
> is first accessed with getattr()).
> from autoimp import A, B (Import specific lazy module wrapper objects).
>
> The main point of autoimp is to make usage of the interactive Python prompt
> more productive by including "from autoimp import *" in the PYTHONSTARTUP file.


And it does. Gets rid of "oops, I forgot to import that module"
moments without cluttering my $PYTHONSTARTUP file with imports. +1.

My only complaint is that it breaks globals().

 
Reply With Quote
 
 
 
 
Georg Brandl
Guest
Posts: n/a
 
      10-06-2006
Dan Bishop wrote:
> On Sep 22, 10:09 pm, Connelly Barnes <(E-Mail Removed)> wrote:
>> Hi,
>>
>> I wrote the 'autoimp' module [1], which allows you to import lazy modules:
>>
>> from autoimp import * (Import lazy wrapper objects around all modules; "lazy
>> modules" will turn into normal modules when an attribute
>> is first accessed with getattr()).
>> from autoimp import A, B (Import specific lazy module wrapper objects).
>>
>> The main point of autoimp is to make usage of the interactive Python prompt
>> more productive by including "from autoimp import *" in the PYTHONSTARTUP file.

>
> And it does. Gets rid of "oops, I forgot to import that module"
> moments without cluttering my $PYTHONSTARTUP file with imports. +1.
>
> My only complaint is that it breaks globals().


And startup takes quite long the first time, because a list of all available
modules must be gathered.

To work around that, one can either use a special importing "lib" object,
defined like that:

class _lib:
def __getattr__(self, name):
return __import__(name)
lib = _lib()


or modify the globals() to automatically look up modules on KeyError, like this
(put into PYTHONSTARTUP file):


import sys, code

class LazyImpDict(dict):
def __getitem__(self, name):
try:
return dict.__getitem__(self, name)
except KeyError:
exc = sys.exc_info()
try:
return __import__(name)
except ImportError:
raise exc[0], exc[1], exc[2]

d = LazyImpDict()
code.interact(banner='', local=d)
sys.exit()


Of course, this is not perfect as it may break quite a lot of things,
I haven't tested it thoroughly.

Georg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About Rational Number (PEP 239/PEP 240) Lie Python 25 12-18-2007 06:13 AM
can't reload with PEP 302 import hooks Mustafa Thamer Python 0 08-13-2004 09:30 PM
[PEP] auto keyword for automatic objects support in Python Manlio Perillo Python 2 06-23-2004 01:56 PM
PEP: import version Manlio Perillo Python 16 05-19-2004 03:48 PM
PEP for new modules (I read PEP 2) Christoph Becker-Freyseng Python 3 01-16-2004 04:26 PM



Advertisments