473,327 Members | 1,930 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,327 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 1265
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.