Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-wieser authored Nov 17, 2016
2 parents d82ee43 + 25adc7b commit bd67f07
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .zappr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
approvals:
minimum: 1
groups:
core:
minimum: 1
from:
users:
- hgrecco
2 changes: 1 addition & 1 deletion pint/default_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ btu = 1.05505585262e3 * joule = Btu = BTU = british_thermal_unit
electron_volt = 1.60217653e-19 * J = eV
quadrillion_btu = 10**15 * btu = quad
thm = 100000 * BTU = therm = EC_therm
cal = 4.184 * joule = calorie = thermochemical_calorie
calorie = 4.184 * joule = cal = thermochemical_calorie
international_steam_table_calorie = 4.1868 * joule
ton_TNT = 4.184e9 * joule = tTNT
US_therm = 1.054804e8 * joule
Expand Down
4 changes: 2 additions & 2 deletions pint/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ def to_compact(self, unit=None):
unit_power = list(q_base._units.items())[0][1]

if unit_power > 0:
power = int(math.floor(math.log10(magnitude) / unit_power / 3)) * 3
power = int(math.floor(math.log10(abs(magnitude)) / unit_power / 3)) * 3
else:
power = int(math.ceil(math.log10(magnitude) / unit_power / 3)) * 3
power = int(math.ceil(math.log10(abs(magnitude)) / unit_power / 3)) * 3

prefix = SI_bases[bisect.bisect_left(SI_powers, power)]

Expand Down
2 changes: 1 addition & 1 deletion pint/systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def __init__(self, d):
self.d = d

def __dir__(self):
return frozenset(self.d.keys())
return list(self.d.keys())

def __getattr__(self, item):
return self.d[item]
Expand Down
6 changes: 6 additions & 0 deletions pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,14 @@ def test_format_compact(self):
self.assertEqual(q2.magnitude, q2b.magnitude)
self.assertEqual(q2.units, q2b.units)

q3 = (-1000.0 * self.ureg('meters')).to_compact()
q3b = self.Q_(-1., 'kilometer')
self.assertEqual(q3.magnitude, q3b.magnitude)
self.assertEqual(q3.units, q3b.units)

self.assertEqual('{0:#.1f}'.format(q1), '{0}'.format(q1b))
self.assertEqual('{0:#.1f}'.format(q2), '{0}'.format(q2b))
self.assertEqual('{0:#.1f}'.format(q3), '{0}'.format(q3b))


def test_default_formatting(self):
Expand Down
18 changes: 17 additions & 1 deletion pint/testsuite/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import collections
import copy
import operator as op
from decimal import Decimal
from pint.testsuite import BaseTestCase, QuantityTestCase
from pint.util import (string_preprocessor, find_shortest_path, matrix_to_string,
transpose, find_connected_nodes, ParserHelper,
transpose, tokenizer, find_connected_nodes, ParserHelper,
UnitsContainer, to_units_container)


Expand Down Expand Up @@ -183,6 +184,21 @@ def test_calculate(self):
self.assertEqual(dict(seconds=1) / z(),
ParserHelper(0.5, seconds=1, meter=-2))

def _test_eval_token(self, expected, expression, use_decimal=False):
token = next(tokenizer(expression))
actual = ParserHelper.eval_token(token, use_decimal=use_decimal)
self.assertEqual(expected, actual)
self.assertEqual(type(expected), type(actual))


def test_eval_token(self):
self._test_eval_token(1000.0, '1e3')
self._test_eval_token(1000.0, '1E3')
self._test_eval_token(Decimal(1000), '1e3', use_decimal=True)
self._test_eval_token(1000, '1000')
# integer numbers are represented as ints, not Decimals
self._test_eval_token(1000, '1000', use_decimal=True)


class TestStringProcessor(BaseTestCase):

Expand Down
9 changes: 5 additions & 4 deletions pint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,15 @@ def from_string(cls, input_string):

@classmethod
def eval_token(cls, token, use_decimal=False):
token_type = token[0]
token_text = token[1]
token_type = token.type
token_text = token.string
if token_type == NUMBER:
if '.' in token_text or 'e' in token_text:
try:
return int(token_text)
except ValueError:
if use_decimal:
return Decimal(token_text)
return float(token_text)
return int(token_text)
elif token_type == NAME:
return ParserHelper.from_word(token_text)
else:
Expand Down

0 comments on commit bd67f07

Please sign in to comment.