jdunck / google-visualization-python

Automatically exported from code.google.com/p/google-visualization-python
Apache License 2.0
0 stars 1 forks source link

Add support for decimal.Decimal #20

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What
Allow the developer to add not only int, long, float values but also 
decimal.Decimal values.

Why
When writing your own data source, you often pull data from a MySQL database. 
Any aggregation in the database (like AVG() or SUM()) returns a 
decimal.Decimal. It's bothersome to have to cast to int, long, or float and you 
have to take care of None values separately. It would be better if you could 
add any numbers from the database to a DataTable without having to know what 
SQL generated it.

Original issue reported on code.google.com by momander@google.com on 18 Apr 2012 at 6:46

GoogleCodeExporter commented 9 years ago
Today I faced same issue. Since we are not supporting decimal.Decimal type I 
got "DataTableException: Wrong type <class 'decimal.Decimal'> when expected 
number" exception. Without it we need write complex SQLs.

Original comment by vivektm...@gmail.com on 3 May 2013 at 10:36

GoogleCodeExporter commented 9 years ago
I haven't figured out how to work around this situation yet, either.
Any suggestions?  I've been trying to convert these data items, but they are in 
tuples, and I'm not a strong python person. This has been burdensome.

Original comment by jagauth...@gmail.com on 8 May 2013 at 7:54

GoogleCodeExporter commented 9 years ago
I found a very simple solution
Make sure you 'import decimal'.

    for row in cursor:  
        rowlist = list(row)
        for idx, item in enumerate(rowlist):
            if (type(rowlist[idx]) is decimal.Decimal):
                rowlist[idx] = int(rowlist[idx])

        data.append(rowlist)

This will convert row into a list, then convert the decimal.Decimal to an 
integer and add it to the data row.  This, assumes, you are using a JSON table.

Original comment by jagauth...@gmail.com on 8 May 2013 at 8:31

GoogleCodeExporter commented 9 years ago
It doesn't seem that this project supports any contributions to the source, but 
I made the following change in gviz_api.py that fixes the issue: 

On line 235, change: 

   elif value_type == "number":
      if isinstance(value, (int, long, float)):
        return value
        raise DataTableException("Wrong type %s when expected number" % t_value)

To:

   elif value_type == "number":
      if isinstance(value, (int, long, float)):
        return value
      else:
        try: 
            return float(value)
        except ValueError:
            raise DataTableException("Wrong type %s when expected number" % t_value)

What it does: if value type is set to number, and the type doesn't match int, 
long, or float, then it tries to coerce the value to a float. This will convert 
decimal values to a float. If the conversion fails, then it raises the original 
exception.

I've attached a modified version of the file.

Original comment by nrhin...@gmail.com on 24 Feb 2014 at 7:51

Attachments: