The Artima Developer Community
Sponsored Link

Python Buzz Forum
Dynamic __doc__

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ian Bicking

Posts: 900
Nickname: ianb
Registered: Apr, 2003

Ian Bicking is a freelance programmer
Dynamic __doc__ Posted: Dec 7, 2005 3:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ian Bicking.
Original Post: Dynamic __doc__
Feed Title: Ian Bicking
Feed URL: http://www.ianbicking.org/feeds/atom.xml
Feed Description: Thoughts on Python and Programming.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ian Bicking
Latest Posts From Ian Bicking

Advertisement

One thing I did in the last (0.4) release of FormEncode is add some dynamic docstrings. I've started relying more heavily on automatically-generated documentation, but there was information specific to validators that I wanted to include without the tedium or redundancy of copying it into the docstring. In particular, I wanted to display information about all of the error messages a validator can produce.

Since I was already using a __classinit__ metaclass it was easy to add code that gets run for every subclass of Validator. And I just did something like this (to paraphrase):

def __classinit__(cls, new_attrs):
    ...
    for name, value in cls._messages.items():
        cls.__doc__ += '\n``%s``: %s' % (name, value)

Before I had been thinking about putting all sorts of cleverness into the documentation generation system so it could detect these attributes, but this is much better all around, and simpler to boot. Of course, it would be nice to have indexes and whatnot, but ultimately I think those would be better implemented in the underlying documentation system (restructured text moreso than Pudge), and maybe I could just add a special role to the text.

Anyway, it made me feel clever so I thought I'd share.

Read: Dynamic __doc__

Topic: iTunes... Previous Topic   Next Topic Topic: Templates Shmeplates

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use