Skip to content

Commit

Permalink
Merge d9976e6 into 143ce2b
Browse files Browse the repository at this point in the history
  • Loading branch information
twheys committed Aug 8, 2018
2 parents 143ce2b + d9976e6 commit 256e68d
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 415 deletions.
21 changes: 9 additions & 12 deletions fireant/formats.py
@@ -1,13 +1,11 @@
import locale
import numpy as np
import pandas as pd
from datetime import (
date,
datetime,
time,
)

import numpy as np
import pandas as pd

INFINITY = "Infinity"
NULL_VALUE = 'null'
TOTALS_VALUE = 'totals'
Expand Down Expand Up @@ -116,23 +114,22 @@ def metric_display(value, prefix=None, suffix=None, precision=None):
if pd.isnull(value) or value in {np.inf, -np.inf}:
return ''

if isinstance(value, bool):
value = str(value).lower()

if isinstance(value, float):
if precision is not None:
float_format = '%d' if precision == 0 else '%.{}f'.format(precision)
value = locale.format(float_format, value, grouping=True)
value = '{:,.{precision}f}'.format(value, precision=precision)

elif value.is_integer():
float_format = '%d'
value = locale.format(float_format, value, grouping=True)
value = '{:,.0f}'.format(value)

else:
float_format = '%f'
# Stripping trailing zeros is necessary because %f can add them if no precision is set
value = locale.format(float_format, value, grouping=True).rstrip('.0')
value = '{:,f}'.format(value).rstrip('.0')

if isinstance(value, int):
float_format = '%d'
value = locale.format(float_format, value, grouping=True)
value = '{:,.0f}'.format(value)

return '{prefix}{value}{suffix}'.format(
prefix=prefix or '',
Expand Down
36 changes: 8 additions & 28 deletions fireant/slicer/widgets/reacttable.py
Expand Up @@ -6,6 +6,8 @@
from fireant.formats import (
RAW_VALUE,
TOTALS_VALUE,
metric_display,
metric_value,
)
from fireant.utils import (
format_dimension_key,
Expand Down Expand Up @@ -69,26 +71,6 @@ def fillna_index(index, value):
return index.fillna(value)


def display_value(value, prefix=None, suffix=None, precision=None):
if isinstance(value, bool):
value = str(value).lower()

def format_numeric_value(value):
if precision is not None and isinstance(value, float):
return '{:.{precision}f}'.format(value, precision=precision)

if isinstance(value, int):
return '{:,}'.format(value)

return '{}'.format(value)

return '{prefix}{value}{suffix}'.format(
prefix=(prefix or ""),
suffix=(suffix or ""),
value=format_numeric_value(value),
)


class ReferenceItem:
def __init__(self, item, reference):
if reference is None:
Expand Down Expand Up @@ -371,17 +353,15 @@ def transform_data(data_frame, item_map, dimension_display_values):

# Add the values to the row
for key, value in series.iteritems():
# pd.Series casts everything to float, cast it back to int if it's an int
if np.int64 == data_frame[key].dtype:
value = int(value)

data = {RAW_VALUE: value}
value = metric_value(value)
data = {RAW_VALUE: metric_value(value)}

# Try to find a display value for the item
item = item_map.get(key[0] if isinstance(key, tuple) else key)
display = display_value(value, item.prefix, item.suffix, item.precision) \
if item is not None else \
value
display = metric_display(value,
getattr(item, 'prefix', None),
getattr(item, 'suffix', None),
getattr(item, 'precision', None))

if display is not None:
data['display'] = display
Expand Down
82 changes: 41 additions & 41 deletions fireant/tests/slicer/widgets/test_datatables.py
Expand Up @@ -41,7 +41,7 @@ def test_single_metric(self):
'render': {'_': 'value', 'display': 'display'},
}],
'data': [{
'votes': {'value': 111674336, 'display': '111674336'}
'votes': {'value': 111674336, 'display': '111,674,336'}
}],
}, result)

Expand All @@ -60,7 +60,7 @@ def test_multiple_metrics(self):
'render': {'_': 'value', 'display': 'display'},
}],
'data': [{
'votes': {'value': 111674336, 'display': '111674336'},
'votes': {'value': 111674336, 'display': '111,674,336'},
'wins': {'value': 12, 'display': '12'},
}],
}, result)
Expand All @@ -81,7 +81,7 @@ def test_multiple_metrics_reversed(self):
}],
'data': [{
'wins': {'value': 12, 'display': '12'},
'votes': {'value': 111674336, 'display': '111674336'},
'votes': {'value': 111674336, 'display': '111,674,336'},
}],
}, result)

