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

策略模式 | 单一职责原则(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
相关推荐
索迪迈科技31 分钟前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
Zz_waiting.1 小时前
Javaweb - 14.6 - Vue3 数据交互 Axios
开发语言·前端·javascript·vue·axios
萌新小码农‍1 小时前
Java分页 Element—UI
java·开发语言·ui
大佬,救命!!!1 小时前
整理python快速构建数据可视化前端的Dash库
python·信息可视化·学习笔记·dash·记录成长
孔丘闻言1 小时前
python调用mysql
android·python·mysql
Tiger_shl1 小时前
【.Net技术栈梳理】03-核心框架与运行时(异常处理)
开发语言·.net
再睡亿分钟!1 小时前
Spring MVC 的常用注解
java·开发语言·spring boot·spring
Teletele-Lin1 小时前
Miniconda安装与VSCode搭建远程Python、Jupyter开发环境
vscode·python·jupyter·环境配置·远程开发
至此流年莫相忘2 小时前
设计模式:策略模式
设计模式·策略模式
MChine慕青2 小时前
顺序表与单链表:核心原理与实战应用
linux·c语言·开发语言·数据结构·c++·算法·链表