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 1211
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: 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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |