473,799 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_VERS ION"
- set to "1.0") that defines the version.

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

def loadModule(modu le, version, v = globals()):
import compiler
loadStr = "import %s_%s as %s" % (module, version.replace (".", "_"),
module)
eval(compiler.c ompile(loadStr, "/tmp/%s_%s_errors.tx t" % (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.lo adModule("myMod ule", os.getenv("MODU LE_VERSION"))
However, this doesn't work. The two options that do work are:

import moduleLoader
moduleLoader.lo adModule("myMod ule", os.getenv("MODU LE_VERSION"),
globals())
import moduleLoader
moduleLoader.lo adModule("myMod ule", os.getenv("MODU LE_VERSION"))
from moduleLoader import myModule
What I'm after is a way of moduleLoader.lo adModule 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 1487

Hugh> What I'm after is a way of moduleLoader.lo adModule 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.lo adModule, so my loading
code is:

import moduleLoader
myModule = moduleLoader.lo adModule("myMod ule",
os.getenv("MODU LE_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_VERS ION" - 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(modu le, version, v = globals()):
import compiler
loadStr = "import %s_%s as %s" % (module, version.replace (".", "_"), module)
eval(compiler.c ompile(loadStr, "/tmp/%s_%s_errors.tx t" % (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.lo adModule("myMod ule", os.getenv("MODU LE_VERSION"))
However, this doesn't work. The two options that do work are:

import moduleLoader
moduleLoader.lo adModule("myMod ule", os.getenv("MODU LE_VERSION"),
globals())
import moduleLoader
moduleLoader.lo adModule("myMod ule", os.getenv("MODU LE_VERSION"))
from moduleLoader import myModule
What I'm after is a way of moduleLoader.lo adModule 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
7074
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 simple/advanced html forms and how it can be used to override cookie/session
0
1269
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 recommended. Still, it seems to work. I post the recipe here since since I would like to know if 1. it really works; 2. if yes, is there a chance that it will not work in future versions of Python; 3. will it work under Jython? Even if it turns...
0
1694
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 connection on binary log file.. ( username, host.. i don't know anything ) only thing that i know is "thread_id".
3
2441
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 appreciated. public int CalcDiskSpace(string strComputer) { ManagementScope oMS = new ManagementScope("\\\\" + strComputer); ObjectQuery oQuery = new ObjectQuery("Select Size From Win32_DiskDrive"); ManagementObjectSearcher oSearcher = new
39
3199
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 function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
0
3782
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 you think? check out my website its about hacking networks and step by step guides of how to do it all. Any suggestions on information or anything you think would be interesting to write about please tell me. Also what do you think of the...
78
4979
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 problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the variable, but can't change it. Declaring the variable global in the nested function doesn't work...
11
1604
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 due to me reading a few new books etc i've started cleaning up my code and sticking the engine part into its own class and methods to tidy it up. The thing i'm struggling with (due to my lack of understanding) is the scope
0
143
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 now, even with the pickle limitation (not every class are intended to be pickled), more you didn't give any evidence or any pertinent quote of this and at least one of Guido in the above threads seems contradict you :...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.