471,350 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 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 1206
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

49 posts views Thread by Steven Bethard | last post: by
4 posts views Thread by Axel Straschil | last post: by
161 posts views Thread by KraftDiner | last post: by
9 posts views Thread by Cesar | last post: by
6 posts views Thread by Lloyd Sheen | last post: by

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.