Mike takes to heart the adage that ideas are f***ing worthless without action.

Monday, July 27, 2009

getting back into it... day 3

Today I decided I needed a debugger... since I'm not really up on the syntax of python (lists look like tuples look like arrays to me)... I was really lost... it took about an hour to digging around to decide on PyScripter to be my IDE. I don't want to get tied into a product I can't afford, so I took a pass on Komodo from ActiveState.

I've managed to finally deduce what I was doing wrong, and pass one of my trivial unit tests... the code now looks like this:

"""Translates internal representation to and from xhtml"""

#define Exceptions

class HtmlError(Exception): pass
class invalidHtmlError(HtmlError): pass

#Define escape mapping
htmlCharMap = [('>', ">"),
('<', "<")]

def toHtml(x):
"""convert x to html string"""
return x

def fromHtml(s):
"""convert html string to internal representation"""

result = ''
index = 0
while index < len(s):
foundat = -1
i = 0
for ascii,html in htmlCharMap:
if (foundat < 0) & (s[index:index+len(html)] == html):
foundat = i
i += 1
if foundat >= 0:
ascii, html = htmlCharMap[foundat]
result += ascii
index += len(html)
result += s[index]
index += 1
return result

"""Unit test for translate01.py"""

import translate01
import unittest

class KnownValues(unittest.TestCase):
knownValues = ( ( '', ''),
( 'x','x'),
( '>','>'),
( '<','<'),
( 'this is a test','this is a test') )

def testToHtmlKnownValues(self):
"""ToHtml should give known results with known input"""
for s1,s2 in self.knownValues:
result = translate01.toHtml(s1)

def testFromHtmlKnownValues(self):
"""FromHtml should give known results with known input"""
for s1,s2 in self.knownValues:
result = translate01.fromHtml(s2)

if __name__ == "__main__":

No comments:

About Me

My photo
Munster, Indiana, United States
I fix things, I take pictures, I write, and marvel at the joy of life. I'm trying to leave the world in better condition than when I found it.