473,224 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 1259
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********@spritenote.co.uk <ch********@spritenote.co.ukwrote:
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...@spritenote.co.uk <chris.l...@spritenote.co.ukwrote:
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********@spritenote.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(classname), or
globals().get(classname).
>
>>>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
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...
4
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
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...
161
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.....
1
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...
9
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: ...
6
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...
0
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:...
0
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.