473,608 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the cleanest way to for a module to access objects from the script that imports it?

Hi,

I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?

Sincerely,
Noam Samuel.

Oct 27 '06 #1
9 1224
At Friday 27/10/2006 18:53, no*****@gmail.c om wrote:
>I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?
Put the bulk of your "original script" into modules so they are
easily importable from inside the plugins.
In your application, you can scan the available plugins (using
os.listdir by example) and import them.
(Of course this is risky so you must trust the plugin developers...)
--
Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Oct 27 '06 #2
Wouldn't importing and re-importing the same modules cause considerable
resource bulk? Or does python cache that stuff?

On Oct 27, 6:28 pm, Gabriel Genellina <gagsl...@yahoo .com.arwrote:
At Friday 27/10/2006 18:53, noam...@gmail.c om wrote:
I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?Put the bulk of your "original script" into modules so they are
easily importable from inside the plugins.
In your application, you can scan the available plugins (using
os.listdir by example) and import them.
(Of course this is risky so you must trust the plugin developers...)

--
Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! -http://correo.yahoo.co m.ar
Oct 27 '06 #3
no*****@gmail.c om wrote:
Wouldn't importing and re-importing the same modules cause considerable
resource bulk? Or does python cache that stuff?
If a module is already imported, then the import statement just uses the
cached module. However, you can force a full reload of the module using
the following syntax:

reload(mymodule )

-Farshid
Oct 27 '06 #4
At Friday 27/10/2006 19:48, no*****@gmail.c om wrote:
>Wouldn't importing and re-importing the same modules cause considerable
resource bulk? Or does python cache that stuff?
No. Once a module is imported by the first time, the module object is
placed in sys.modules; if a subsequent import finds the module there,
it's not reloaded from disk.
See <http://docs.python.org/ref/import.html>
--
Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Oct 27 '06 #5
no*****@gmail.c om schrieb:
Hi,

I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?
If you really have a C++ background, you should be aware that this
language requires each and every bit of declarations to be known
beforehand. Which means you need stuff to be factorized into header
files, and include them wherever you want to use things.

Python is waaaay more relaxed in this regard. As long as you only use
objects, it actually doesn't give a damn about "knowing" them. if you
need to instantiate them, there isn't anything wrong about importing a
main module from a plugin module - you can do that. However, things can
get messed up if you really do a circular import, meaning that you don't
have a plugin system with lazy loading, but "real" cycles.

Diez
Oct 28 '06 #6
no*****@gmail.c om wrote:
Hi,

I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?
you can import __main__ in your module: import __main__; __main__.app_xf unc(y)

But for most cases I'd say your "problem" is an indication of weak design (thanks to Pythons clear module tech).

Maybe:

* if the funcs are tools, put them in an extra module

* if its about app-global parameters(tool s), make a module "myglob" or so

* if the funcs have app-context, hand over/set them as "callback" functions or iterators like ..

def app_xfunc(par): pass
mody.set_xhandl er(app_xfunc)
mody.yfunc(a,b, ..., cbProgress=app_ xfunc)
def app_xstepper():
yield next
mody.yfunc2(a,b ,..., step=app_xstepp er)
....
* if you have 2 moduls on equal dependency level and each needs the other (sometimes) - thus you don't want to have one big module, then cross import them ..

#modx
import mody
def fx():
mody.doy()
#mody
import modx
def fy():
modx.dox()
Python allows everything most easy for that kind of problems of all langs I know of. Mainly the fact that a module is a real object in Python provides tremendous flexibility and self-similarity of techniques. Ruby for example is weired - even really bad - in this.

-robert
Oct 28 '06 #7

robert ha escrito:
no*****@gmail.c om wrote:
Hi,

I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?

you can import __main__ in your module: import __main__; __main__.app_xf unc(y)

But for most cases I'd say your "problem" is an indication of weak design (thanks to Pythons clear module tech).

Maybe:

* if the funcs are tools, put them in an extra module

* if its about app-global parameters(tool s), make a module "myglob" or so

* if the funcs have app-context, hand over/set them as "callback" functions or iterators like ..

