diff --git a/PKG-INFO b/PKG-INFO index a3079ad..557319d 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -1,12 +1,12 @@ Metadata-Version: 1.0 Name: texttable -Version: 0.8.4 +Version: 0.8.5 Summary: module for creating simple ASCII tables Home-page: https://github.com/foutaise/texttable/ Author: Gerome Fournier Author-email: jef(at)foutaise.org License: LGPL -Download-URL: https://github.com/foutaise/texttable/archive/v0.8.4.tar.gz +Download-URL: https://github.com/foutaise/texttable/archive/v0.8.5.tar.gz Description: texttable is a module to generate a formatted text table, using ASCII characters. Platform: any diff --git a/README.md b/README.md index 0d9895e..07a8cc6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Python module for creating simple ASCII tables ## Availability -This module is available on [PypI](https://pypi.python.org/pypi/texttable/0.8.4), and has been packaged for several Linux/Unix platforms +This module is available on [PypI](https://pypi.python.org/pypi/texttable/0.8.5), and has been packaged for several Linux/Unix platforms ([Debian](https://packages.debian.org/search?&searchon=names&keywords=python-texttable+), [FreeBSD](https://www.freebsd.org/cgi/ports.cgi?query=texttable&stype=all), Fedora, Suse...). @@ -190,10 +190,10 @@ DATA __author__ = 'Gerome Fournier ' __credits__ = 'Jeff Kowalczyk:\n - textwrap improved import\n ...:\... __license__ = 'LGPL' - __version__ = '0.8.4' + __version__ = '0.8.5' VERSION - 0.8.4 + 0.8.5 AUTHOR Gerome Fournier diff --git a/setup.py b/setup.py index eb8c9e3..b5ed3ff 100644 --- a/setup.py +++ b/setup.py @@ -33,11 +33,11 @@ setup( name = "texttable", - version = "0.8.4", + version = "0.8.5", author = "Gerome Fournier", author_email = "jef(at)foutaise.org", url = "https://github.com/foutaise/texttable/", - download_url = "https://github.com/foutaise/texttable/archive/v0.8.4.tar.gz", + download_url = "https://github.com/foutaise/texttable/archive/v0.8.5.tar.gz", license = "LGPL", py_modules = ["texttable"], description = DESCRIPTION, diff --git a/texttable.py b/texttable.py index 775a43e..d6fdae5 100644 --- a/texttable.py +++ b/texttable.py @@ -25,14 +25,14 @@ table = Texttable() table.set_cols_align(["l", "r", "c"]) table.set_cols_valign(["t", "m", "b"]) - table.add_rows([["Name", "Age", "Nickname"], + table.add_rows([["Name", "Age", "Nickname"], ["Mr\\nXavier\\nHuon", 32, "Xav'"], ["Mr\\nBaptiste\\nClement", 1, "Baby"]]) print table.draw() + "\\n" table = Texttable() table.set_deco(Texttable.HEADER) - table.set_cols_dtype(['t', # text + table.set_cols_dtype(['t', # text 'f', # float (decimal) 'e', # float (exponent) 'i', # integer @@ -113,12 +113,30 @@ if sys.version >= '2.7': from functools import reduce +def obj2unicode(obj): + """Return a unicode representation of a python object + """ + try: + if sys.version >= '3.0': + return str(obj) + else: + if isinstance(obj, unicode): + return obj + else: + return str(obj).decode('utf') + except UnicodeDecodeError as strerror: + sys.stderr.write("UnicodeDecodeError exception for string '%s': %s\n" % (obj, strerror)) + if sys.version >= '3.0': + return str(obj, 'utf', 'replace') + else: + return str(obj).decode('utf', 'replace') + def len(iterable): """Redefining len here so it will be able to work with non-ASCII characters """ if not isinstance(iterable, str): return iterable.__len__() - + try: if sys.version >= '3.0': return len(str) @@ -303,7 +321,7 @@ def add_row(self, array): if not hasattr(self, "_dtype"): self._dtype = ["a"] * self._row_size - + cells = [] for i, x in enumerate(array): cells.append(self._str(i, x)) @@ -318,7 +336,7 @@ def add_rows(self, rows, header=True): of the table """ - # nb: don't use 'iter' on by-dimensional arrays, to get a + # nb: don't use 'iter' on by-dimensional arrays, to get a # usable code for python 2.1 if header: if hasattr(rows, '__iter__') and hasattr(rows, 'next'): @@ -359,13 +377,13 @@ def draw(self): def _str(self, i, x): """Handles string formatting of cell data - i - index of the cell datatype in self._dtype + i - index of the cell datatype in self._dtype x - cell data to format """ try: f = float(x) except: - return str(x) + return obj2unicode(x) n = self._precision dtype = self._dtype[i] @@ -377,7 +395,7 @@ def _str(self, i, x): elif dtype == 'e': return '%.*e' % (n, f) elif dtype == 't': - return str(x) + return obj2unicode(x) else: if f - round(f) == 0: if abs(f) > 1e8: @@ -554,17 +572,6 @@ def _splitit(self, line, isheader): for cell, width in zip(line, self._width): array = [] for c in cell.split('\n'): - try: - if sys.version >= '3.0': - c = str(c) - else: - c = unicode(c, 'utf') - except UnicodeDecodeError as strerror: - sys.stderr.write("UnicodeDecodeError exception for string '%s': %s\n" % (c, strerror)) - if sys.version >= '3.0': - c = str(c, 'utf', 'replace') - else: - c = unicode(c, 'utf', 'replace') array.extend(textwrap.wrap(c, width)) line_wrapped.append(array) max_cell_lines = reduce(max, list(map(len, line_wrapped))) @@ -593,7 +600,7 @@ def _splitit(self, line, isheader): table = Texttable() table.set_deco(Texttable.HEADER) - table.set_cols_dtype(['t', # text + table.set_cols_dtype(['t', # text 'f', # float (decimal) 'e', # float (exponent) 'i', # integer