tienbm90 / prettytable

Automatically exported from code.google.com/p/prettytable
Other
0 stars 0 forks source link

Setting float_format in **kwargs does not actually affect output - confusing behavior #55

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create a new table, send it the **kwargs value of float_format:
>>> table = new PrettyTable(['rowA','rowB'],float_format='.3')
2. Add a row of floating point data:
>>> table.add_row([0.012345,1.23456])
3. Print the row:
>>> print table.get_string()

What is the expected output? What do you see instead?

We expect output to be formatted according to the float_format string sent in 
object creation, but instead it isn't. Expected:

>>> print table.get_string()
+-------+-------+
|  rowA |  rowB |
+-------+-------+
| 0.012 | 1.234 |
+-------+-------+

Seen:

>>> print table.get_string()
+----------+--------+
|   rowA   |  rowB  |
+----------+--------+
| 0.012345 | 1.2345 |
+----------+--------+

The way to achieve the desired behavior is to create the table with only the 
row names (float_format can NOT be given as a **kwargs - this prevents future 
float_format assignment), and then access the float_format property, ie:

>>> table = PrettyTable(['rowA','rowB'])
>>> table.float_format='.3'

What version of the product are you using? On what operating system?

0.7.2

Please provide any additional information below.

Simple command line issue shown below. Notice how trying to set 
table.float_format after already sending float_format as a **kwargs leads to an 
error.

I believe an easy fix would be to change line 160 from:

self._float_format = kwargs["float_format"] or {}

to:

self._set_float_format(kwargs["float_format"]) if kwargs["float_format"] else 
self._float_format = {}

or alternatively, to disallow float_format being sent as a kwargs.

Output of simple test follows:

=====================================

>>> from prettytable import PrettyTable
>>> table = PrettyTable(['rowA','rowB'],float_format='.3')
>>> table.add_row([0.012345,1.2345])
>>> print table.get_string()
+----------+--------+
|   rowA   |  rowB  |
+----------+--------+
| 0.012345 | 1.2345 |
+----------+--------+
>>> table.float_format='.3'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/eng/tools/current/lib/python2.7/dist-packages/prettytable.py", line 614, in _set_float_format
    self._float_format[field] = val
TypeError: 'str' object does not support item assignment
>>> table = PrettyTable(['rowA','rowB'])
>>> table.float_format='.3'
>>> table.add_row([0.012345,1.2345])
>>> print table.get_string()
+-------+-------+
|  rowA |  rowB |
+-------+-------+
| 0.012 | 1.234 |
+-------+-------+
>>>

Original issue reported on code.google.com by john.fil...@gmail.com on 25 Jun 2014 at 6:50

GoogleCodeExporter commented 8 years ago
Fixed this issue, merged into /trunk.

Original comment by john.fil...@gmail.com on 6 Jul 2014 at 9:18