Statically Typed

because Hindley-Milner rocks

Pragmatic Programmers: Scripting “Wrist Work”

I love coding.  I like writing little functions that together create magic.  I like optimizing performance in performance critical applications.  I like refactoring code I’ve written to make it more concise and clear.  However, one thing I don’t like doing is what I’ll describe as “wrist work.”

Wrist work is code that needs to be written but is so mind-numbingly repetitive that it begs to be handed to another developer or an intern or anyone else with a pulse who isn’t you.  This is the kind of thing where you have 27+ fields that need accessors/mutators, all the same signature and arguably identical plumbing.  It’s easy to make a mistake when you’re brain is off but if you do, everyone knows it.  Luckily for me I know how to not have to do it while still doing it.

Today, at around 6pm, I volunteered myself to implement another developers idea with classes that I originally built using a script.  I didn’t finish it, my stomach won the “stay late or eat” battle.  Instead I came here to post the idea online so I could copy and paste it when I got back to work.  And who says I don’t have a good work ethic?

The goal is to create a class holding global variables containing the names of strings held in our data repository and replace all such instances with a single variable.  Normally I’m not for global variables (especially not the mutable kind) but as it was his idea and it solves an ongoing problem, I’m all for it.  Here’s what we’re aiming to have:

class Products(object):
    SOMETHING_BIG = "SomethingBig"
    SOMETHING_SMALL = "SomethingSmall"
    SOMETHING_INBETWEEN_BIG_AND_SMALL = "SomethingInbetweenBigAndSmall"

    def getItem( name ):
        if name in NAMES:
            return self.__model__ + name
        return None

where we’ve defaulted to having the globals be copies of their contents.  Later on, we can change them.

So here’s to you, Pragmatic Programmers.  A simple script to automate my task:

def makeGlobal( myTuple ):
    return '_'.join( myTuple ).upper()

def makeName( myTuple ):
    return ''.join( ( item.capitalize() for item in myTuple ) )

def printProducts( names ):
    print "class Products(object):"
    for items in names:
        print ''.join( ( "    ", makeGlobal( items ), " = \"", makeName( items ), "\"" ) )
    print ''.join( ("    NAMES = [", ','.join( ( makeGlobal( item ) for item in names ) ), "]" ) )
    print "    def getName( name ):"
    print "        if name in NAMES:"
    print "            return self.__module__ + name"
    print "        return None"

There, done.  Now all I have to do is package the names of the variables as individual words for Pascal case capitalization.   Tomorrow I can copy and paste (with slight modifications).  Automating repetitive tasks, gotta love it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


This entry was posted on June 15, 2010 by in Pragmatic Programmer, Python.
%d bloggers like this: