I’ve been messing around with Python for the past 6 months and I’m loving it. Today I went to my second UtahPython users group meeting and had a lot of fun and learned a ton of stuff.

Chronological order of things I learned:

  • Supy bot - an IRC bot written in Python.
  • supybot-doxygen - A plugin for supy bot that can provide api documentation for any software that uses doxygen.
    • This could be really useful at work if I can setup an internal IRC server for the developers to hang out.
  • Objectify is a module in python for parsing XML files.
  • doctest - a python module for TDD that is super simple. I’m really excited about this. Thanks to Matt for showing me how to use this.

Matt suggested that we do some pair programming during the meetup.
Here’s the task: Write a simple python program that can take page numbers as user input and convert it to a list of numbers. 

User Input: 0, 1, 5, 7-10
Output: 0,1,5,7,8,9,10

Here is the code I wrote with the doc test based unit test in the doc-string:

###### PrintParser.py #######
    
    #!/usr/bin/env python

    def convert(inp):
        """
 \* Get the input from user.
 \* Parse the input to extract numbers
 \*\* Split by comma
 \*\*\* Each item in the list will then be split by '-'
 \*\*\*\* Populate the number between a-b using range(a,b)

 >>> convert("")
 []
 >>> convert("1")
 [1]
 >>> convert("1,2")
 [1, 2]
 >>> convert("1,2-5")
 [1, 2, 3, 4, 5]
 >>> convert("1-3,2-5,8,10,15-20")
 [1, 2, 3, 2, 3, 4, 5, 8, 10, 15, 16, 17, 18, 19, 20]
 """
        if not inp:
            return []
        pages = []
        comma_separated = []
        comma_separated = inp.split(",")
        for item in comma_separated:
            if "-" in item:
                a = item.split("-")
                pages.extend(range(int(a[0]),int(a[1])+1))
            else:
                pages.append(int(item))

        return pages

    if __name__ == '\_\_main\_\_' :
        import doctest
        doctest.testmod()