Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Duplicating Modules

Reply
Thread Tools

Duplicating Modules

 
 
Misto .
Guest
Posts: n/a
 
      09-30-2005
Hi folks!

Short:

There is a way to dumplicate a module ?

I tried
copy.deepcopy(module) but hangs with an error (also with standard modules )..

The only solution that I have by now is creating two files and importing them.
I.E:
> cp module.py module1.py


>> import module
>> import module1



Any Ideas?

P.S: I know that there is some design Issue here, but my boss says no

Misto
 
Reply With Quote
 
 
 
 
kimes
Guest
Posts: n/a
 
      09-30-2005
Why don't you do like this..

import module
import mudule as module2

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-30-2005
kimes wrote:

> Why don't you do like this..
>
> import module
> import mudule as module2


>>> import module as a
>>> import module as b
>>> b is a

True

You have to remove the module from the cache before the second import:

>>> import sys
>>> import module as a
>>> del sys.modules["module"]
>>> import module as b
>>> b is a

False

Peter


 
Reply With Quote
 
Dave Benjamin
Guest
Posts: n/a
 
      09-30-2005
Misto . wrote:
> Hi folks!
>
> Short:
>
> There is a way to dumplicate a module ?


Here's one way... it doesn't quite work with modules inside of packages,
unfortunately, but it does avoid defeating module caching and tries to
keep sys.modules in a predictable state. I don't know what the
thread-safety implications are for this sort of trickery with sys.modules.

def import_as(newname, oldname):
"""Import a module under a different name.

This procedure always returns a brand new module, even if
the original module has always been imported.

Example::

try:
# Reuse this module if it's already been imported
# as "mymath".
import mymath
except ImportError:
# "mymath" has not yet been imported.
# Import and customize it.
mymath = import_as('mymath', 'math')
mymath.phi = (mymath.sqrt(5.0) - 1.0) / 2.0

The above code will not reinitialize "mymath" if it executes
a second time (ie. if the module containing this code is
reloaded). Whether or not "math" has already been imported,
it will always be a different object than "mymath".
"""

import sys
if sys.modules.has_key(oldname):
tmp = sys.modules[oldname]
del sys.modules[oldname]
result = __import__(oldname)
sys.modules[newname] = sys.modules[oldname]
sys.modules[oldname] = tmp
else:
result = __import__(oldname)
sys.modules[newname] = sys.modules[oldname]
del sys.modules[oldname]
return result

Dave
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-01-2005
On Fri, 30 Sep 2005 19:52:56 +0200, Misto . wrote:

> There is a way to dumplicate a module ?


[snip]

> P.S: I know that there is some design Issue here, but my boss says no


It depends on what you are expecting to do with the duplicated module. If
all you need is to access the same module from two different names, you
can do this:

py> import sys
py> my_boss_is_an_idiot = sys # *grins*
py> my_boss_is_an_idiot.version
'2.3.3 (#1, May 7 2004, 10:31:40) \n[GCC 3.3.3 20040412 (Red Hat Linux
3.3.3-7)]'


But keep in mind that using this method, sys and my_boss_is_an_idiot are
merely different names for the same underlying module. Change one and you
change the other.

I'm curious... I don't expect you to comment on your boss' mental state,
but how/why do you need to duplicate the module?


--
Steven.

 
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
ASP.Net Duplicating DB Entries Todd Plambeck ASP .Net 2 08-05-2005 07:55 PM
Duplicating datasheet screens Don ASP .Net 0 02-23-2005 03:09 AM
log4j duplicating appender entries George Jempty Java 2 01-15-2005 06:09 AM
Forms authentication duplicating ReturnURL AC ASP .Net 0 07-27-2004 02:10 PM



Advertisments