@@ -29,6 +29,8 @@ if PYTHON3:
|
|||||||
return x
|
return x
|
||||||
|
|
||||||
maketrans = str.maketrans
|
maketrans = str.maketrans
|
||||||
|
|
||||||
|
long_type = int
|
||||||
else:
|
else:
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
string_types = basestring
|
string_types = basestring
|
||||||
@@ -41,6 +43,8 @@ else:
|
|||||||
|
|
||||||
maketrans = lambda f, t: dict((ord(a), b) for a, b in zip(f, t))
|
maketrans = lambda f, t: dict((ord(a), b) for a, b in zip(f, t))
|
||||||
|
|
||||||
|
long_type = long
|
||||||
|
|
||||||
if sys.version_info < (2, 7):
|
if sys.version_info < (2, 7):
|
||||||
try:
|
try:
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import functools
|
|||||||
|
|
||||||
from .formatting import remove_custom_flags
|
from .formatting import remove_custom_flags
|
||||||
from .unit import DimensionalityError, UnitsContainer, UnitDefinition, UndefinedUnitError
|
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
|
from .util import logger
|
||||||
|
|
||||||
|
|
||||||
@@ -276,6 +276,16 @@ class _Quantity(object):
|
|||||||
return self.__class__(magnitude, other)
|
return self.__class__(magnitude, other)
|
||||||
|
|
||||||
# Mathematical operations
|
# 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):
|
def __float__(self):
|
||||||
if self.dimensionless:
|
if self.dimensionless:
|
||||||
return float(self._convert_magnitude_not_inplace(UnitsContainer()))
|
return float(self._convert_magnitude_not_inplace(UnitsContainer()))
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import division, unicode_literals, print_function, absolute_import
|
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():
|
def requires_numpy18():
|
||||||
@@ -24,3 +24,10 @@ def requires_uncertainties():
|
|||||||
def requires_not_uncertainties():
|
def requires_not_uncertainties():
|
||||||
return unittest.skipIf(HAS_UNCERTAINTIES, 'Requires Uncertainties is not installed.')
|
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.unit import UnitsContainer
|
||||||
from pint.util import ParserHelper
|
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
|
from pint.testsuite import QuantityTestCase, helpers
|
||||||
|
|
||||||
|
|
||||||
@@ -250,6 +250,21 @@ class TestIssues(QuantityTestCase):
|
|||||||
self.assertQuantityAlmostEqual(summer(y), ureg.Quantity(3, 'meter'))
|
self.assertQuantityAlmostEqual(summer(y), ureg.Quantity(3, 'meter'))
|
||||||
self.assertQuantityAlmostEqual(y[0], ureg.Quantity(1, '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()
|
@helpers.requires_numpy()
|
||||||
class TestIssuesNP(QuantityTestCase):
|
class TestIssuesNP(QuantityTestCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user