当前位置: 首页>>代码示例>>Python>>正文


Python Fraction.__le__方法代码示例

本文整理汇总了Python中fractions.Fraction.__le__方法的典型用法代码示例。如果您正苦于以下问题:Python Fraction.__le__方法的具体用法?Python Fraction.__le__怎么用?Python Fraction.__le__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fractions.Fraction的用法示例。


在下文中一共展示了Fraction.__le__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: F

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import __le__ [as 别名]
class F(Fraction):
    '''
    Classe que representa um numero fracionario, extendendo fractions.Fraction, e implentando a lógica o "M grande"
    '''   
    def __init__(self,n,m=Fraction(0)):
        self.fraction = Fraction(n)
        self.m = Fraction(m)
   
    def __repr__(self):
        """repr(self)"""
        return str(float(self.fraction)) if self.m == 0 else str(float(self.fraction)) + ' + (' + str(float(self.m)) + '*M)'


    def __str__(self):
        """str(self)"""
        return str(float(self.fraction)) if self.m == 0 else str(float(self.fraction)) + ' + (' + str(float(self.m)) + '*M)'
        
    def __eq__(self, f):
        """a == b"""
        if type(f) is not type(self):
            f = F(f)
            
        return self.fraction.__eq__(f.fraction) and self.m.__eq__(f.m)

    def __add__(self, f):
        """a + b"""
        if type(f) is not type(self):
            f = F(f)
            
        return F(self.fraction.__add__(f.fraction),self.m.__add__(f.m))

    def ___sub__(self, f):
        """a - b"""
        if type(f) is not type(self):
            f = F(f)
            
        return F(self.fraction.__sub__(f.fraction),self.m.___sub__(f.m))

    def __mul__(self, f):
        """a * b"""
        if type(f) is not type(self):
            f = F(f)
        
        if f.m == 0:
            return F(self.fraction.__mul__(f.fraction))
        else:
            return F(self.fraction.__mul__(f.fraction),self.m.__mul__(f.m)) 

    def __div__(self, f):
        """a / b"""
        if type(f) is not type(self):
            f = F(f)
        
        if f.m == 0:
            return F(self.fraction.__div__(f.fraction))
        else:
            return F(self.fraction.__div__(f.fraction),self.m.__div__(f.m))
    
    def __lt__(self, f):
        """a < b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__lt__(f.fraction)
        
        else:   
            return self.m.__lt__(f.m)
        
    def __gt__(self, f):
        """a > b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__gt__(f.fraction)
        
        else:   
            return self.m.__gt__(f.m)

    def __le__(self, f):
        """a <= b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__le__(f.fraction)
        
        else:   
            return self.m.__le__(f.m)

    def __ge__(self, f):
        """a >= b"""
        if type(f) is not type(self):
            f = F(f)
            
        if self.m == f.m:
            return self.fraction.__ge__(f.fraction)
        
        else:   
#.........这里部分代码省略.........
开发者ID:odrand,项目名称:simplesX,代码行数:103,代码来源:F.py


注:本文中的fractions.Fraction.__le__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。