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

策略模式 | 单一职责原则(Single Responsibility Principle, SRP):islenone和islentwo分别根据特定条件返回电话号码

函数式编程: '' if pd.isna(self.note1) else len(re.findall(r'\d+', self.note1))

重复代码: 当前代码重复还是太高,消除了重复的赋值操作和类似的条件分支。

python 复制代码
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'] 用到多次了。

python 复制代码
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
相关推荐
杨树与晨光1 分钟前
常用字符串方法<python>
开发语言·笔记·python
穆_清4 分钟前
python基础问题记录
python
萠哥啥都行11 分钟前
PTA:7-32 最小公倍数(递归)
java·开发语言
湖中仙人11 分钟前
【无标题】c# WEBAPI 读写表到Redis
开发语言
编程指南针22 分钟前
计算机Java项目|基于SpringBoot的作业管理系统设计与实现
java·开发语言·spring boot
在路上-正出发28 分钟前
【perl】脚本编程的一些坑&案例
java·python·perl
编码时空的诗意行者28 分钟前
深入探讨C++的高级反射机制
开发语言·c++·反射机制·内省·模板编程
码力码力我爱你30 分钟前
C++自定义智能指针
开发语言·c++·算法
橙子味冰可乐36 分钟前
input()函数——输入
linux·服务器·windows·python·tcp/ip·sqlite
Perfect—完美38 分钟前
PHP 面向对象编程(OOP)入门指南
android·开发语言·php