Inspect Python Classes for instance data information

George Sakkis gsakkis at rutgers.edu
Mon May 2 21:10:59 EDT 2005


"Tim Henderson" <tim.tadh at 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: tim.tadh at gmail.com

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




More information about the Python-list mailing list