From 7b427cefa3f125282fa2f0afee2e2e1de853ef31 Mon Sep 17 00:00:00 2001 From: Tom Ritchford Date: Sat, 1 Mar 2014 13:43:55 -0500 Subject: Fix https://github.com/hgrecco/pint/issues/112 and add _DEFAULT_FORMAT --- pint/unit.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pint/unit.py b/pint/unit.py index 342517c..2275371 100644 --- a/pint/unit.py +++ b/pint/unit.py @@ -293,6 +293,15 @@ _LATEX_FORMAT = { 'parentheses_fmt': r'({0})', } +_DEFAULT_FORMAT = { + 'as_ratio': True, + 'single_denominator': False, + 'product_fmt': ' * ', + 'division_fmt': ' / ', + 'power_fmt': '{0} ** {1}', + 'parentheses_fmt': r'({0})', + } + class UnitsContainer(dict): """The UnitsContainer stores the product of units and their respective @@ -334,25 +343,25 @@ class UnitsContainer(dict): return dict.__eq__(self, other) def __str__(self): - if not self: - return 'dimensionless' - return formatter(self.items()) + return self.__format__('') def __repr__(self): tmp = '{%s}' % ', '.join(["'{0}': {1}".format(key, value) for key, value in sorted(self.items())]) return ''.format(tmp) def __format__(self, spec): + if not self: + return 'dimensionless' if 'L' in spec: tmp = formatter(self.items(), **_LATEX_PRINT_FORMAT) tmp = tmp.replace('[', '{').replace(']', '}') return tmp - elif 'P' in spec: - return formatter(self.items(), **_PRETTY_FORMAT) elif 'H' in spec: return formatter(self.items(), **_LATEX_FORMAT) + elif 'P' in spec: + return formatter(self.items(), **_PRETTY_FORMAT) else: - return str(self) + return formatter(self.items(), **_DEFAULT_FORMAT) def __copy__(self): ret = self.__class__() -- cgit v1.2.1