Package flumotion :: Package common :: Module fraction
[hide private]

Source Code for Module flumotion.common.fraction

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_config -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """convert to and from fractions 
 23  """ 
 24   
 25  __version__ = "$Rev$" 
 26   
 27  import math 
 28   
 29   
30 -def gcd(a, b):
31 """ 32 Returns the greatest common divisor of two integers. 33 34 @type a: int 35 @type b: int 36 37 @rtype : int 38 """ 39 while b: 40 a, b = b, a % b 41 42 return a
43 44
45 -def fractionFromValue(value):
46 """ 47 Converts a value to a fraction 48 49 @param value: the value to convert to a tuple 50 @type value: one of 51 - string, unicode 52 - number, eg int/float/long 53 - two sized tuple 54 55 @returns: the fraction 56 @rtype: a two sized tuple with 2 integers 57 """ 58 59 def _frac(num, denom=1): 60 return int(num), int(denom)
61 62 if isinstance(value, basestring): 63 noSlashes = value.count('/') 64 if noSlashes == 0: 65 parts = [value] 66 elif noSlashes == 1: 67 parts = value.split('/') 68 else: 69 raise ValueError('Expected at most one /, not %r' % (value, )) 70 return _frac(*parts) 71 elif isinstance(value, tuple): 72 if len(value) != 2: 73 raise ValueError( 74 "Can only convert two sized tuple to fraction") 75 return value 76 elif isinstance(value, (int, long)): 77 return _frac(value) 78 elif isinstance(value, float): 79 ipart = int(value) 80 fpart = value - ipart 81 82 if not fpart: 83 return _frac(value) 84 else: 85 den = math.pow(10, len(str(fpart))-2) 86 num = value*den 87 div = gcd(num, den) 88 return _frac(num/div, den/div) 89 else: 90 raise ValueError( 91 "Cannot convert %r of type %s to a fraction" % ( 92 value, type(value).__name__)) 93 94
95 -def fractionAsFloat(value):
96 """ 97 Converts a fraction to a float 98 @param value: the value to convert to a tuple, can be one of: 99 @type value: a two sized tuple with 2 integers 100 @returns: fraction representation in float 101 @rtype: float 102 """ 103 assert type(value) in [list, tuple], value 104 assert len(value) == 2, value 105 return float(value[0]) / float(value[1])
106 107
108 -def fractionAsString(value):
109 """ 110 Converts a fraction to a string 111 @param value: the value to convert to a tuple, can be one of: 112 @type value: a two sized tuple with 2 integers 113 @returns: fraction representation as a string 114 @rtype: string 115 """ 116 assert type(value) in [list, tuple], value 117 assert len(value) == 2, value 118 return '%s/%s' % value
119