473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Duplicating Modules

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
Sep 30 '05 #1
4 1727
Why don't you do like this..

import module
import mudule as module2

Sep 30 '05 #2
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
Sep 30 '05 #3
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
Sep 30 '05 #4
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.

Oct 1 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abu | last post by:
I'm having a problem with Fast Template duplicating the content. Here's the code, hoping someone may be of help, thanks! while(list($last_name)=mysql_fetch_array($data1)) { $data2 =...
5
by: DB_2 | last post by:
I was wondering if there was a way to create a copy of a table in a single SQL statement, together with its column structure and data. In Oracle, there is a "CREATE TABLE new-table AS...
2
by: Todd Plambeck | last post by:
I have an ASP.net order entry system that will occasionally insert duplcate orders into the database. We are using SQL Server for the session state. This also only seems to happen when the server...
2
by: Samuel R. Neff | last post by:
I'm trying to find a good way to handle Control.InvokeRequired without duplicating four lines of code in every function/event. Typically what I've seen in books is this: If InvokeRequired Then...
8
by: Josetta | last post by:
I have found a wealth of information here on how to duplicate records in forms with subforms. I have adapted code found here to work with my forms. It works beautifully the first time I hit the...
0
by: rmli | last post by:
Duplicating a Database using RMAN http://quickdba.blogspot.com/2006/05/duplicating-database-using-rman_22.html
3
by: Darren.Ratcliffe | last post by:
Hi everyone I'm using v 2.0 of the framework with C# and am developing a web application. I am finding that my cookie names are duplicating themselves over and over again, for example when I...
2
by: Ciaran | last post by:
Hi is there a way of duplicating the same mysql_query resource result or is there a way to stop it from being cleared once it is no longer needed? I need to run a ......
0
by: Jonathan Wood | last post by:
When a GridView performs a row edit, edit controls appear without needing to display a different page, or, apparently, needing to refresh the existing page. Does anyone have tips on duplicating...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.