python 实现某个方法

方法

 if callable(getattr(smartBox, 'get_receipt_button1', None)):
  pass

对象访问

class IncomeMap(dict):
    @property
    def totalIncome(self):
        return RMB(self.get('totalIncome', 0))
    
    @property
    def totalIncomeRecharge(self):
        return RMB(self.get('totalIncomeRecharge', 0))
    
    @property
    def totalIncomeChargeCard(self):
        return RMB(self.get('totalIncomeChargeCard', 0))
    
    @property
    def totalIncomeChargeVirtualCard(self):
        return RMB(self.get('totalIncomeChargeVirtualCard', 0))
    
    @property
    def totalIncomeCount(self):
        return Quantity(self.get('totalIncomeCount', 0),'0')
    
    @property
    def deviceIncome(self):
        rv = RMB(0)

        for source in [
            DEALER_INCOME_SOURCE.RECHARGE,
            DEALER_INCOME_SOURCE.RECHARGE_CARD,
            DEALER_INCOME_SOURCE.RECHARGE_VIRTUAL_CARD,
            DEALER_INCOME_SOURCE.RECHARGE_MONTHLY_PACKAGE
        ]:
            rv += RMB(self.get(format(source), 0))

        return rv

    @property
    def rechargeIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.RECHARGE), 0))

    @property
    def rechargeCardIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.RECHARGE_CARD), 0))

    @property
    def rechargeVirtualCardIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.RECHARGE_VIRTUAL_CARD), 0)) + \
               RMB(self.get(format(DEALER_INCOME_SOURCE.RECHARGE_MONTHLY_PACKAGE), 0))

    @property
    def adIncome(self):
        rv = RMB(0)

        for source in [
            DEALER_INCOME_SOURCE.AD,
            DEALER_INCOME_SOURCE.REDPACK,
            DEALER_INCOME_SOURCE.CHARGING_INSURANCE,
            DEALER_INCOME_SOURCE.LIGHT_AD_SPLITTING
        ]:
            rv += RMB(self.get(format(source), 0))

        return rv

    @property
    def refundCashIncome(self):
        rv = RMB(0)
        for source in [
            DEALER_INCOME_SOURCE.REFUND_CASH,
            DEALER_INCOME_SOURCE.REFUND_CHARGE_INSURANCE
        ]:
            rv += RMB(self.get(format(source), 0))

        return rv

    @property
    def revokeRefundCashIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.REVOKE_REFUND_CASH), 0))

    @property
    def autoSimIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.AUTO_SIM), 0))

    @property
    def refundAutoSimIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.REFUND_AUTO_SIM), 0))

    @property
    def autoInsureIncome(self):
        return RMB(self.get(format(DEALER_INCOME_SOURCE.AUTO_SITE_INSURANCE), 0))

    @property
    def payIncome(self):
        return self.deviceIncome + self.adIncome + self.revokeRefundCashIncome

    @classmethod
    def from_stat(cls, stat, sourceIn = True): # 这里用incomeMap表示各项的统计数据实体,包括一些消费类的
        value = {
            'totalIncome': RMB(stat.get('totalIncome', 0)),
            'totalIncomeRecharge': RMB(stat.get('totalIncomeRecharge', 0)),
            'totalIncomeChargeCard': RMB(stat.get('totalIncomeChargeCard', 0)),
            'totalIncomeChargeVirtualCard': RMB(stat.get('totalIncomeChargeVirtualCard', 0)),
            'totalIncomeCount': Quantity(stat.get('totalIncomeCount', Quantity(0,'0')),'0'),
            'addedUserCount': stat.get('addedUserCount', 0)
        }
        
        if 'consumption' in stat and 'elec' in stat['consumption']:
            value.update({'totalElec':Quantity(stat['consumption'].get('elec',0),'0.00')})
            value.update({'totalElecFee':RMB(0)}) 
        
        if 'totalElec' in stat:
            value.update({'totalElec':Quantity(stat.get('totalElec',0),'0.00')})
            value.update({'totalElecFee':RMB(0)}) 
        
        if 'consumption' in stat and 'elecFee' in stat['consumption']:
            value.update({'totalElecFee':RMB(stat['consumption'].get('elecFee',0))})
            
        if 'totalElecFee' in stat or 'totalElec' in stat:
            value.update({'totalElecFee':RMB(stat.get('totalElecFee',0))})
        
        if 'totalGrossProfit' in stat:
            value.update({'totalGrossProfit':RMB(stat.get('totalGrossProfit',0))})
        else:
            if 'totalElecFee' in value and 'totalIncome' in value:
                value.update({'totalGrossProfit':RMB(value.get('totalIncome',0)-value.get('totalElecFee',0))})
        
        if 'peakUsage' in stat:
            value.update({'peakUsage': Quantity(stat.get('peakUsage', Quantity(0,'0.0')),'0.0')})
        
        if 'avOrderCount' in stat:
            value.update({'avOrderCount': Quantity(stat.get('avOrderCount', Quantity(0,'0.00')),'0.00')})
            
        if sourceIn:
            for source in DEALER_INCOME_SOURCE.choices():
                value[format(source)] = RMB(
                    get_in(["income", format(source)], stat, default = 0))

        return cls(value)
    
    @classmethod
    def translate_income_detail(cls, mapValue):
        result = []
        for key, value in mapValue.items(): 
            if key in ['totalIncome','totalIncomeCount','totalElec'] or value == RMB(0) : # 0元的直接过滤掉, 总数据也不要
                continue
            if key not in DEALER_INCOME_SOURCE_TRANSLATION: # totalIncomeCount 这种字段不需要出现
                continue
            result.append({'desc':DEALER_INCOME_SOURCE_TRANSLATION.get(key),'value':value})
        return result
    
    addValueDict = {
                    'totalIncome':'0.00', # 对应的字段名称,已经精度
                    'totalIncomeRecharge':'0.00',
                    'totalIncomeChargeCard':'0.00',
                    'totalIncomeChargeVirtualCard':'0.00',
                    'totalIncomeCount':'0',
                    'totalElec':'0.00',
                    'totalElecFee':'0.00',
                    'totalGrossProfit':'0.00',
#                     'devCount':'0'
                    }
    def add_value(self, mapValue):
        for k in IncomeMap.addValueDict.keys(): # 目前只做这几个的累加
            if k in self:
                if k in mapValue:
                    newValue = Quantity(self.get(k),IncomeMap.addValueDict.get(k)) + Quantity(mapValue.get(k),IncomeMap.addValueDict.get(k))
                    self.update({k:Quantity(newValue,IncomeMap.addValueDict.get(k))})
                else:
                    pass
            else:
                if k in mapValue:
                    self.update({k:Quantity(mapValue.get(k),IncomeMap.addValueDict.get(k))})
                else:
                    self.update({k:Quantity(0,IncomeMap.addValueDict.get(k))})
                    
    # 所有字段都进行汇总
    def add_all_value(self, mapValue):
        for k in mapValue.keys(): 
            if k in self:
                self.update({k:self.get(k)+mapValue.get(k)})
            else:
                self.update({k:mapValue.get(k)})
                    
                    
    @property
    def totalElec(self):
        return Quantity(self.get('totalElec',Quantity(0.00)),'0.00')
    
    @property
    def totalElecFee(self):
        return RMB(self.get('totalElecFee',0))
    
    @property
    def totalGrossProfit(self):
        if 'totalGrossProfit' in self:
            return RMB(self.get('totalGrossProfit',0) )
        else:
            return self.totalIncome - self.totalElecFee
    
    @property
    def peakUsage(self):
        return Quantity(self.get('peakUsage',0),'0.0')
    
    @property
    def avPeakUsage(self):
        return Quantity(self.get('avPeakUsage',0),'0.0')
    
    @property
    def avOrderCount(self):
        return Quantity(self.get('avOrderCount',0),'0.00')
posted @ 2024-12-03 11:43  vx_guanchaoguo0  阅读(11)  评论(0)    收藏  举报