sasdata.data_util.formatnum module¶
Format values and uncertainties nicely for printing.
format_uncertainty_pm() produces the expanded format v +/- err.
format_uncertainty_compact() produces the compact format v(##),
where the number in parenthesis is the uncertainty in the last two digits of v.
format_uncertainty() uses the compact format by default, but this
can be changed to use the expanded +/- format by setting
format_uncertainty.compact to False.
The formatted string uses only the number of digits warranted by the uncertainty in the measurement.
If the uncertainty is 0 or not otherwise provided, the simple %g floating point format option is used.
Infinite and indefinite numbers are represented as inf and NaN.
Example:
>>> v,dv = 757.2356,0.01032
>>> print format_uncertainty_pm(v,dv)
757.236 +/- 0.010
>>> print format_uncertainty_compact(v,dv)
757.236(10)
>>> print format_uncertainty(v,dv)
757.236(10)
>>> format_uncertainty.compact = False
>>> print format_uncertainty(v,dv)
757.236 +/- 0.010
UncertaintyFormatter() returns a private formatter with its own formatter.compact flag.
- class sasdata.data_util.formatnum.UncertaintyFormatter¶
Bases:
objectValue and uncertainty formatter.
The formatter instance will use either the expanded v +/- dv form or the compact v(##) form depending on whether formatter.compact is True or False. The default is True.
- __annotate_func__ = None¶
- __annotations_cache__ = {}¶
- __call__(value, uncertainty)¶
Given value and uncertainty, return a string representation.
- __dict__ = mappingproxy({'__module__': 'sasdata.data_util.formatnum', '__firstlineno__': 79, '__doc__': '\nValue and uncertainty formatter.\n\nThe *formatter* instance will use either the expanded v +/- dv form\nor the compact v(##) form depending on whether *formatter.compact* is\nTrue or False. The default is True.\n', 'compact': True, '__call__': <function UncertaintyFormatter.__call__>, '__static_attributes__': (), '__dict__': <attribute '__dict__' of 'UncertaintyFormatter' objects>, '__weakref__': <attribute '__weakref__' of 'UncertaintyFormatter' objects>, '__annotations_cache__': {}})¶
- __doc__ = '\nValue and uncertainty formatter.\n\nThe *formatter* instance will use either the expanded v +/- dv form\nor the compact v(##) form depending on whether *formatter.compact* is\nTrue or False. The default is True.\n'¶
- __firstlineno__ = 79¶
- __module__ = 'sasdata.data_util.formatnum'¶
- __static_attributes__ = ()¶
- __weakref__¶
list of weak references to the object
- compact = True¶
- sasdata.data_util.formatnum._format_uncertainty(value, uncertainty, compact)¶
Implementation of both the compact and the +/- formats.
- sasdata.data_util.formatnum.format_uncertainty_compact(value, uncertainty)¶
Given value v and uncertainty dv, return the compact representation v(##), where ## are the first two digits of the uncertainty.
- sasdata.data_util.formatnum.format_uncertainty_pm(value, uncertainty)¶
Given value v and uncertainty dv, return a string v +/- dv.
- sasdata.data_util.formatnum.main()¶
Run all tests.
This is equivalent to “nosetests –with-doctest”
- sasdata.data_util.formatnum.test_compact()¶
- sasdata.data_util.formatnum.test_default()¶
- sasdata.data_util.formatnum.test_pm()¶