Using's database library for data munging

Thursday, 23 October 2008

The more I use, the more I appreciate its compact libraries.'s database library is very handy in writing scripts that do the manipulation and heavy lifting. Let's cut to the chase and see how to use's database library.

Consider the simple use-case of normalising all the tags for articles stored in articles table to a standard format of lower__case_words (often called a slug). The current tags have a mix of upper case words and whitespace, which does not look very neat (Ah! geek OCD).

import web
db = web.database(dbn=os.environ.get('DATABASE_ENGINE', 'mysql'),
                  db='devdb', user="user", passwd="passwd")

def cleanup_blog_tags():
    tags ='tags')
    ccount = 0
    for t in tags:
        name =
        slug = slugify(name)
        print name, slug
        db.update('tags', where="id=$id", _test=False,
            vars={'id'}, slug=slug, name=name)
    print '%s tags updated' % (ccount, )

def main():

if __name__ == '__main__':

blog comments powered by Disqus