Automatic Generation of Python Class Files

Sunburned Surveyor sunburned.surveyor at gmail.com
Mon Oct 22 20:42:57 CEST 2007


On Oct 22, 11:23 am, "Chris Mellon" <arka... at gmail.com> wrote:
> On 10/22/07, Sunburned Surveyor <sunburned.surve... at gmail.com> wrote:
>
>
>
>
>
> > On Oct 22, 10:26 am, "Chris Mellon" <arka... at gmail.com> wrote:
> > > On 10/22/07, Sunburned Surveyor <sunburned.surve... at gmail.com> wrote:
>
> > > > I was thinking of a way I could make writing Python Class Files a
> > > > little less painful. I was considering a Ptyhon script that read a
> > > > file with a list of property names and method names and then generated
> > > > a skeleton class file.
>
> > > > I was even thinking of automatically generating the shell for doc
> > > > strings and epydoc tags.
>
> > > > Is there existing scripts that do something like this? If not, I will
> > > > try to come up with something. If I'm sucessful I'll release the code
> > > > under the GPL and will report back to the list.
>
> > > > However, I thought I would check here first so that I don't reinvent
> > > > the wheel.
>
> > > > Thanks,
>
> > > > Scott Huey
>
> > > I can't think of a single reason why you would ever want to do this,
> > > since your "list of method and property names" would be just as
> > > verbose as just typing the actual python code.
>
> > > Auto generated documentation stubs are considered harmful because they
> > > take the place of real documentation.- Hide quoted text -
>
> > > - Show quoted text -
>
> > Chris,
>
> > You wrote: " can't think of a single reason why you would ever want to
> > do this,
> > since your "list of method and property names" would be just as
> > verbose as just typing the actual python code."
>
> > I don't think I understand how this would be the same amount of
> > typing. Consider the following example that would generate a Monster
> > class file from an input text file (I only did one property and method
> > in generated class file.):
>
> > Contents of input text file:
>
> > [Name]
> > Fire Breathing Dragon
>
> > [Properties]
> > Strength
> > Scariness
> > Endurance
>
> > [Methods]
> > eatMaiden argMaiden
> > fightKnight argKnight
>
> > Generated Python Class File:
>
> > def class FireBreathingDragon:
>
> >    def getStrength(self):
> >       """
> >       Docstring goes here.
>
> >       @return
> >       @rtype
> >       """
> >       return self.strength
>
> >    def setStrength(self, argStrength):
> >       """
> >       Docstring goes here.
>
> >       @param argStrength
> >       @ptype
> >       """
> >       return self.strength
>
> >    def eatMaiden(self, argMaiden):
> >       """
> >       Docstring goes here.
>
> >       @param argMaiden
> >       @ptype
> >       """
>
> Your getters/setters are useless (Python isn't Java) and so are the
> docstrings, because you have to write them over again anyway. Removing
> them results in 3 lines of code, quite a bit shorter than your  input
> file, and it doesn't have any of the quite well known issues with this
> sort of boiler plate code generation.- Hide quoted text -
>
> - Show quoted text -

Thank you for all of the feedback. As you can no doubt tell I am still
learning to think in "Python" instead of Java.

My intention is to copy the generated text into the module that will
containt the class definition. (I should have said "class definition"
not "class file".

I also intended to add statements creating properties from the getter
and setter methods. I understand that getters and setters aren't
really necessary if you aren't making a property. I just forgot to add
the property statements to my example.

I don't know much about pylint, but I will check it out. It sounds
like a nifty tool. At any rate, I always try to add documentation to
my code, but I can see how the docstring stubs could cause more harm
than good if this wasn't a programmer's habbit.

Thank you for all of the good responses. I hope I didn't offend anyone
with my "Javaness". I think Python is a great language and I hope to
continue learning and using it.

Scott Huey




More information about the Python-list mailing list