def app_xfunc(par): pass
mody.set_xhandl er(app_xfunc)
mody.yfunc(a,b, ..., cbProgress=app_ xfunc)
def app_xstepper():
yield next
mody.yfunc2(a,b ,..., step=app_xstepp er)
...
* if you have 2 moduls on equal dependency level and each needs the other (sometimes) - thus you don't want to have one big module, then cross import them ..

#modx
import mody
def fx():
mody.doy()
#mody
import modx
def fy():
modx.dox()
Python allows everything most easy for that kind of problems of all langs I know of. Mainly the fact that a module is a real object in Python provides tremendous flexibility and self-similarity of techniques. Ruby for example is weired - even really bad - in this.

-robert
Oct 28 '06 #8
On Fri, 2006-10-27 at 14:53 -0700, no*****@gmail.c om wrote:
Hi,

I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?
I have a system that uses modules as plugins also. These are loaded
dynamically when the user specifies them from a gui. I put all these
modules in an array using the __import__ function. I found, though,
that I needed to specify whether or not each module had actually loaded,
or if there had been an exception (module not found or whatever). So I
wrote a wrapper object that would try to load the module and store it as
a local attribute. I made my wrapper object implement functions like
__getattr__ and pass any unknown calls into the module object itself,
making the wrapper object act as if it was the module, but having extra
capabilities, such as being able to tell me if the module had actually
loaded or not.

Michael

>
Sincerely,
Noam Samuel.
Oct 31 '06 #9

no*****@gmail.c om wrote:
Hi,

I am new to python and am currently writing my first application. One
of the problems I quickly ran into, however, is that python's imports
are very different from php/C++ includes in the sense that they
completely wrap the imported script in a module object. One of the
problems with this was that a plugin system that I am making requires
use of objects, classes and the such from the original script. Thus, on
one hand, I am hesitant to use execfile(), since I *do* want to wrap
the plugin up, but on the other hand, I want the plugin to be able to
use functions from the original script. Any ideas?
This is a situation I've been in before when writing plugins - you need
some way of 'connecting' the plugin to the main application.

I usually provide a baseclass which the user should subclass to
implement the plugin.

The override something like an 'onActivate' method, which when called,
is passed in (by the application) all the information they need.

They can also optionally override 'onLoad' and 'onUnload' or whatever
other methods you want to provide.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Sincerely,
Noam Samuel.
Oct 31 '06 #10

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

Similar topics

54
6539
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
28
3277
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
4
8349
by: Brad Tilley | last post by:
When memory usage is a concern, is it better to do: from X import Y or import X Also, is there a way to load and unload modules as they are needed. I have some scripts that sleep for extended periods during a while loop
6
1834
by: kimes | last post by:
I've just started digging into how python works.. I found that other mudules are clearly declared like one file per a module.. But the only os.path doesn't have their own file.. ye I know is has actually depending on os like in my case posixpath.. What I'd love to know is.. when I call import os.path.. how happened under the hood?
31
2505
by: N.Davis | last post by:
I am very new to Python, but have done plenty of development in C++ and Java. One thing I find weird about python is the idea of a module. Why is this needed when there are already the ideas of class, file, and package? To my mind, although one CAN put many classes in a file, it is better to put one class per file, for readability and maintainability. One can then create packages and libraries, using groups of files, one
121
9992
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
0
1014
by: Glenn | last post by:
I'm trying to write/compile a class that (1) accesses all the intrinsic ASP.NET objects (2) Uses Server.Transfer to redirect a user back to a log in page if a session has expired. I've gotten inconsistent results. The redirection works perfectly if the user session has expired, but throws an exception if it hasn't. Both "ASPIntrinsicObjects" and "Miscfunctions" compile correctly without a hitch, but bomb out upon executio I start with a Base...
0
962
by: Jorgen Bodde | last post by:
Hi All, I am wrestling with some architecture inside my app. Let's say I have a tunings collection, which contains e.g. 23 types of guitar tunings. In my song object I want to restore a relation between one of the tuning objects inside the tunings module. I already figured out I need somethign like a global collection inside the tunings module,. but how global is it? When I am inside my app object, and import the tunings module, can I...
6
1783
by: Samuel | last post by:
Hi, Given the following directory structure: --------- |-- Obj.py |-- __init__.py |-- foo | |-- FooTest.py | `-- __init__.py
0
8059
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
8470
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
8145
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
8330
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
5475
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
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
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
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1328
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.