By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

Inspect Python Classes for instance data information

P: n/a
Hello

I want to creat a program that can inspect a set of classes that i have
made and spit out a savable version of these classes. To do this I need
to be able to inspect each class and get all of its instance data as
well as information about a particular meathod. This is what i want it
to do:
class song:
def __init__(self, name, artist, album, published):
self.name = name
self.artist = artist
self.album = album
self.published = published
def build(self, name, artist, album, published):
self.name = name
self.artist = artist
self.album = album
self.published = published

#now what i want to do with this dummy class
s = song("12:51", "The Strokes", "Room on Fire", 2005)
print getTextVersion(s)

<class>
<name>song</name>
<data>
<attribute>
<date_type>string</data_type>
<value>12:51</value>
<attribute_name>name</attribute_name>
</attribute>
<attribute>
<date_type>string</data_type>
<value>The Strokes</value>
<attribute_name>artist</attribute_name>
</attribute>
<attribute>
<date_type>string</data_type>
<value>Room on Fire</value>
<attribute_name>album</attribute_name>
</attribute>
<attribute>
<date_type>int</data_type>
<value>2005</value>
<attribute_name>published</attribute_name>
</attribute>
</data>
<build_parameter_order>name artist album
published</build_parameter_order>
</class>
I have no idea how to do the inspection part of this little problem. I
could just have each class define its own meathod to convert to this
format, but that would be a lot of work when i could just make one
class that solves the problem for all classes. Is there a way to do
this?

cheers
---
Tim Henderson
mail me: ti******@gmail.com

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Tim Henderson" <ti******@gmail.com> wrote:
Hello

I want to creat a program that can inspect a set of classes that i have made and spit out a savable version of these classes. To do this I need to be able to inspect each class and get all of its instance data as
well as information about a particular meathod. This is what i want it to do:
class song:
def __init__(self, name, artist, album, published):
self.name = name
self.artist = artist
self.album = album
self.published = published
def build(self, name, artist, album, published):
self.name = name
self.artist = artist
self.album = album
self.published = published

#now what i want to do with this dummy class
s = song("12:51", "The Strokes", "Room on Fire", 2005)
print getTextVersion(s)

<class>
<name>song</name>
<data>
<attribute>
<date_type>string</data_type>
<value>12:51</value>
<attribute_name>name</attribute_name>
</attribute>
<attribute>
<date_type>string</data_type>
<value>The Strokes</value>
<attribute_name>artist</attribute_name>
</attribute>
<attribute>
<date_type>string</data_type>
<value>Room on Fire</value>
<attribute_name>album</attribute_name>
</attribute>
<attribute>
<date_type>int</data_type>
<value>2005</value>
<attribute_name>published</attribute_name>
</attribute>
</data>
<build_parameter_order>name artist album
published</build_parameter_order>
</class>
I have no idea how to do the inspection part of this little problem. I could just have each class define its own meathod to convert to this
format, but that would be a lot of work when i could just make one
class that solves the problem for all classes. Is there a way to do
this?

cheers
---
Tim Henderson
mail me: ti******@gmail.com


Check out the gnosis.magic package of David Mertz (
http://www-106.ibm.com/developerwork...l-pymeta.html).
Even if it's not exactly what you want, it will give you an idea of
where to go from there.

George

Jul 19 '05 #2

P: n/a
Its not obvious what you are asking here. Try inheriting from object:
class song(object): .... def __init__(self, name, artist, album, published):
.... self.name = name
.... self.artist = artist
.... self.album = album
.... self.published = published
.... def build(self, name, artist, album, published):
.... self.name = name
.... self.artist = artist
.... self.album = album
.... self.published = published
.... s = song("12:51", "The Strokes", "Room on Fire", 2005)
dir(s) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
'album', 'artist', 'build', 'name', 'published'] s.__class__ <class '__main__.song'> song <class '__main__.song'> s.__dict__ {'album': 'Room on Fire', 'published': 2005, 'name': '12:51', 'artist': 'The
Strokes'} dir(s.__class__) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
'build'] s.build
<bound method song.build of <__main__.song object at 0x403ba10c>>
On Monday 02 May 2005 05:50 pm, so sayeth Tim Henderson: Hello

I want to creat a program that can inspect a set of classes that i have
made and spit out a savable version of these classes. To do this I need
to be able to inspect each class and get all of its instance data as
well as information about a particular meathod. This is what i want it
to do:
class song:
def __init__(self, name, artist, album, published):
self.name = name
self.artist = artist
self.album = album
self.published = published
def build(self, name, artist, album, published):
self.name = name
self.artist = artist
self.album = album
self.published = published

#now what i want to do with this dummy class
s = song("12:51", "The Strokes", "Room on Fire", 2005)
print getTextVersion(s)

<class>
<name>song</name>
<data>
<attribute>
<date_type>string</data_type>
<value>12:51</value>
<attribute_name>name</attribute_name>
</attribute>
<attribute>
<date_type>string</data_type>
<value>The Strokes</value>
<attribute_name>artist</attribute_name>
</attribute>
<attribute>
<date_type>string</data_type>
<value>Room on Fire</value>
<attribute_name>album</attribute_name>
</attribute>
<attribute>
<date_type>int</data_type>
<value>2005</value>
<attribute_name>published</attribute_name>
</attribute>
</data>
<build_parameter_order>name artist album
published</build_parameter_order>
</class>
I have no idea how to do the inspection part of this little problem. I
could just have each class define its own meathod to convert to this
format, but that would be a lot of work when i could just make one
class that solves the problem for all classes. Is there a way to do
this?

cheers
---
Tim Henderson
mail me: ti******@gmail.com


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #3

P: n/a
> > I have no idea how to do the inspection part of this little
problem. I could just have each class define its own meathod to
convert to this format, but that would be a lot of work when i
could just make one class that solves the problem for all classes.
Is there a way to do this?
Check out the gnosis.magic package of David Mertz (

http://www-106.ibm.com/develop*erwor...-pymeta*.html).
Even if it's not exactly what you want, it will give you an idea of
where to go from there


...or if you'd rather avoid metaclasses and start from scratch, this
article is a nice guideline to introspection:
http://www-106.ibm.com/developerwork...y/l-pyint.html

George

Jul 19 '05 #4

P: n/a
Thanks to both of you. Now that I know the correct terminology for what
I want to do, I can detirmine the best way to do it. I am not sure if
meta-classing is the simplist solution to this problem, however it can
be the most elegant. When I have a final implimintation of this project
it will be posted.

cheers

Tim Henderson

Jul 19 '05 #5

P: n/a
Tim Henderson wrote:
Hello

I want to creat a program that can inspect a set of classes that i have
made and spit out a savable version of these classes. To do this I need
to be able to inspect each class and get all of its instance data as
well as information about a particular meathod.


If XML format is not a requirement then the pickle module may do what you want. For XML output you
might be interested in
http://www.yared.com/pybypy/xml/index.html
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.