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

策略模式 | 单一职责原则(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
相关推荐
天才少女爱迪生12 小时前
LLVM(Low Level Virtual Machine)介绍
python·数据挖掘
碳酸的唐16 小时前
A* 工程实践全指南:从启发式设计到可视化与性能优化
python·神经网络
执尺量北斗18 小时前
[特殊字符] 基于 Qt + OpenGL 实现的入门级打砖块游戏
开发语言·qt·游戏
夏子曦18 小时前
C#内存管理深度解析:从栈堆原理到高性能编程实践
开发语言·c#
倔强青铜三19 小时前
苦练Python第64天:从零掌握多线程,threading模块全面指南
人工智能·python·面试
Q264336502320 小时前
【有源码】基于Hadoop生态的大数据共享单车数据分析与可视化平台-基于Python与大数据的共享单车多维度数据分析可视化系统
大数据·hadoop·python·机器学习·数据分析·spark·毕业设计
jiajixi20 小时前
Go 异步编程
开发语言·后端·golang
QX_hao20 小时前
【Go】--strings包
开发语言·后端·golang
计算机毕业设计木哥20 小时前
计算机毕设选题推荐:基于Hadoop和Python的游戏销售大数据可视化分析系统
大数据·开发语言·hadoop·python·信息可视化·spark·课程设计