summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2012-05-18 02:46:06 +0000
committerluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2012-05-18 02:46:06 +0000
commitffda96106ca1af92121d479c9105eb1ca176a69c (patch)
treead1e98a068c723875da4c100dbda3d1e6dc726a9
parentbde568227827f24fc11193a3a14ff70c90038f26 (diff)
downloadpython-prettytable-ptable-ffda96106ca1af92121d479c9105eb1ca176a69c.tar.gz
1) Fixed a bug regarding validation of max_width, on the basis of an email from Anthony Toole. Also made some other changes to validation code and unit testing on the basis of this bug.
2) Repaired Python 3 compatibility, which the switch to StringIO had broken.
-rw-r--r--CHANGELOG5
-rw-r--r--prettytable.py11
-rw-r--r--prettytable_test.py32
3 files changed, 43 insertions, 5 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 541b57a..4780a3f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+########## PrettyTable 0.7 - MM DD, 2012 ##########
+
+* Fixed a simple bug regarding validation of max_width (thanks to
+ Anthony Toole for pointing out this bug and providing a patch).
+
########## PrettyTable 0.6 - April XX, 2012 ##########
* Code is now simultaneously compatible with Python 2 and 3
diff --git a/prettytable.py b/prettytable.py
index 04974fd..ee85ba4 100644
--- a/prettytable.py
+++ b/prettytable.py
@@ -32,7 +32,6 @@
__version__ = "TRUNK"
import copy
-import cStringIO
import random
import sys
import textwrap
@@ -42,8 +41,10 @@ if py3k:
unicode = str
basestring = str
from html import escape
+ from io import StringIO
else:
from cgi import escape
+ from cStringIO import StringIO
# hrule styles
FRAME = 0
@@ -195,7 +196,7 @@ class PrettyTable(object):
# Secondly, in the _get_options method, where keyword arguments are mixed with persistent settings
def _validate_option(self, option, val):
- if option in ("start", "end", "padding_width", "left_padding_width", "right_padding_width", "format"):
+ if option in ("start", "end", "max_width", "padding_width", "left_padding_width", "right_padding_width", "format"):
self._validate_nonnegative_int(option, val)
elif option in ("sortby"):
self._validate_field_name(option, val)
@@ -331,7 +332,7 @@ class PrettyTable(object):
def _get_max_width(self):
return self._max_width
def _set_max_width(self, val):
- self._validate_nonnegativeint(val)
+ self._validate_option("max_width", val)
for field in self._field_names:
self._max_width[field] = val
max_width = property(_get_max_width, _set_max_width)
@@ -549,7 +550,7 @@ class PrettyTable(object):
attributes - dictionary of attributes"""
return self._attributes
def _set_attributes(self, val):
- self.validate_option("attributes", val)
+ self._validate_option("attributes", val)
self._attributes = val
attributes = property(_get_attributes, _set_attributes)
@@ -786,7 +787,7 @@ class PrettyTable(object):
options = self._get_options(kwargs)
- string = cStringIO.StringIO()
+ string = StringIO()
# Don't think too hard about an empty table
if self.rowcount == 0:
diff --git a/prettytable_test.py b/prettytable_test.py
index d53c1ca..a3adc4d 100644
--- a/prettytable_test.py
+++ b/prettytable_test.py
@@ -121,6 +121,38 @@ class OptionOverrideTests(CityDataTest):
override = self.x.get_string(hrules=NONE)
self.assertTrue(default != override)
+class OptionAttributeTests(CityDataTest):
+
+ """Make sure all options which have an attribute interface work as they should."""
+
+ def testSetForAllColumns(self):
+ self.x.field_names = sorted(self.x.field_names)
+ self.x.align = "l"
+ self.x.max_width = 10
+ self.x.start = 2
+ self.x.end = 4
+ self.x.sortby = "Area"
+ self.x.reversesort = True
+ self.x.header = True
+ self.x.border = False
+ self.x.hrule = True
+ self.x.int_format = "4"
+ self.x.float_format = "2.2"
+ self.x.padding_width = 2
+ self.x.left_padding_width = 2
+ self.x.right_padding_width = 2
+ self.x.vertical_char = "!"
+ self.x.horizontal_char = "~"
+ self.x.junction_char = "*"
+ self.x.format = True
+ self.x.attributes = {"class" : "prettytable"}
+
+ def testSetForOneColumn(self):
+ self.x.align["Rainfall"] = "l"
+ self.x.max_width["Name"] = 10
+ self.x.int_format["Population"] = "4"
+ self.x.float_format["Area"] = "2.2"
+
class BasicTests(CityDataTest):
"""Some very basic tests."""