mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-20 00:31:13 +00:00
gst/__init__.py: Implement multiplication, divison and float coercing for fractions.
Original commit message from CVS: * gst/__init__.py: Implement multiplication, divison and float coercing for fractions. * testsuite/test_fraction.py: Add fraction tests
This commit is contained in:
parent
51baae336c
commit
1b1b56c762
3 changed files with 131 additions and 13 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,9 +1,19 @@
|
|||
2007-03-18 Johan Dahlin <jdahlin@async.com.br>
|
||||
|
||||
reviewed by: Edward Hervey <edward@fluendo.com>
|
||||
|
||||
* gst/__init__.py: Implement multiplication, divison and float
|
||||
coercing for fractions.
|
||||
|
||||
* testsuite/test_fraction.py:
|
||||
Add fraction tests
|
||||
|
||||
2007-03-17 Johan Dahlin <jdahlin@async.com.br>
|
||||
|
||||
* gst/gsttaglist.override (_wrap_gst_tag_list_contains):
|
||||
* testsuite/test_taglist.py (TestTagList.testKeys):
|
||||
|
||||
Implement sq_contains and add tests for TagList.
|
||||
Implement sq_contains and add tests for gst.TagList.
|
||||
|
||||
2007-03-02 Edward Hervey <edward@fluendo.com>
|
||||
|
||||
|
|
|
@ -76,17 +76,48 @@ class FractionRange(Value):
|
|||
self.high.denom)
|
||||
|
||||
class Fraction(Value):
|
||||
def __init__(self, num, denom):
|
||||
Value.__init__(self, 'fraction')
|
||||
self.num = num
|
||||
self.denom = denom
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Fraction):
|
||||
return self.num * other.denom == other.num * self.denom
|
||||
return False
|
||||
|
||||
def __repr__(self):
|
||||
return '<gst.Fraction %d/%d>' % (self.num, self.denom)
|
||||
def __init__(self, num, denom=1):
|
||||
Value.__init__(self, 'fraction')
|
||||
self.num = num
|
||||
self.denom = denom
|
||||
|
||||
def __repr__(self):
|
||||
return '<gst.Fraction %d/%d>' % (self.num, self.denom)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Fraction):
|
||||
return self.num * other.denom == other.num * self.denom
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Fraction):
|
||||
return Fraction(self.num * other.num,
|
||||
self.denom * other.denom)
|
||||
elif isinstance(other, int):
|
||||
return Fraction(self.num * other, self.denom)
|
||||
raise TypeError
|
||||
|
||||
__rmul__ = __mul__
|
||||
|
||||
def __div__(self, other):
|
||||
if isinstance(other, Fraction):
|
||||
return Fraction(self.num * other.denom,
|
||||
self.denom * other.num)
|
||||
elif isinstance(other, int):
|
||||
return Fraction(self.num, self.denom * other)
|
||||
return TypeError
|
||||
|
||||
def __rdiv__(self, other):
|
||||
if isinstance(other, int):
|
||||
return Fraction(self.denom * other, self.num)
|
||||
return TypeError
|
||||
|
||||
def __float__(self):
|
||||
return float(self.num) / float(self.denom)
|
||||
|
||||
|
||||
import sys
|
||||
dlsave = sys.getdlopenflags()
|
||||
|
@ -116,7 +147,7 @@ if RTLD_GLOBAL != -1 and RTLD_LAZY != -1:
|
|||
import libxml2
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
version = get_gst_version
|
||||
|
||||
sys.setdlopenflags(dlsave)
|
||||
|
|
77
testsuite/test_fraction.py
Normal file
77
testsuite/test_fraction.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
# -*- Mode: Python -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
#
|
||||
# gst-python - Python bindings for GStreamer
|
||||
# Copyright (C) 2007 Johan Dahlin
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
from common import gst, TestCase
|
||||
|
||||
F = gst.Fraction
|
||||
|
||||
class TestFraction(TestCase):
|
||||
def testConstructor(self):
|
||||
frac = F(1, 2)
|
||||
self.assertEquals(frac.num, 1)
|
||||
self.assertEquals(frac.denom, 2)
|
||||
|
||||
frac = F(1)
|
||||
self.assertEquals(frac.num, 1)
|
||||
self.assertEquals(frac.denom, 1)
|
||||
|
||||
self.assertRaises(TypeError, F)
|
||||
|
||||
def testRepr(self):
|
||||
self.assertEquals(repr(F(1, 2)), '<gst.Fraction 1/2>')
|
||||
|
||||
def testEqNe(self):
|
||||
frac = F(1, 2)
|
||||
self.assertEquals(frac, frac)
|
||||
self.assertEquals(F(1, 2), F(1, 2))
|
||||
self.assertEquals(F(2, 4), F(1, 2))
|
||||
|
||||
self.assertNotEquals(F(1, 3), F(1, 2))
|
||||
self.assertNotEquals(F(2, 1), F(1, 2))
|
||||
|
||||
def testMul(self):
|
||||
self.assertEquals(F(1, 2) * F(1, 2), F(1, 4))
|
||||
self.assertEquals(F(2, 3) * F(4, 5), F(8, 15))
|
||||
self.assertEquals(F(1, 3) * F(4), F(4, 3))
|
||||
self.assertEquals(F(1, 3) * 4, F(4, 3))
|
||||
|
||||
def testRMul(self):
|
||||
self.assertEquals(2 * F(1, 2), F(1))
|
||||
self.assertEquals(4 * F(1, 2), F(2))
|
||||
self.assertEquals(-10 * F(1, 2), F(-5))
|
||||
|
||||
def testDiv(self):
|
||||
self.assertEquals(F(1, 3) / F(1, 4), F(4, 3))
|
||||
self.assertEquals(F(2, 3) / F(4, 5), F(10, 12))
|
||||
|
||||
self.assertEquals(F(1, 3) / F(4), F(1, 12))
|
||||
self.assertEquals(F(1, 3) / 4, F(1, 12))
|
||||
self.assertEquals(F(1, 3) / 2, F(1, 6))
|
||||
self.assertEquals(F(1, 5) / -4, F(1, -20))
|
||||
|
||||
def testRDiv(self):
|
||||
self.assertEquals(2 / F(1, 3), F(6, 1))
|
||||
self.assertEquals(-4 / F(1, 5), F(-20, 1))
|
||||
|
||||
def testFloat(self):
|
||||
self.assertEquals(float(F(1, 2)), 0.5)
|
||||
|
||||
|
||||
|
Loading…
Reference in a new issue