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

策略模式 | 单一职责原则(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
相关推荐
冷雨夜中漫步2 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴2 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再2 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
m0_736919104 小时前
C++代码风格检查工具
开发语言·c++·算法
喵手4 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934734 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy4 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖5 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472465 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
肖永威6 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos