Playing with modules

J

jjcassidy

I know that this is not particularly "pythonic", but I would like to
mess with modules a bit.

I would like to develop a modular module which allows different
services to plug themselves into the module and become the
implementation for a named module.

Say I have a suite of "cassidy.blah.blah.blah...." modules. As it's not
really my intention to brand everything, I want to remap all these
modules as "home....". Thus if someone (eventually) likes my code for
x, but does not want the whole cassidy avalanche, and just wants
something simple to do home.x, they can provide their own
implementation or create a wrapper class to existing functionality
which does the work.

My problem is that this code fails all the time that I have tried to do
this.

import home.base

Does anybody know of a way to "fool" python into thinking that my suite
is actually this neutrally named suite called "home"?

Thanks,

John
 
J

Jonathan Gardner

You're going to have to create the home.base module somewhere. If you
want to put in that some code that basically imports another module's
namespace into the home.base module's namespace, then that may do what
you want.
 
J

jjcassidy

You're going to have to create the home.base module somewhere. If you
want to put in that some code that basically imports another module's
namespace into the home.base module's namespace, then that may do what
you want.

Thanks, Jonathon, but I think I've tried what you are saying. I've
tried doing this a couple different ways.

I suppose that what I could do is go somewhat explicit, and instead of
trying to exploit the natural import language, create a method for this
such as home.use('base') which could use even use the tracestack, get
the current frame's globals and stick the entry { 'base': <module
'cassidy.base' from 'C:\Dev\Python24\lib\cassidy\base\__init__.pyc'> }
into the currect scope and just make that part of the "API".

But I was wondering whether I could fool or "backend" the normal import
mechanism. Even in Java, I can write my own class loader which can
quick compile Java snippets and load the anonymous byte code.

Thanks,

John
 
K

Kent Johnson

Thanks, Jonathon, but I think I've tried what you are saying. I've
tried doing this a couple different ways.

What did you try? You should be able to write home/__init__.py to import
whatever parts of cassidy.blah... you like and define the name base to
be a reference to some part of cassidy.
But I was wondering whether I could fool or "backend" the normal import
mechanism. Even in Java, I can write my own class loader which can
quick compile Java snippets and load the anonymous byte code.

See http://www.python.org/doc/2.3/whatsnew/section-pep302.html

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top