策略模式与函数式编程应用

发布于:2024-06-28 ⋅ 阅读:(15) ⋅ 点赞:(0)

策略模式 | 单一职责原则(Single Responsibility Principle, SRP):islenone和islentwo分别根据特定条件返回电话号码
函数式编程: ‘’ if pd.isna(self.note1) else len(re.findall(r’\d+', self.note1))
重复代码: 当前代码重复还是太高,消除了重复的赋值操作和类似的条件分支。

class hmtq:
    def __init__(self, phone1, phone2, note1, note3, phonegg):
        self.phone1 = phone1
        self.phone2 = phone2
        self.note1 = note1
        self.note3 = note3
        self.phonegg = phonegg
        self.dict_len = {}

    def islen(self):

        self.dict_len['b'] = '' if pd.isna(self.note1) else len(re.findall(r'\d+', self.note1))
        self.dict_len['c'] =  '' if pd.isna(self.note3) else len(re.findall(r'\d+', self.note3))
        self.dict_len['a'] = False if pd.isna(self.phonegg)  else self.phonegg
        return self.dict_len

    def islenone(self, indata, phone):
        return phone if int(re.findall(r'\d+', indata)[0]) == 1 else ''

    def islentwo(self, indata, phone, phone2):
        return phone if int(re.findall(r'\d+', indata)[0]) == 1 else \
            (phone2 if int(re.findall(r'\d+', indata)[1]) == 1 else '')
    def hm(self):
        self.islen()
        if self.dict_len['a']==True:
            a = self.phonegg
        elif self.dict_len['b'] == 1:
            a = self.islenone(self.note1, self.phone1)
        elif self.dict_len['b'] == 2:
            a = self.islentwo(self.note1, self.phone1, self.phone2)
        else:
            a=''
        if a !='':
            pass
        elif  self.dict_len['c'] == 1:
            a = self.islenone(self.note3, self.phone1)
        elif  self.dict_len['c'] == 2:
            a = self.islentwo(self.note3, self.phone1, self.phone2)
        else:
            a = ''
        return a

重复代码改进,hm方法使用。hm主处理方法,根据条件选择电话号码,也用上三元运算。
把重复的代码能抽取出来,self.dict_len[‘c’] 和self.dict_len[‘b’] 用到多次了。

class hmtqupdate:
    def __init__(self, phone1, phone2, note1, note3, phonegg):
        """
        初始化hmtq类实例,设置电话号码和笔记信息。

        参数:
        - phone1, phone2: 电话号码1和2
        - note1, note3: 笔记1和3,可能包含数字
        - phonegg: 特殊电话号码标记
        """
        self.phone1 = phone1
        self.phone2 = phone2
        self.note1 = note1
        self.note3 = note3
        self.phonegg = phonegg
        self.dict_len = self.islen()  # 初始化时直接计算dict_len

    def islen(self):
        """
        计算note中数字的数量,并处理phonegg的逻辑。
        """
        self.dict_len = {
            'b': len(re.findall(r'\d+', self.note1)) if not pd.isna(self.note1) else '',
            'c': len(re.findall(r'\d+', self.note3)) if not pd.isna(self.note3) else '',
            'a': not pd.isna(self.phonegg)
        }
        return self.dict_len

    def islenone(self, indata, phone):
        """如果indata中的第一个数字为1,则返回phone。"""
        return phone if re.findall(r'\d+', indata) and int(re.findall(r'\d+', indata)[0]) == 1 else ''

    def islentwo(self, indata, phone, phone2):
        """根据indata中的数字决定返回哪个电话号码。"""
        digits = re.findall(r'\d+', indata)
        if digits and int(digits[0]) == 1:
            return phone
        if len(digits) > 1 and int(digits[1]) == 1:
            return phone2
        return ''

    def hm(self):
        """
        主处理方法,根据条件选择电话号码。
        """
        # 直接尝试获取'a'源的电话号码
        a = self.phonegg if self.dict_len['a'] else ''

        # 如果'a'源未成功获取,尝试'b'源
        if not a and self.dict_len['b']:
            a = self.islenone(self.note1, self.phone1) if self.dict_len['b'] == 1 else \
                self.islentwo(self.note1, self.phone1, self.phone2)

        # 最后尝试'c'源
        if not a and self.dict_len['c']:
            a = self.islenone(self.note3, self.phone1) if self.dict_len['c'] == 1 else \
                self.islentwo(self.note3, self.phone1, self.phone2)

        return a