[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnue] r7874 - in trunk/gnue-common: src/utils tests
From: |
jamest |
Subject: |
[gnue] r7874 - in trunk/gnue-common: src/utils tests |
Date: |
Tue, 23 Aug 2005 14:32:55 -0500 (CDT) |
Author: jamest
Date: 2005-08-23 14:32:54 -0500 (Tue, 23 Aug 2005)
New Revision: 7874
Modified:
trunk/gnue-common/src/utils/TextUtils.py
trunk/gnue-common/tests/utils_TextUtils.py
Log:
added stripHTML function to TextUtils
more unit tests for TextUtils
misc cleanup
Modified: trunk/gnue-common/src/utils/TextUtils.py
===================================================================
--- trunk/gnue-common/src/utils/TextUtils.py 2005-08-22 14:31:33 UTC (rev
7873)
+++ trunk/gnue-common/src/utils/TextUtils.py 2005-08-23 19:32:54 UTC (rev
7874)
@@ -21,20 +21,21 @@
# FILE:
# TextUtils.py
#
-# DESCRIPTION:
-# Common text-related utilities
-#
# NOTES:
-#
+"""
+Common text-related utilities
+"""
+__revision__ = "$Id"
-import string
+import formatter, htmllib, StringIO
-ALIGN_LEFT=0
-ALIGN_RIGHT=1
-ALIGN_CENTER=2
+ALIGN_LEFT = 0
+ALIGN_RIGHT = 1
+ALIGN_CENTER = 2
# very simple lineWrap
-def lineWrap(message,maxWidth, preserveNewlines=1, alignment=ALIGN_LEFT,
eol=1):
+def lineWrap(message, maxWidth, preserveNewlines=1,
+ alignment=ALIGN_LEFT, eol=1):
"""
A simple linewrap function.
@@ -56,21 +57,21 @@
text = ""
- temptext = string.strip(str(message))
+ temptext = str(message).strip()
if preserveNewlines:
- buff = string.split(temptext,"\n")
+ buff = temptext.split("\n")
else:
- buff = (temptext,)
+ buff = (temptext, )
for strings in buff:
while len(strings) > maxWidth:
index = 0
- for sep in [' ',',',':','.',';']:
- ind = string.rfind(strings,sep,0,maxWidth-1)+1
- if ind > index: index = ind
+ for sep in [' ', ',', ':', '.', ';']:
+ ind = strings.rfind(sep, 0, maxWidth-1)+1
+ index = max(ind, index)
- if index > maxWidth or index==0:
+ if index > maxWidth or index == 0:
index = maxWidth-1
line = strings[:index]
@@ -117,7 +118,7 @@
@return: The converted value
@rtype: number
"""
- if text[-1] in ('0','1','2','3','4','5','6','7','8','9'):
+ if text[-1].isdigit():
value = float(text) / 72 * multiplier
else:
unit = text[-2:]
@@ -143,18 +144,18 @@
@return: The roman numeral equivalent.
@rtype: string
"""
- n = int(num) # just in case
- rv = ""
- for dec, rom in (
+ number = int(num) # just in case
+ output = ""
+ for dec, roman in (
(1000, 'M'), (900, 'CM', ),
(500, 'D'), (400, 'CD'), (100, 'C'),
(90, 'XC'),(50, 'L'), (40, 'XL'),
(10, 'X'), (9, 'IX'), (5, 'V'),
(4, 'IV'), (1, 'I') ):
- while (n >= dec):
- n -= dec;
- rv += rom;
- return rv
+ while (number >= dec):
+ number -= dec;
+ output += roman;
+ return output
def dollarToText(num):
@@ -175,24 +176,25 @@
"""
whole = int(num)
cents = round((num-whole)*100)
- rv = 'and %02d/100' % cents
+ output = 'and %02d/100' % cents
if whole:
thirdRange = 0
while whole:
whole, segment = divmod(whole, 1000)
hundreds, tens = divmod(segment, 100)
try:
- rv = _smallDollarMap[tens] + _thirdDollarMap[thirdRange] + rv
+ output = _smallDollarMap[tens] + _thirdDollarMap[thirdRange] + output
except IndexError:
- ten, ones = divmod(tens,10)
- rv = _tenDollarMap[ten] + _smallDollarMap[ones] +
_thirdDollarMap[thirdRange] + rv
+ ten, ones = divmod(tens, 10)
+ output = _tenDollarMap[ten] + _smallDollarMap[ones] + \
+ _thirdDollarMap[thirdRange] + output
if hundreds:
- rv = _smallDollarMap[hundreds] + 'Hundred ' + rv
+ output = _smallDollarMap[hundreds] + 'Hundred ' + output
thirdRange += 1
else:
- rv = 'Zero ' + rv
+ output = 'Zero ' + output
- return rv
+ return output
_smallDollarMap = ('', 'One ', 'Two ', 'Three ', 'Four ', 'Five ',
@@ -207,7 +209,7 @@
# Comify a number
-def comify(num, decimals=2, parenthesis=0):
+def comify(num, decimals=2, parenthesis=False):
"""
Comify a number (e.g., print -9900 as -9,900.00)
@@ -223,34 +225,81 @@
@param decimals: The number of decimal places to retain
@type decimals: number
@param parenthesis: If true then negative numbers will be returned inside
parenthesis
- @type parenthesis: number
+ @type parenthesis: boolean
@return: A properly formatted number
@rtype: string
"""
neg = num < 0
num = abs(num)
- whole, dec = (string.split(string.strip(("%%12.%sf" % decimals) %
abs(num)),'.') + [""])[:2]
+ whole, dec = ( ( ( ("%%12.%sf" % decimals) % abs(num)).strip()).split('.') +
+ [""])[:2]
if len(dec):
dec = "." + dec
- s = ""
+ output = ""
- for i in range(divmod(len(whole),3)[0]+1):
+ for i in range(divmod(len(whole), 3)[0]+1):
j = len(whole) - i*3
- s = "," + whole[j > 3 and j-3 or 0:j] + s
+ output = "," + whole[j > 3 and j-3 or 0:j] + output
- s += dec
+ output += dec
- while s[:1] == ',':
- s = s[1:]
+ while output[:1] == ',':
+ output = output[1:]
if neg:
if parenthesis:
- s = "(%s)" % s
+ output = "(%s)" % output
else:
- s = "-" + s
+ output = "-" + output
elif parenthesis:
- s += " "
+ output += " "
- return s
+ return output
+
+
+def stripHTML(htmlString):
+ """
+ Removes all html mark-up from a text string.
+
+ @param htmlString: The text containing the html mark up
+ @type htmlString: String
+ @return: The string minus any html markup
+ @rtype: String
+ """
+ # This is based upon code found at
+ # http://online.effbot.org/2003_08_01_archive.htm#20030811
+ #
+ # It looks as if the original code is in the public domain
+ # per http://effbot.org/zone/copyright.htm
+ #
+ class Parser(htmllib.HTMLParser):
+ """
+ Private class for strip HTML
+ """
+ def anchor_end(self):
+ self.anchor = None
+
+ class Formatter(formatter.AbstractFormatter):
+ """
+ Private class for strip HTML
+ """
+ pass
+
+ class Writer(formatter.DumbWriter):
+ """
+ Private class for strip HTML
+ """
+ def send_label_data(self, data):
+ """
+ """
+ self.send_flowing_data(data)
+ self.send_flowing_data(" ")
+
+ output = StringIO.StringIO()
+ parser = Parser(Formatter(Writer(output)))
+ parser.feed(htmlString)
+ parser.close()
+
+ return output.getvalue()
Modified: trunk/gnue-common/tests/utils_TextUtils.py
===================================================================
--- trunk/gnue-common/tests/utils_TextUtils.py 2005-08-22 14:31:33 UTC (rev
7873)
+++ trunk/gnue-common/tests/utils_TextUtils.py 2005-08-23 19:32:54 UTC (rev
7874)
@@ -34,7 +34,7 @@
def setUp(self):
self.shortText="Hello"
self.longText="This is a longer line used to test the line wrap. This is
only a test"
-
+
def testLineWrap(self):
"""Check that the basic line wrap function properly wraps lines"""
output = lineWrap(self.longText,27)
@@ -48,7 +48,30 @@
assert lineWrap(self.longText, 27, alignment=ALIGN_RIGHT) ==
expectedResultRight, 'right alignment incorrect'
assert lineWrap(self.longText, 27, alignment=ALIGN_CENTER) ==
expectedResultCenter, 'center alignment incorrect'
+
+
+ def testIntToRoman(self):
+ assert intToRoman(1999) == 'MCMXCIX', 'Roman numeral conversion failed'
+ assert intToRoman(2005) == 'MMV', 'Roman numeral conversion failed'
+ def testComify(self):
+ """Check comify function"""
+
+ assert comify(9999.25) == '9,999.25', 'comify failure'
+ assert comify(-9999.934, 2) == '-9,999.93', 'decimal truncation failure'
+ assert comify(-9999.934, 2, 1) == '(9,999.93)', 'parenthesis failure'
+
+
+ def testStripHtml(self):
+ """Verify that stripHTML works"""
+ htmlText="""<body>
+<p>This is a test</p>
+</body>"""
+
+ expectedText="""
+This is a test"""
+ assert stripHTML(htmlText) == expectedText, 'stripHTML failure'
+
def suite():
suite = unittest.makeSuite(TextTestCase,'test')
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnue] r7874 - in trunk/gnue-common: src/utils tests,
jamest <=