Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

TableForm: add alignment="center" support

  Also, <, ^, > are aliases for left, center and right alignment

  Tableform no longer adds a space to the end of lines and the
  string handling is a little more efficient.
  • Loading branch information...
commit 40b6e337e854e2a362161268cf68c033485b62ef 1 parent c6c4708
@smichr smichr authored committed
Showing with 17 additions and 10 deletions.
  1. +17 −10 sympy/core/containers.py
View
27 sympy/core/containers.py
@@ -231,7 +231,10 @@ def __new__(cls, data, headings=None, alignment="left",
"automatic" ... gives successive integer labels
[[l1, l2, ...], ...] gives labels for each entry in
each dimension (can be None for some dimension)
- alignment ... "left", "center", "right" (alignment of the columns)
+ alignment ... alignment of the columns controlled by values;
+ "left" or "<"
+ "center" or "^"
+ "right" or ">"
Example:
@@ -259,7 +262,7 @@ def __new__(cls, data, headings=None, alignment="left",
h2 = range(1, _w + 1)
_headings = [h1, h2]
- _alignment = alignment
+ _alignment = {'<': 'left', '>': 'right', '^': 'center'}.get(alignment, alignment)
if column_formats:
_column_formats = column_formats
else:
@@ -310,15 +313,18 @@ def as_str(self):
new_line.append(s)
self._headings[1] = new_line
- format_str = ""
+ format_str = []
for w in column_widths:
if self._alignment == "left":
align = "-"
elif self._alignment == "right":
align = ""
+ elif self._alignment == "center":
+ align = ""
else:
raise NotImplementedError()
- format_str += "%" + align + str(w) + "s "
+ format_str += ["%" + align + str(w) + "s"]
+ format_str = ' '.join(format_str)
format_str += "\n"
if self._headings[0]:
@@ -326,20 +332,21 @@ def as_str(self):
heading_width = max([len(x) for x in self._headings[0]])
format_str = "%" + str(heading_width) + "s | " + format_str
- s = ""
+ s = []
if self._headings[1]:
d = self._headings[1]
if self._headings[0]:
d = [""] + d
first_line = format_str % tuple(d)
- s += first_line
- s += "-" * (len(first_line) - 2) + "\n"
+ s.append(first_line)
+ s.append("-" * (len(first_line) - 2) + "\n")
for i, line in enumerate(lines):
- d = line
+ d = [l if self._alignment != 'center' else
+ l.center(column_widths[j]) for j,l in enumerate(line)]
if self._headings[0]:
d = [self._headings[0][i]] + d
- s += format_str % tuple(d)
- return s
+ s.append(format_str % tuple(d))
+ return ''.join(s)
def as_latex(self):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.