From 8dac223555437730beb8b3b9690ebbe93bf0019d Mon Sep 17 00:00:00 2001 From: Paul Schoenfelder Date: Wed, 3 May 2017 14:09:23 -0400 Subject: [PATCH 1/3] "align" kwarg allowed in init --- prettytable/prettytable.py | 46 +++++++++++++++++++++++++------------- tests/test_prettytable.py | 16 +++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index 1f9e3a1..57a9165 100644 --- a/prettytable/prettytable.py +++ b/prettytable/prettytable.py @@ -33,6 +33,16 @@ def _get_size(text): class PrettyTable(object): + OPTIONS = [ + 'align', 'attributes', 'border', 'end', 'fields', 'float_format', + 'format', 'header', 'header_style', 'horizontal_char', 'hrules', + 'int_format', 'junction_char', 'left_padding_width', + 'max_table_width', 'max_width', 'min_table_width', 'min_width', + 'oldsortslice', 'padding_width', 'print_empty', 'reversesort', + 'right_padding_width', 'sort_key', 'sortby', 'start', 'title', + 'valign', 'vertical_char', 'vrules', 'xhtml' + ] + def __init__(self, field_names=None, **kwargs): """Return a new PrettyTable instance @@ -62,13 +72,16 @@ def __init__(self, field_names=None, **kwargs): junction_char - single character string used to draw line junctions sortby - name of field to sort rows by sort_key - sorting key function, applied to data points before sorting + align - default align for each row valign - default valign for each row (None, "t", "m" or "b") reversesort - True or False to sort in descending or ascending order oldsortslice - Slice rows before sorting in the "old style" """ self.encoding = kwargs.get("encoding", "UTF-8") + self._default_align = kwargs.get("align", "c") - # Data + # Some attributes must be set before argument validation occurs because + # some validation uses these fields. self._field_names = [] self._rows = [] self.align = {} @@ -82,14 +95,8 @@ def __init__(self, field_names=None, **kwargs): else: self._widths = [] - # Options - self._options = "title start end fields header border sortby reversesort sort_key attributes format hrules vrules".split() - self._options.extend( - "int_format float_format min_table_width max_table_width padding_width left_padding_width right_padding_width".split()) - self._options.extend( - "vertical_char horizontal_char junction_char header_style valign xhtml print_empty oldsortslice".split()) - self._options.extend("align valign max_width min_width".split()) - for option in self._options: + # Option validation + for option in PrettyTable.OPTIONS: if option in kwargs: self._validate_option(option, kwargs[option]) else: @@ -198,9 +205,10 @@ def __getitem__(self, index): new = PrettyTable() new.field_names = self.field_names - for attr in self._options: + for attr in PrettyTable.OPTIONS: setattr(new, "_" + attr, getattr(self, "_" + attr)) setattr(new, "_align", getattr(self, "_align")) + setattr(new, "_default_align", getattr(self, "_default_align")) if isinstance(index, slice): for row in self._rows[index]: new.add_row(row) @@ -259,6 +267,10 @@ def _validate_option(self, option, val): self._validate_single_char(option, val) elif option in ("attributes"): self._validate_attributes(option, val) + elif option in ("align"): + self._validate_align(val) + elif option in ("valign"): + self._validate_valign(val) def _validate_field_names(self, val): # Check for appropriate length @@ -399,7 +411,7 @@ def field_names(self, val): if old_name not in self._align: self._align.pop(old_name) else: - self.align = "c" + self.align = self._default_align if self._valign and old_names: for old_name, new_name in zip(old_names, val): self._valign[new_name] = self._valign[old_name] @@ -423,7 +435,7 @@ def align(self, val): self._align = {} elif val is None or (isinstance(val, dict) and len(val) is 0): for field in self._field_names: - self._align[field] = "c" + self._align[field] = self._default_align else: self._validate_align(val) for field in self._field_names: @@ -857,7 +869,7 @@ def oldsortslice(self, val): def _get_options(self, kwargs): options = {} - for option in self._options: + for option in PrettyTable.OPTIONS: if option in kwargs: self._validate_option(option, kwargs[option]) options[option] = kwargs[option] @@ -958,7 +970,7 @@ def del_row(self, row_index): raise Exception("Cant delete row at index %d, table only has %d rows!" % (row_index, len(self._rows))) del self._rows[row_index] - def add_column(self, fieldname, column, align="c", valign="t"): + def add_column(self, fieldname, column, align=None, valign="t"): """Add a column to the table. @@ -971,7 +983,11 @@ def add_column(self, fieldname, column, align="c", valign="t"): valign - desired vertical alignment for new columns - "t" for top, "m" for middle and "b" for bottom""" if len(self._rows) in (0, len(column)): - self._validate_align(align) + if align is None: + align = self._default_align + else: + self._validate_align(align) + self._validate_valign(valign) self._field_names.append(fieldname) self._align[fieldname] = align diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 6d8c573..4752063 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -727,5 +727,21 @@ def testBordered(self): """.strip()) +class InitialOptionsPreservationTest(unittest.TestCase): + def setUp(self): + self.x = PrettyTable(align='l', header=False, padding_width=0) + self.x.add_row(["abc", "def", "ghi"]) + self.x.add_row("jkl") + + def testBordered(self): + self.x.border = True + result = self.x.get_string() + self.assertEqual(result.strip(), """ ++---+---+---+ +|abc|def|ghi| +|j |k |l | ++---+---+---+ +""".strip()) + if __name__ == "__main__": unittest.main() From b238e59b928cf5218864573344ea7d7764d0b881 Mon Sep 17 00:00:00 2001 From: Paul Schoenfelder Date: Wed, 3 May 2017 14:21:38 -0400 Subject: [PATCH 2/3] Self CR --- prettytable/prettytable.py | 3 +-- tests/test_prettytable.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index 57a9165..0f5ecda 100644 --- a/prettytable/prettytable.py +++ b/prettytable/prettytable.py @@ -72,7 +72,7 @@ def __init__(self, field_names=None, **kwargs): junction_char - single character string used to draw line junctions sortby - name of field to sort rows by sort_key - sorting key function, applied to data points before sorting - align - default align for each row + align - default align for each row (None, "l", "c", "r") valign - default valign for each row (None, "t", "m" or "b") reversesort - True or False to sort in descending or ascending order oldsortslice - Slice rows before sorting in the "old style" """ @@ -208,7 +208,6 @@ def __getitem__(self, index): for attr in PrettyTable.OPTIONS: setattr(new, "_" + attr, getattr(self, "_" + attr)) setattr(new, "_align", getattr(self, "_align")) - setattr(new, "_default_align", getattr(self, "_default_align")) if isinstance(index, slice): for row in self._rows[index]: new.add_row(row) diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 4752063..de7851c 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -733,8 +733,7 @@ def setUp(self): self.x.add_row(["abc", "def", "ghi"]) self.x.add_row("jkl") - def testBordered(self): - self.x.border = True + def testAlign(self): result = self.x.get_string() self.assertEqual(result.strip(), """ +---+---+---+ From fdf3226ecd3d5dd3f526fb25491dccdc17604b0c Mon Sep 17 00:00:00 2001 From: Paul Schoenfelder Date: Wed, 3 May 2017 14:25:06 -0400 Subject: [PATCH 3/3] Additional docstring info --- prettytable/prettytable.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index 0f5ecda..108296d 100644 --- a/prettytable/prettytable.py +++ b/prettytable/prettytable.py @@ -72,7 +72,7 @@ def __init__(self, field_names=None, **kwargs): junction_char - single character string used to draw line junctions sortby - name of field to sort rows by sort_key - sorting key function, applied to data points before sorting - align - default align for each row (None, "l", "c", "r") + align - default align for each column (None, "l", "c", or "r"). If None, defaults to "c". valign - default valign for each row (None, "t", "m" or "b") reversesort - True or False to sort in descending or ascending order oldsortslice - Slice rows before sorting in the "old style" """ @@ -80,8 +80,7 @@ def __init__(self, field_names=None, **kwargs): self.encoding = kwargs.get("encoding", "UTF-8") self._default_align = kwargs.get("align", "c") - # Some attributes must be set before argument validation occurs because - # some validation uses these fields. + # Data self._field_names = [] self._rows = [] self.align = {}