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. 9 1149
At Friday 27/10/2006 18:53, no*****@gmail.com 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
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.com 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
no*****@gmail.com 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
At Friday 27/10/2006 19:48, no*****@gmail.com 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 no*****@gmail.com 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 no*****@gmail.com 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_xfunc(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(tools), 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_xhandler(app_xfunc)
mody.yfunc(a,b,..., cbProgress=app_xfunc)
def app_xstepper():
yield next
mody.yfunc2(a,b,..., step=app_xstepper)
....
* 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
robert ha escrito: no*****@gmail.com 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_xfunc(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(tools), 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_xhandler(app_xfunc)
mody.yfunc(a,b,..., cbProgress=app_xfunc)
def app_xstepper():
yield next
mody.yfunc2(a,b,..., step=app_xstepper)
...
* 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
On Fri, 2006-10-27 at 14:53 -0700, no*****@gmail.com 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. no*****@gmail.com 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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Samuel |
last post by:
Hi,
Given the following directory structure:
---------
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |