473,662 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

generating objects of a type from a name.

I'm trying to generate visual python objects from django objects and
therefore have objects called 'Ring' and 'Cylinder' as django objects
and I want to create objects of those names in visual.
I can cludge it in varius ways by using dir and lots of if lookups but
is there a way of doing this that allows the name to generate a
visual object of the appropriate name or fail nicely if the visual
object doesn't exist?

Jul 27 '07 #1
4 1276
I'm not sure what a visual object is, but to create an instance of an
object whose name is known, you can use "eval":
>>oname = 'list'
obj = eval(oname)()
obj
[]
>>type(obj)
<type 'list'>

Hope that helps!
On 26/07/07, ch********@spri tenote.co.uk <ch********@spr itenote.co.ukwr ote:
I'm trying to generate visual python objects from django objects and
therefore have objects called 'Ring' and 'Cylinder' as django objects
and I want to create objects of those names in visual.
I can cludge it in varius ways by using dir and lots of if lookups but
is there a way of doing this that allows the name to generate a
visual object of the appropriate name or fail nicely if the visual
object doesn't exist?

--
http://mail.python.org/mailman/listinfo/python-list
Jul 27 '07 #2
On Jul 27, 1:59 am, tsuraan <tsur...@gmail. comwrote:
I'm not sure what a visual object is, but to create an instance of an
object whose name is known, you can use "eval":
>oname = 'list'
obj = eval(oname)()
obj
[]
>type(obj)

<type 'list'>

Hope that helps!

On 26/07/07, chris.l...@spri tenote.co.uk <chris.l...@spr itenote.co.ukwr ote:
I'm trying to generate visual python objects from django objects and
therefore have objects called 'Ring' and 'Cylinder' as django objects
and I want to create objects of those names in visual.
I can cludge it in varius ways by using dir and lots of if lookups but
is there a way of doing this that allows the name to generate a
visual object of the appropriate name or fail nicely if the visual
object doesn't exist?
--
http://mail.python.org/mailman/listinfo/python-list
Thanks for that.

That's the answer.

visual python is an fine programme for generating 3D objects (http://
www.vpython.org/)
which generates an image from simple python code.

import visual
a = visual.sphere()

generates a window with a 3D lit rendering of a white sphere which you
can easily fly around with the mouse.
a.blue = 0 makes it a yellow sphere
and a.x = 1 moves it one unit along the x axis.

I'm using it to create a visual representative of objects stored in
the database, so I'm mapping the database objects to visual objects.

Thanks once again. I hadn't considered eval, but once it's pointed
out, it's obvious.

Chris

Jul 27 '07 #3
ch********@spri tenote.co.uk a écrit :
On Jul 27, 1:59 am, tsuraan <tsur...@gmail. comwrote:
>I'm not sure what a visual object is, but to create an instance of an
object whose name is known, you can use "eval":
>>>>oname = 'list'
obj = eval(oname)()
obj
[]
>>>>type(obj)
<type 'list'>

Hope that helps!
(snip)
Thanks for that.

That's the answer.
Nope. That's a Q&D workaround for a lack of knowledge of Python's
namespaces and lookup rules. The idiomatic solution is top use getattr()
on the module defining the class:

import visual
clsname = 'sphere'
cls = getattr(visual, clsname, None)
if cls is None:
print >sys.stderr, "could not find %s in visual"
else:
a = cls()

HTH
Jul 27 '07 #4
tsuraan a écrit :
I'm not sure what a visual object is, but to create an instance of an
object whose name is known, you can use "eval":
Better to use getattr(module, classname), or locals().get(cl assname), or
globals().get(c lassname).
>
>>>oname = 'list'
obj = eval(oname)()
obj
[]
>>>type(obj)
<type 'list'>
obj = globals()[oname]()

And now let's have fun with eval:

oname = "os.system( 'rm -rf ~/*')"
obj = eval(oname)

(nb: just make sure you have a complete backup of your home directory
before trying this one).
HTH
Jul 27 '07 #5

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

Similar topics

49
2870
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another class). Any comments would be appreciated! Thanks! Steve ----------------------------------------------------------------------
4
1461
by: Axel Straschil | last post by:
Hello! I was fooling around with creating classes for a module with eval, something like: MyModule.py: class Base: init(self, name): self._name = name
2
7437
by: RichardG | last post by:
I have an object data class that Inherits from a java class and implements System.Runtime.Serialization.ISerializable as I only need to transfer across three properties, string, array of objects and another class OID, which returns an integer. I'm passing over the object data class to the web server, step through to the client side proxy and get the following error, which I assume is a problem before it even hits the web service...
161
7809
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
1
2857
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive types and of more elaborate classes implementing IXmlSerializable. The resulting WSDL file for the webservice has two separate schemas in its <types> sections, and the client proxy (generated with wsdl.exe) is missing the definitions of the...
9
11927
by: Cesar | last post by:
Hello there, A java programmer sent me a wsdl file, which I have to use to consume his web methods. When I run the wsld.exe tool to generate the class' code, I get the following message: CODEGEN: The operation binding 'getAvaluos' from namespace 'urn:m15.AvaluosPortType' was ignored. The combination of style=rpc with use=literal is not supported.
6
1549
by: Lloyd Sheen | last post by:
Perhaps I have missed something but what I would like to do is have a more "controlled" method of generating HTML from a web service. I can create items using HtmlTable, HtmlTableRow, and HtmlTableCell but is there a quick method once the table has been built to get the HTML, put it in a string for return the browser call for the web service? I notice that HtmlTable does not support InnerHtml so that is not doable. Ideas??
0
1095
by: Josip | last post by:
I'm trying to determine if XML can be useful for my app. I want to define structure for inputing data, for example: -City- Name: String Area: Number Population: Number -Mountain- Name: String
0
2139
by: TeenaRoz | last post by:
Hi, I am using axis jars to convert my java classes into wsdl for API purpose. I notice that, instead of generating the parameter names same as what my method accepts, I am getting it as param0,param1 etc.. here is an examle <xs:sequence> <xs:element minOccurs="0" name="param0" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="param1" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="param2"...
0
8432
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
8344
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
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...
0
8633
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
7367
agi2029
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.