Hi list and python gurus :-)
I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:
exec 'import '+some_modulename
But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
And have to I check if the modul is already loaded?
Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:
exec 'newclass = '+classname+'()'
newclass.somefunction()
Again it seems to easy. Is there a better/proper way to do it?
Do anybody now a good howto or tutorial to this?
Many thanks and hope you all have a happy new year :-)
/marc 5 1515
On Dec 30, 8:24 am, marcroy.ol...@gmail.com wrote:
Hi list and python gurus :-)
I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:
exec 'import '+some_modulename
The correct way to do this is use the __import__ function. It takes
the string name of the module you want to import and returns the
module.
new_mod = __import__(some_modulename)
>
But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
Well, it's generally frowned on to use exec and eval.
And have to I check if the modul is already loaded?
sys.modules is a list of all imported modules, but python won't import
a module if it's already been loaded.
>
Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:
exec 'newclass = '+classname+'()'
newclass.somefunction()
Again it seems to easy. Is there a better/proper way to do it?
If you just have the string name of a class, you have to use eval or
exec:
newclass = eval(classname)
However, if you have the class object, you can just instantiate that:
class LargeClass:
def meth(): pass
some_class = LargeClass
new_class = some_class()
some_class.meth()
>
Do anybody now a good howto or tutorial to this?
Many thanks and hope you all have a happy new year :-)
You, too!
>
/marc
Hallchen! ma***********@gmail.com writes:
I'm playing with some mod_python and web development. And in me
code I need to do som dynamic imports. Right now I just do a:
exec 'import '+some_modulename
But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc) And have to I check if the modul is
already loaded?
I use the imp module for this:
try:
file, pathname, description = imp.find_module(full_name)
my_module = imp.load_module(full_name, file, pathname, description)
finally:
file.close()
Tsch,
Torsten.
--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for further contact info.)
En Sun, 30 Dec 2007 12:24:53 -0200, <ma***********@gmail.comescribi�:
I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:
exec 'import '+some_modulename
But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
Use __import__, specially if some_modulename comes from the outside.
What if some_modulename contains "modulename\nsome_nasty_function_call()"
And have to I check if the modul is already loaded?
Not needed; the standard import machinery already does that.
Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:
exec 'newclass = '+classname+'()'
newclass.somefunction()
Again it seems to easy. Is there a better/proper way to do it?
Use getattr to obtain the desired class from the containing module, then
use it as any other class:
the_module = __import__(some_modulename)
the_class = getattr(the_module, classname)
o = the_class()
o.somefunction()
Never use exec/eval and friends - and never ever use them in a web
application!
Do anybody now a good howto or tutorial to this?
No... what do you want covered?
Many thanks and hope you all have a happy new year :-)
Thanks, and a happy new year for you too!
--
Gabriel Genellina
First of thanks to all for you, especially for the quick replys.
Just need to walk the dog then I giv it a short.
On Dec 30, 3:57*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>
Do anybody now a good howto or tutorial to this?
No... what do you want covered?
Nothing, think you reply coved it all.
Otherwise I will be back ;-) ma***********@gmail.com schrieb:
First of thanks to all for you, especially for the quick replys.
Just need to walk the dog then I giv it a short.
Please, don't kill your dog! We're a peace-loving community here that
respects dogs, and snakes and even trolls.
SCNR,
Diez This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matthew Wilson |
last post by:
Hi-
I'm writing a bunch of classes, several of which need functions and
variables defined in the math module. In some instances, I'm going to
import my module like this:
import myshapes
...
|
by: Rudy |
last post by:
I'm building a application that will interface with SQL server. The amount
of data is not alot, but the data will need to be proccessed quickly at about
3 - 4 minutes at a time. Then Idle for 2...
|
by: Alfonso Morra |
last post by:
I have written the following code, to test the concept of storing
objects in a vector. I encounter two run time errors:
1). myClass gets destructed when pushed onto the vector
2). Prog throws a...
|
by: Mark |
last post by:
I have been working for quite some time on this issue which in theory
should be quite simple. The problem is that the Cancel and Save
events are not fired when their respective buttons are...
|
by: Stan Sainte-Rose |
last post by:
Hi,
I use this bit of code to generate dynamic tables in the page load section
....
Dim ntable as New Table
For i = 1993 To 2008
ntable = New Table
ntable.ID = "Q" + i.ToString
....
....
| |
by: Bass Pro |
last post by:
Hi,
I am creating textbox, radiobuttonlist and checkboxlist dynamically
depending on data from a table. It is a questionnaire.
I add the control on a Panel control during the 1st load_page event....
|
by: JMann101 |
last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I
have a datagrid which list users and when an admin clicks "edit" a
defined column becomes visible and a dynamic listbox control...
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: vamshiv |
last post by:
hi...im working on asp.net with VB
i have created a database which hav fields id,name,image path
in web form i hav written code for creation of image buttons dynamically
i hav connected the form...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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 projectplanning, coding, testing,...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |