473,602 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using "external" vars on module load time


Hi

I have a script that looks for modules and tries to load them. After
loading I attach the "server"-API function to it.

module = __import__(modu le_name, globals(), locals(), [])
module.server = server.server_a pi

Now, some modules need the "server" functionality on load/init. I tried to
modify the globals():

my_globals = globals().copy( )
my_globals['server'] = server.server_a pi
module = __import__(modu le_name, my_globals, locals(), [])
module.server = server.server_a pi

This did not work as intended the module to be imported did not get the
"server" from my_globals. The solution I have, is to add __init_module__ ()
method that is called after the module was imported:

module = __import__(modu le_name, globals(), locals(), [])
module.server = server.server_a pi
if module.__dict__ .has_key('__ini t_module__'):
module.__init_m odule__()

This is not really satisfying. I would like:

Load a module and hand in already the server_api. How can I achieve this?

Thanks for any suggestions,
Marco

Jun 14 '06 #1
3 1542
Marco Aschwanden wrote:
Load a module and hand in already the server_api. How can I achieve this?


Using execfile() is the simplest approach:

import imp
import sys

def import_preloade d(name, path=None, variables={}):
if path is None:
file, path, description = imp.find_module (name)
module = imp.new_module( name)
module.__dict__ .update(variabl es)
execfile(path, module.__dict__ )
sys.modules[name] = module
return module

if __name__ == "__main__":
demo = import_preloade d("demo", variables=dict( whatever=42))

I think you should consider a class instead of a module, though.

Peter

Jun 14 '06 #2
Marvelous that was the solution I was looking for.
I think you should consider a class instead of a module, though.


What I don't get: Why should I consider using a class?

Greetings from sunny Switzerland,
Marco

Jun 14 '06 #3
Marco Aschwanden schrieb:
Marvelous that was the solution I was looking for.
I think you should consider a class instead of a module, though.


What I don't get: Why should I consider using a class?


Because sharing state in such a way that several functions need to
access some later bound globals cries for a class that gets that very
state passed on instantiation.

Diez
Jun 14 '06 #4

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

Similar topics

9
6888
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different titles, I found a total of about five pages that covered js files. For all practical purposes, these pages said, "You can use js files. But we won't tell you how." Therefore, I hope someone can answer a few questions about js files... 1....
1
2650
by: Paul Bawin | last post by:
Hi All, How can I load an object A that host an object B that is defined in an other Assembly ? Thank you See the exemple below : I have a DLL with these classes
0
6579
by: Learner | last post by:
Gurus, I have a C# .NET Web application that downloads a datagrid to an Excel sheet. But when I upload the same excel sheet to server again, if gives me this wiered message "External table is not in the expected format". I have some idea why this is happening..Because my downloaded file is getting saved as an HTML file eventhough it will get opened in Excel and looks like an Excel file. If I open the downloaded file and do a
3
3020
by: sewerized | last post by:
Hello all, I'm having a problem with a user preferences form I'm creating. I want to use dynamic dropdown SELECT boxes for this so my page doesn't get cluttered with 100000 links. Since I have 10000 Options embedded in the SELECT tags, I'm using an External JS file to display them. I'm using these same Options for different SELECT tags, so they can be "re-used (or looped)", instead of writing them over and over again.
2
3997
by: Ognjen Bezanov | last post by:
Hello, I have some external C libraries I would like to use with python. I have been searching on the internet and found many such modules/bindings for libraries (e.g. Py-Lame) but have not yet come across any information of how to actually go about creating such bindings, so I was wondering if anybody here could point me in the right direction?
2
2472
by: Gnus | last post by:
Hi all! I have a little(?) problem with external schema location... I'm using xerces/xalan (C++) for doing some actions with some set of xml-files. Some of these files must validating against schema, which location specify in config file ("external" schema). Other files must be validating against schema, which location specify in parsed xml file (via xsi:shemaLocation attribute, "internal" schema) How can I switch between these ...
2
1046
by: Thijs | last post by:
Hello, I´ve got a question regarding controlling an external app. I´m writing a vb.NET app that, at some point, needs to push a button in an external app. This external app has no API whatsoever. Is there a way to do this? I guess I have to raise a ButtonX_click event in the other app, but I don´t know how to do that, nor do I know how to even find the name of this ButtonX. Does anyone have a clue as how to do this?
4
3238
by: =?Utf-8?B?U0g=?= | last post by:
There seems to be an issue with .NET 3.5/Visual Studio 2008 (Team System). After convincing my company to create our latest project in .NET 3.5, this is kind of reflecting bad on me. Problem: Solution will NOT compile. Error is simply: "External component has thrown an exception". NO other details.
0
1466
by: ravikim1 | last post by:
Hi , use Sort::External; $output = '1.txt'; open(FILE, $output); my $sortex = Sort::External->new( mem_threshold => 1024**2 * 1024 ); while (<FILE>) { $sortex->feed($_); # tried to change it to ~$_ but did not work !!!!! }
0
7993
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
7920
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
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
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
5440
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
3900
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
1254
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.