Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Decimal Type Rendering
Browse files Browse the repository at this point in the history
Updates the render encoding across all JSON
serialization sources to include support for
rendering decimals to JSON-compatible floats.
  • Loading branch information
sernst committed Sep 11, 2020
1 parent c5feed9 commit d8d7d33
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cauldron/render/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import numpy as np
import pandas as pd
import decimal
import typing
from collections import namedtuple

Expand All @@ -14,6 +15,8 @@
def default_override(value: typing.Any):
"""..."""

if isinstance(value, decimal.Decimal):
return float(value)
if isinstance(value, datetime.date):
return value.isoformat()
elif isinstance(value, datetime.datetime):
Expand Down
2 changes: 1 addition & 1 deletion cauldron/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "1.0.3",
"version": "1.0.4",
"notebookVersion": "v1"
}
12 changes: 7 additions & 5 deletions cauldron/test/render/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from unittest.mock import patch
from datetime import date
import decimal

import pandas as pd
from cauldron import render
Expand All @@ -13,12 +14,13 @@ class TestRender(scaffolds.ResultsTest):

def test_table(self):
"""Should render a table"""

dt = date(2016, 9, 9)
d = decimal.Decimal('3.14')
df = pd.DataFrame([
{'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)},
{'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)},
{'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)},
{'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}
{'a': 1, 'b': 'hello', 'c': True, 'd': dt, 'e': d},
{'a': 1, 'b': 'hello', 'c': True, 'd': dt, 'e': d},
{'a': 1, 'b': 'hello', 'c': True, 'd': dt, 'e': d},
{'a': 1, 'b': 'hello', 'c': True, 'd': dt, 'e': d}
])

result = render.table(df, 0.5, include_index=True)
Expand Down
2 changes: 1 addition & 1 deletion cauldron/test/support/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def __enter__(self):
return self.mock_importer

def __exit__(self, *args, **kwargs):
return self._patch.__exit__()
return self._patch.__exit__(*args)
5 changes: 5 additions & 0 deletions development.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.8

COPY requirements.txt /build-data/requirements.txt

RUN pip install -r /build-data/requirements.txt --upgrade

0 comments on commit d8d7d33

Please sign in to comment.