Will's blog

purpose: Will Kahn-Greene's blog of Python, Linux, random content, PyBlosxom, Miro, and other projects mixed in there ad hoc, half-baked, and with a twist of lemon

[ home | blog home | recent activity | guestbook | plugins i'm using (19) ]

Tue, 27 Jan 2004

Tip for storing state between callbacks

Some plugins can do all their functionality in one callback and don't have any need to store state. These are your simple plugins that do things like count how many words there are in an entry and then store that number in the entry object's properties. More complex plugins do some stuff in one callback, then need to store their state, and do some other things in another callback.

In PyBlosxom 0.8 and prior, you could do this by storing the state in global variables on the plugin module like this:

      state = {}

      def cb_date_head(args):
          global state
          if state.has_key(...):
              ...

      def cb_filelist(args):
          global state
          ...
          state["blah"] = "blahblah"
   

This is "ok" for previous versions of PyBlosxom. In future versions (post 0.8), this is not ok and the official way to maintain state is to store state information on the Request object in the data dict using a unique key:

      STATE_KEY = "myplugin_state"

      def cb_date_head(args):
          request = args["request"]
          data = request.getData()

          if data.has_key(STATE_KEY) and data[STATE_KEY]["blah"] == "blahblah":
              ...


      def cb_filelist(args):
          request = args["request"]
          data = request.getData()

          data[STATE_KEY] = {}
          data[STATE_KEY]["blah"] = "blahblah"
   

changelog

I reserve the right to remove comments that are anonymous, flames, spammy, inappropriate and/or rude. Sometimes I'll reply in email directly--so make sure your email address is correct. New comments get placed in a "draft" status and will NOT show up on the site until I explicitly approve it. Sometimes that happens within 24 hours.

If you can't for some reason post a comment, send me an email: willg at bluesock dot org.

Your name:


Your e-mail address (this doesn't get displayed to anyone--sometimes I'll reply directly to you):


URL of your website (optional):


Comment:


Yes, I am a human!


pyblosxom::1.5-dev git-master

All contents Copyright 1996 to 2010 Will Guaraldi Kahn-Greene.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.