Inspect Python Classes for instance data information

T

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
 
G

George Sakkis

Tim Henderson said:
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

Check out the gnosis.magic package of David Mertz (
http://www-106.ibm.com/developerworks/linux/library/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
 
J

James Stroud

Its not obvious what you are asking here. Try inheriting from 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
....['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
'album', 'artist', 'build', 'name', 'published']{'album': 'Room on Fire', 'published': 2005, 'name': '12:51', 'artist': 'The
Strokes'}['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
'build']<bound method song.build of <__main__.song object at 0x403ba10c>>


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

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

http://www.jamesstroud.com/
 
G

George Sakkis

T

Tim Henderson

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
 
K

Kent Johnson

Tim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top