Skip to content

Commit

Permalink
Add ValueErrorTuple
Browse files Browse the repository at this point in the history
This just prints a value and an error nicely, e.g. in the report.
  • Loading branch information
Zaharid committed Sep 19, 2017
1 parent 99ffb26 commit 76c5623
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/reportengine/floatformatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
it looks like.
"""
import decimal
import numbers
from typing import NamedTuple

def significant_digits(value, digits):
"""Return a `Decimal` object with all the digits less signingicant than
Expand Down Expand Up @@ -69,3 +71,12 @@ def format_error_value_columns(df, valcol, errcol, inplace=False, **kwargs):

if not inplace:
return df

class ValueErrorTuple(NamedTuple):
"""A class to represent a value and its error. The only functionality it
adds is the ``__str__`` method, to represent the quantity."""
value: numbers.Real
error: numbers.Real
def __str__(self):
valstr, errstr = format_value_error(self.value, self.error)
return f'{valstr}±{errstr}'
8 changes: 7 additions & 1 deletion src/reportengine/tests/test_floatformatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import pandas as pd
import numpy as np

from reportengine.floatformatting import format_number, significant_digits, format_error_value_columns
from reportengine.floatformatting import (format_number, significant_digits,
format_error_value_columns,
ValueErrorTuple)

@given(floats(allow_nan=False))
def test_format_rountrip(x):
Expand All @@ -24,3 +26,7 @@ def test_cols(valerr):
formatted = format_error_value_columns(df, 'Val', 'Err')
format_error_value_columns(df, 'Val', 'Err', inplace=True)
assert (formatted == df).all().all()

@given(floats(), floats())
def test_valueerrortuple(value, error):
assert '±' in str(ValueErrorTuple(value,error))

0 comments on commit 76c5623

Please sign in to comment.