summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ritchford <tom@swirly.com>2014-03-01 13:43:55 -0500
committerTom Ritchford <tom@swirly.com>2014-03-01 13:43:55 -0500
commit7b427cefa3f125282fa2f0afee2e2e1de853ef31 (patch)
tree15ef30a59919b236d1f5a9d1a947f7b621944e3f
parent8819b4f56827c1fc91acb72cf79fe80e5ab08b20 (diff)
downloadpint-7b427cefa3f125282fa2f0afee2e2e1de853ef31.tar.gz
Fix https://github.com/hgrecco/pint/issues/112 and add _DEFAULT_FORMAT
-rw-r--r--pint/unit.py21
1 files 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 '<UnitsContainer({0})>'.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__()