Skip to content

Commit

Permalink
Implemented Gst.Fraction override
Browse files Browse the repository at this point in the history
  • Loading branch information
ApsOps committed Jun 4, 2014
1 parent fed4322 commit ce1d9b3
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions flumotion/__init__.py
Expand Up @@ -28,3 +28,83 @@ def TIME_ARGS(time):
time % Gst.SECOND)

Gst.TIME_ARGS = TIME_ARGS


if not hasattr(Gst, '__value__'):
class Fraction(Gst.Fraction):
def __init__(self, num, denom=1):
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.denom = denom

self.num = num
self.denom = denom

__simplify()
self.type = "fraction"

def __repr__(self):
return '<Gst.Fraction %d/%d>' % (self.num, self.denom)

def __value__(self):
return 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 __truediv__(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

__div__ = __truediv__

def __rtruediv__(self, other):
if isinstance(other, int):
return Fraction(self.denom * other, self.num)
return TypeError

__rdiv__ = __rtruediv__

def __float__(self):
return float(self.num) / float(self.denom)

Gst.Fraction = Fraction

0 comments on commit ce1d9b3

Please sign in to comment.