@@ -29,6 +29,8 @@ if PYTHON3:
|
||||
return x
|
||||
|
||||
maketrans = str.maketrans
|
||||
|
||||
long_type = int
|
||||
else:
|
||||
from StringIO import StringIO
|
||||
string_types = basestring
|
||||
@@ -41,6 +43,8 @@ else:
|
||||
|
||||
maketrans = lambda f, t: dict((ord(a), b) for a, b in zip(f, t))
|
||||
|
||||
long_type = long
|
||||
|
||||
if sys.version_info < (2, 7):
|
||||
try:
|
||||
import unittest2 as unittest
|
||||
|
||||
@@ -16,7 +16,7 @@ import functools
|
||||
|
||||
from .formatting import remove_custom_flags
|
||||
from .unit import DimensionalityError, UnitsContainer, UnitDefinition, UndefinedUnitError
|
||||
from .compat import string_types, ndarray, np, _to_magnitude
|
||||
from .compat import string_types, ndarray, np, _to_magnitude, long_type
|
||||
from .util import logger
|
||||
|
||||
|
||||
@@ -276,6 +276,16 @@ class _Quantity(object):
|
||||
return self.__class__(magnitude, other)
|
||||
|
||||
# Mathematical operations
|
||||
def __int__(self):
|
||||
if self.dimensionless:
|
||||
return int(self._convert_magnitude_not_inplace(UnitsContainer()))
|
||||
raise DimensionalityError(self.units, 'dimensionless')
|
||||
|
||||
def __long__(self):
|
||||
if self.dimensionless:
|
||||
return long_type(self._convert_magnitude_not_inplace(UnitsContainer()))
|
||||
raise DimensionalityError(self.units, 'dimensionless')
|
||||
|
||||
def __float__(self):
|
||||
if self.dimensionless:
|
||||
return float(self._convert_magnitude_not_inplace(UnitsContainer()))
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import division, unicode_literals, print_function, absolute_import
|
||||
|
||||
from pint.compat import unittest, HAS_NUMPY, HAS_UNCERTAINTIES, NUMPY_VER
|
||||
from pint.compat import unittest, HAS_NUMPY, HAS_UNCERTAINTIES, NUMPY_VER, PYTHON3
|
||||
|
||||
|
||||
def requires_numpy18():
|
||||
@@ -24,3 +24,10 @@ def requires_uncertainties():
|
||||
def requires_not_uncertainties():
|
||||
return unittest.skipIf(HAS_UNCERTAINTIES, 'Requires Uncertainties is not installed.')
|
||||
|
||||
|
||||
def requires_python2():
|
||||
return unittest.skipIf(PYTHON3, 'Requires Python 2.X.')
|
||||
|
||||
|
||||
def requires_python3():
|
||||
return unittest.skipUnless(PYTHON3, 'Requires Python 3.X.')
|
||||
|
||||
@@ -8,7 +8,7 @@ from pint import UnitRegistry
|
||||
from pint.unit import UnitsContainer
|
||||
from pint.util import ParserHelper
|
||||
|
||||
from pint.compat import np, unittest
|
||||
from pint.compat import np, unittest, long_type
|
||||
from pint.testsuite import QuantityTestCase, helpers
|
||||
|
||||
|
||||
@@ -250,6 +250,21 @@ class TestIssues(QuantityTestCase):
|
||||
self.assertQuantityAlmostEqual(summer(y), ureg.Quantity(3, 'meter'))
|
||||
self.assertQuantityAlmostEqual(y[0], ureg.Quantity(1, 'meter'))
|
||||
|
||||
def test_issue170(self):
|
||||
Q_ = UnitRegistry().Quantity
|
||||
q = Q_('1 kHz')/Q_('100 Hz')
|
||||
iq = int(q)
|
||||
self.assertEqual(iq, 10)
|
||||
self.assertIsInstance(iq, int)
|
||||
|
||||
@helpers.requires_python2()
|
||||
def test_issue170b(self):
|
||||
Q_ = UnitRegistry().Quantity
|
||||
q = Q_('1 kHz')/Q_('100 Hz')
|
||||
iq = long(q)
|
||||
self.assertEqual(iq, long(10))
|
||||
self.assertIsInstance(iq, long)
|
||||
|
||||
|
||||
@helpers.requires_numpy()
|
||||
class TestIssuesNP(QuantityTestCase):
|
||||
|
||||
Reference in New Issue
Block a user