473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loading classes dynamically

What is the most pythonic way to load a class and instaniate an object
dynamically. Right now, I am using eval:

try:
load = eval('%s(props) ' % (props['plugin.generat e']))
except:

It works, doesnt seem very safe. Where props['plugin.generat e'] is a
class name string. And 'props' is the first arg in the constructor.

What do you think?

--
Ramza from Atlanta
http://www.newspiritcompany.com
Aug 15 '05 #1
1 1487
Ramza Brown wrote:
try:
load = eval('%s(props) ' % (props['plugin.generat e']))
except:

It works, doesnt seem very safe. Where props['plugin.generat e'] is a
class name string. And 'props' is the first arg in the constructor.


Where is the class defined? The right answer to this is usually
somethign like:

load = getattr(some_mo dule, props['plugin.generat e'])(props)

If the class is defined in the current module, another possibility is:

load = globals()[props['plugin.generat e']](props)

HTH,

STeVe
Aug 15 '05 #2

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

Similar topics

0
1604
by: dale cyr | last post by:
nick: hope i'm not stating the obvious for you, but you *are* aware, i presume, that your loading app wants to find the loaded app in the loading app's runtime directory, or a sub directory of the loading apps runtime directory... (i recently changed the build path of a "loaded at runtime" dll and chased this very symptom for a few...
2
3111
by: Foehammer | last post by:
Hello, I'm trying to load an assembly dynamically using an app domain. This is a proof-of-concept for a larger project, so please excuse the lame class names. TestLib is the dll where all the dynamic loading code will go. The assemblies being dynamically loaded do not contain any code. They are resource-only assemblies. I have succeeded in...
17
4792
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. The trick is, the instatiation will be based on a string with the exact same name as the type that I am trying to instantiate. Obviously, writing a...
1
1263
by: ASayre | last post by:
I'm currently developing a client that connects to a server and exchanges data with the server back and forth. The client retains the data in specified classes depending on what was received. While adding a plugin type API I ran into the following problem: I want users to be able to dynamically un/re-load their plugins as needed, but...
2
2127
by: NGM | last post by:
Hello All I have a unmanaged C++ DLL, which has been wrapped up with a manged C++ DLL. When i refer to this managed DLL in Windows form based applications it works out fine. But when i refer to the the same managed C++ DLL in ASP.Net application. ngmujumdar@hotmail.com/ amar_a_k@yahhoo.co NG
6
6869
by: Dan Dorey | last post by:
I actually have two questions here, but I'll start by giving an outline of what I'm trying to do. I'm building an app with a simple plugin architecture (all in the same app domain). I have each plugin in a separate sub-dir inside a "Plugins" directory. Each plugin consists of at least two DLLs (a controller and a driver). However, most...
1
1830
by: Bob Rock | last post by:
Hello, I'm new to ASP.NET and I've been looking into the topic of dynamically loading (typically accomplished with a LoadControl followed by a MyControl.Controls.Add()) both user controls and asp.net web controls. I searched for articles on the internet and in the end I must say that I'm confused. I read about issues related to: a.....
5
1345
by: news.microsoft.com | last post by:
I'm strongly considering abandoning the one-physical-file-per-page model and going with an arcitecture that simply loads content from classes dynamically. There will be only one page that a user will go to, and all variation from navigation will come from AJAX calls to load and replace content in a main frame. .... so my concern with this...
5
5116
by: Pete Marsh | last post by:
Wondering if anyone can recomend some sample code for dynamically loading the GD module. I have tried setting the extension dir in php.ini, and loading the GD module from there when apache is started, but it won't load. There is also the option of dynamically loading the module at run time of the script. LD() i believe is the call.
11
3084
by: digz | last post by:
Hello, Apologies if this is the wrong group for this question. I want to design an interface , where for a custom functionality , the client writes a new class with the function implementation and then puts the name of the Class as a configuration parameter or something and then the main program loads the new class like a module and the...
0
7464
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...
0
7396
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...
0
7805
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...
1
7413
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...
0
7751
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...
0
4943
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...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
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
0
700
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...

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.