gst/__init__.py: Make gst.Fraction simplify like the C counterpart

Original commit message from CVS:
Patch by: Jan Schmidt <thaytan at mad dot scientist dot com>
* gst/__init__.py:
Make gst.Fraction simplify like the C counterpart
Fixes #532809
This commit is contained in:
Jan Schmidt 2008-05-14 16:00:39 +00:00 committed by Edward Hervey
parent e94ff65809
commit 8fef95c5c2
2 changed files with 34 additions and 1 deletions

View file

@ -1,3 +1,10 @@
2008-05-14 Edward Hervey <edward.hervey@collabora.co.uk>
Patch by: Jan Schmidt <thaytan at mad dot scientist dot com>
* gst/__init__.py:
Make gst.Fraction simplify like the C counterpart
Fixes #532809
2008-05-14 Edward Hervey <edward.hervey@collabora.co.uk> 2008-05-14 Edward Hervey <edward.hervey@collabora.co.uk>
* gst/gstcaps.override: * gst/gstcaps.override:

View file

@ -77,10 +77,37 @@ class FractionRange(Value):
class Fraction(Value): class Fraction(Value):
def __init__(self, num, denom=1): def __init__(self, num, denom=1):
Value.__init__(self, 'fraction') def __gcd(a,b):
while b != 0:
tmp = a
a = b
b = tmp % b
return abs(a)
def __simplify():
num = self.num
denom = self.denom
if num < 0:
num = -num
denom = -denom
# Compute greatest common divisor
gcd = __gcd(num,denom)
if gcd != 0:
num /= gcd
denom /= gcd
self.num = num self.num = num
self.denom = denom self.denom = denom
Value.__init__(self, 'fraction')
self.num = num
self.denom = denom
__simplify()
def __repr__(self): def __repr__(self):
return '<gst.Fraction %d/%d>' % (self.num, self.denom) return '<gst.Fraction %d/%d>' % (self.num, self.denom)
@ -118,7 +145,6 @@ class Fraction(Value):
def __float__(self): def __float__(self):
return float(self.num) / float(self.denom) return float(self.num) / float(self.denom)
import sys import sys
dlsave = sys.getdlopenflags() dlsave = sys.getdlopenflags()
try: try: