473,322 Members | 1,496 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,322 software developers and data experts.

Hacking the scope to pieces

We're starting to version a number of our python modules here, and I've
written a small function that assists with loading the versioned
modules...

A module would be called something like: myModule_1_0.py

In anything that uses it, though, we want to be able to refer to it
simply as 'myModule', with an environment variable ("MYMODULE_VERSION"
- set to "1.0") that defines the version.

I've written a module called 'moduleLoader' with the follwing function
in:

def loadModule(module, version, v = globals()):
import compiler
loadStr = "import %s_%s as %s" % (module, version.replace(".", "_"),
module)
eval(compiler.compile(loadStr, "/tmp/%s_%s_errors.txt" % (module,
version.replace(".", "_")), "single"))
v[module] = vars()[module]
The ideal situation with this would be to be able, in whatever script,
to have:

import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
However, this doesn't work. The two options that do work are:

import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"),
globals())
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
from moduleLoader import myModule
What I'm after is a way of moduleLoader.loadModule working back up the
scope and placing the imported module in the main global scope. Any
idea how to do this?
--
Hugh Macdonald

Jul 19 '05 #1
4 1467

Hugh> What I'm after is a way of moduleLoader.loadModule working back up
Hugh> the scope and placing the imported module in the main global
Hugh> scope. Any idea how to do this?

You want to write an import hook I think. I'd start with the docs for the
__import__ builtin. Also, Google for "python import hook".

Skip
Jul 19 '05 #2
I will take a look!

Thanks Skip

--
Hugh

Jul 19 '05 #3
Maybe I misunderstood what you meant, but I couldn't quite manage to
get this one working....

My initial hopes about __import__() were that I could define it inside
my new module (moduleLoader) and, when the module is imported, it could
do stuff (like try to hold onto the vars() and globals() from the
importing scope). However, I couldn't get it to import...

The route I've ended up going (which is just about as simple) is just
to return the new module from moduleLoader.loadModule, so my loading
code is:

import moduleLoader
myModule = moduleLoader.loadModule("myModule",
os.getenv("MODULE_VERSION"))

I've also switched over to using 'inp' for this, rather than creating a
compiler string - much nicer....

Anyway, thanks Skip

--
Hugh

Jul 19 '05 #4
gry

Hugh Macdonald wrote:
We're starting to version a number of our python modules here, and I've written a small function that assists with loading the versioned
modules...

A module would be called something like: myModule_1_0.py

In anything that uses it, though, we want to be able to refer to it
simply as 'myModule', with an environment variable ("MYMODULE_VERSION" - set to "1.0") that defines the version.
Another technique that you might want to consider, is to have an
explicit
"require" call in the code, instead of an external environment
variable.
The python gtk interface, "pygtk", is used like so:

import pygtk
pygtk.require('1.5')
import gtk

-- or

import pygtk
pygtk.require('2.0')
import gtk

I imagine you could eliminate the extra "import gtk" step, by clever
coding of the import hook. You can find pygtk at:

http://ftp.gnome.org/pub/GNOME/sourc...k-2.4.1.tar.gz

I've written a module called 'moduleLoader' with the follwing function in:

def loadModule(module, version, v = globals()):
import compiler
loadStr = "import %s_%s as %s" % (module, version.replace(".", "_"), module)
eval(compiler.compile(loadStr, "/tmp/%s_%s_errors.txt" % (module,
version.replace(".", "_")), "single"))
v[module] = vars()[module]
The ideal situation with this would be to be able, in whatever script, to have:

import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
However, this doesn't work. The two options that do work are:

import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"),
globals())
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
from moduleLoader import myModule
What I'm after is a way of moduleLoader.loadModule working back up the scope and placing the imported module in the main global scope. Any
idea how to do this?
--
Hugh Macdonald


Jul 19 '05 #5

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

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
0
by: Michele Simionato | last post by:
Here is a hack to implement attribute access (say for a Web framework) the way I always wanted it to be. I say "hack" since it uses sys._getframe and it sets the locals of a class, which is not...
0
by: ChangAya | last post by:
I use binary log on mysql system. Yesterday i found some hacking attempt on my machine. ( I found some unknown queries on binary log) But i don't get any information about hacking query...
3
by: Primera | last post by:
I am trying to use the below to return different values of an int variable, but I seem to be running into scope issues as I cannot use the iSize variable outside of the foreach block. Any help is...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
0
by: masterjuan | last post by:
Networks Hacking (hack C:/ drives, severs...)and security holes all on my website & hacking commands and I explain ways of erasing your tracks so you dont get caught doing "bad" things... What do...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
11
by: grif | last post by:
Hi everyone! Been a few weeks since i've asked a noob question :) At the moment I'm writing my First Form application compared to the few console bits and pieces that ive been working on. And...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.