diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-12-24 16:56:11 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-12-24 16:56:11 +0100 |
commit | f178386b9ec255d7456977bb23b205802a0452e7 (patch) | |
tree | 5d76b10274b6013d20af5ddf9f8b2108cc43d614 | |
parent | 27f94886b6c043609366dd2f5604566d1264852d (diff) | |
download | pyexiv2-f178386b9ec255d7456977bb23b205802a0452e7.tar.gz |
is_fraction and make_fraction: convenience functions to handle transparently
the conditional availability of the fractions module in the standard library (Python ≥ 2.6).
-rw-r--r-- | src/pyexiv2/utils.py | 28 | ||||
-rwxr-xr-x | test/TestsRunner.py | 3 | ||||
-rw-r--r-- | test/utils.py | 42 |
3 files changed, 71 insertions, 2 deletions
diff --git a/src/pyexiv2/utils.py b/src/pyexiv2/utils.py index 5b0a3d5..3c5aa95 100644 --- a/src/pyexiv2/utils.py +++ b/src/pyexiv2/utils.py @@ -250,6 +250,34 @@ class Rational(object): return '%d/%d' % (self._numerator, self._denominator) +def is_fraction(obj): + """ + Test whether the object is a valid fraction. + """ + if Fraction is not None and isinstance(obj, Fraction): + return True + elif isinstance(obj, Rational): + return True + else: + return False + + +def make_fraction(*args): + """ + Make a fraction. + + The type of the returned object depends on the availability of the + fractions module in the standard library (Python ≥ 2.6). + """ + if Fraction is not None: + return Fraction(*args) + else: + if len(args) == 1: + return Rational.from_string(*args) + else: + return Rational(*args) + + class ListenerInterface(object): """ diff --git a/test/TestsRunner.py b/test/TestsRunner.py index 198bb27..ba5162c 100755 --- a/test/TestsRunner.py +++ b/test/TestsRunner.py @@ -38,7 +38,7 @@ from xmp import TestXmpTag, TestXmpNamespaces from metadata import TestImageMetadata from buffer import TestBuffer from encoding import TestEncodings -from utils import TestConversions +from utils import TestConversions, TestFractions from usercomment import TestUserCommentReadWrite, TestUserCommentAdd from pickling import TestPicklingTags @@ -58,6 +58,7 @@ def run_unit_tests(): suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestBuffer)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestEncodings)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestConversions)) + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestFractions)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUserCommentReadWrite)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUserCommentAdd)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestPicklingTags)) diff --git a/test/utils.py b/test/utils.py index 2f0d19b..ceddcc3 100644 --- a/test/utils.py +++ b/test/utils.py @@ -26,7 +26,8 @@ import unittest -from pyexiv2.utils import undefined_to_string, string_to_undefined +from pyexiv2.utils import undefined_to_string, string_to_undefined, \ + Rational, Fraction, is_fraction, make_fraction class TestConversions(unittest.TestCase): @@ -48,3 +49,42 @@ class TestConversions(unittest.TestCase): value = "48 50 50 49" self.assertEqual(string_to_undefined(undefined_to_string(value)), value) + +class TestFractions(unittest.TestCase): + + def test_is_fraction(self): + if Fraction is not None: + self.failUnless(is_fraction(Fraction())) + self.failUnless(is_fraction(Fraction(3, 5))) + self.failUnless(is_fraction(Fraction(Fraction(4, 5)))) + self.failUnless(is_fraction(Fraction('3/2'))) + self.failUnless(is_fraction(Fraction('-4/5'))) + self.failUnless(is_fraction(Rational(3, 5))) + self.failUnless(is_fraction(Rational(-4, 5))) + self.failUnless(is_fraction(Rational.from_string("3/5"))) + self.failUnless(is_fraction(Rational.from_string("-4/5"))) + + self.failIf(is_fraction(3 / 5)) + self.failIf(is_fraction('3/5')) + self.failIf(is_fraction('2.7')) + self.failIf(is_fraction(2.7)) + self.failIf(is_fraction('notafraction')) + self.failIf(is_fraction(None)) + + def test_make_fraction(self): + if Fraction is not None: + self.assertEqual(make_fraction(3, 5), Fraction(3, 5)) + self.assertEqual(make_fraction(-3, 5), Fraction(-3, 5)) + self.assertEqual(make_fraction('3/2'), Fraction(3, 2)) + self.assertEqual(make_fraction('-3/4'), Fraction(-3, 4)) + else: + self.assertEqual(make_fraction(3, 5), Rational(3, 5)) + self.assertEqual(make_fraction(-3, 5), Rational(-3, 5)) + self.assertEqual(make_fraction('3/2'), Rational(3, 2)) + self.assertEqual(make_fraction('-3/4'), Rational(-3, 4)) + + self.assertRaises(ZeroDivisionError, make_fraction, 3, 0) + self.assertRaises(ZeroDivisionError, make_fraction, '3/0') + self.assertRaises(TypeError, make_fraction, 5, 3, 2) + self.assertRaises(TypeError, make_fraction, None) + |