Expand Down Expand Up @@ -135,22 +135,22 @@ def test_time_series_dim_with_operation(self):
'render': {'_': 'value', 'display': 'display'},
}],
'data': [{
'cumsum(votes)': {'display': '15220449', 'value': 15220449},
'cumsum(votes)': {'display': '15,220,449', 'value': 15220449},
'timestamp': {'value': '1996-01-01'}
}, {
'cumsum(votes)': {'display': '31882466', 'value': 31882466},
'cumsum(votes)': {'display': '31,882,466', 'value': 31882466},
'timestamp': {'value': '2000-01-01'}
}, {
'cumsum(votes)': {'display': '51497398', 'value': 51497398},
'cumsum(votes)': {'display': '51,497,398', 'value': 51497398},
'timestamp': {'value': '2004-01-01'}
}, {
'cumsum(votes)': {'display': '72791613', 'value': 72791613},
'cumsum(votes)': {'display': '72,791,613', 'value': 72791613},
'timestamp': {'value': '2008-01-01'}
}, {
'cumsum(votes)': {'display': '93363823', 'value': 93363823},
'cumsum(votes)': {'display': '93,363,823', 'value': 93363823},
'timestamp': {'value': '2012-01-01'}
}, {
'cumsum(votes)': {'display': '111674336', 'value': 111674336},
'cumsum(votes)': {'display': '111,674,336', 'value': 111674336},
'timestamp': {'value': '2016-01-01'}
}],
}, result)
Expand Down Expand Up @@ -664,38 +664,38 @@ def test_pivoted_multi_dims_time_series_and_uni(self):
'data': [{
'timestamp': {'value': '1996-01-01'},
'votes': {
'1': {'display': '5574387', 'value': 5574387},
'2': {'display': '9646062', 'value': 9646062}
'1': {'display': '5,574,387', 'value': 5574387},
'2': {'display': '9,646,062', 'value': 9646062}
}
}, {
'timestamp': {'value': '2000-01-01'},
'votes': {
'1': {'display': '6233385', 'value': 6233385},
'2': {'display': '10428632', 'value': 10428632}
'1': {'display': '6,233,385', 'value': 6233385},
'2': {'display': '10,428,632', 'value': 10428632}
}
}, {
'timestamp': {'value': '2004-01-01'},
'votes': {
'1': {'display': '7359621', 'value': 7359621},
'2': {'display': '12255311', 'value': 12255311}
'1': {'display': '7,359,621', 'value': 7359621},
'2': {'display': '12,255,311', 'value': 12255311}
}
}, {
'timestamp': {'value': '2008-01-01'},
'votes': {
'1': {'display': '8007961', 'value': 8007961},
'2': {'display': '13286254', 'value': 13286254}
'1': {'display': '8,007,961', 'value': 8007961},
'2': {'display': '13,286,254', 'value': 13286254}
}
}, {
'timestamp': {'value': '2012-01-01'},
'votes': {
'1': {'display': '7877967', 'value': 7877967},
'2': {'display': '12694243', 'value': 12694243}
'1': {'display': '7,877,967', 'value': 7877967},
'2': {'display': '12,694,243', 'value': 12694243}
}
}, {
'timestamp': {'value': '2016-01-01'},
'votes': {
'1': {'display': '5072915', 'value': 5072915},
'2': {'display': '13237598', 'value': 13237598}
'1': {'display': '5,072,915', 'value': 5072915},
'2': {'display': '13,237,598', 'value': 13237598}
}
}],
}, result)
Expand Down Expand Up @@ -732,53 +732,53 @@ def test_time_series_ref(self):
'data': [{
'timestamp': {'value': '2000-01-01'},
'state': {'display': 'Texas', 'value': 1},
'votes': {'display': '6233385', 'value': 6233385.},
'votes_eoe': {'display': '5574387', 'value': 5574387.},
'votes': {'display': '6,233,385', 'value': 6233385.},
'votes_eoe': {'display': '5,574,387', 'value': 5574387.},
}, {
'timestamp': {'value': '2000-01-01'},
'state': {'display': 'California', 'value': 2},
'votes': {'display': '10428632', 'value': 10428632.},
'votes_eoe': {'display': '9646062', 'value': 9646062.},
'votes': {'display': '10,428,632', 'value': 10428632.},
'votes_eoe': {'display': '9,646,062', 'value': 9646062.},
}, {
'timestamp': {'value': '2004-01-01'},
'state': {'display': 'Texas', 'value': 1},
'votes': {'display': '7359621', 'value': 7359621.},
'votes_eoe': {'display': '6233385', 'value': 6233385.},
'votes': {'display': '7,359,621', 'value': 7359621.},
'votes_eoe': {'display': '6,233,385', 'value': 6233385.},
}, {
'timestamp': {'value': '2004-01-01'},
'state': {'display': 'California', 'value': 2},
'votes': {'display': '12255311', 'value': 12255311.},
'votes_eoe': {'display': '10428632', 'value': 10428632.},
'votes': {'display': '12,255,311', 'value': 12255311.},
'votes_eoe': {'display': '10,428,632', 'value': 10428632.},
}, {
'timestamp': {'value': '2008-01-01'},
'state': {'display': 'Texas', 'value': 1},
'votes': {'display': '8007961', 'value': 8007961.},
'votes_eoe': {'display': '7359621', 'value': 7359621.},
'votes': {'display': '8,007,961', 'value': 8007961.},
'votes_eoe': {'display': '7,359,621', 'value': 7359621.},
}, {
'timestamp': {'value': '2008-01-01'},
'state': {'display': 'California', 'value': 2},
'votes': {'display': '13286254', 'value': 13286254.},
'votes_eoe': {'display': '12255311', 'value': 12255311.},
'votes': {'display': '13,286,254', 'value': 13286254.},
'votes_eoe': {'display': '12,255,311', 'value': 12255311.},
}, {
'timestamp': {'value': '2012-01-01'},
'state': {'display': 'Texas', 'value': 1},
'votes': {'display': '7877967', 'value': 7877967.},
'votes_eoe': {'display': '8007961', 'value': 8007961.},
'votes': {'display': '7,877,967', 'value': 7877967.},
'votes_eoe': {'display': '8,007,961', 'value': 8007961.},
}, {
'timestamp': {'value': '2012-01-01'},
'state': {'display': 'California', 'value': 2},
'votes': {'display': '12694243', 'value': 12694243.},
'votes_eoe': {'display': '13286254', 'value': 13286254.},
'votes': {'display': '12,694,243', 'value': 12694243.},
'votes_eoe': {'display': '13,286,254', 'value': 13286254.},
}, {
'timestamp': {'value': '2016-01-01'},
'state': {'display': 'Texas', 'value': 1},
'votes': {'display': '5072915', 'value': 5072915.},
'votes_eoe': {'display': '7877967', 'value': 7877967.},
'votes': {'display': '5,072,915', 'value': 5072915.},
'votes_eoe': {'display': '7,877,967', 'value': 7877967.},
}, {
'timestamp': {'value': '2016-01-01'},
'state': {'display': 'California', 'value': 2},
'votes': {'display': '13237598', 'value': 13237598.},
'votes_eoe': {'display': '12694243', 'value': 12694243.},
'votes': {'display': '13,237,598', 'value': 13237598.},
'votes_eoe': {'display': '12,694,243', 'value': 12694243.},
}],
}, result)

Expand Down
4 changes: 2 additions & 2 deletions fireant/tests/slicer/widgets/test_pandas.py
Expand Up @@ -219,7 +219,7 @@ def test_metric_format(self):
.transform(cont_dim_df / 3, slicer, [slicer.dimensions.timestamp], [])

expected = cont_dim_df.copy()[[fm('votes')]]
expected[fm('votes')] = ['${0:.2f}€'.format(x)
expected[fm('votes')] = ['${0:,.2f}€'.format(x)
for x in expected[fm('votes')] / 3]
expected.index.names = ['Timestamp']
expected.columns = ['Votes']
Expand Down Expand Up @@ -289,4 +289,4 @@ def test_inf_in_metrics_with_precision_zero(self):
expected.columns = ['Wins']
expected.columns.name = 'Metrics'

pandas.testing.assert_frame_equal(expected, result)
pandas.testing.assert_frame_equal(expected, result)

0 comments on commit 256e68d

Please sign in to comment.