news 2026/4/30 2:16:41

python: Interpreter Pattern

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python: Interpreter Pattern

项目结构:

# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:43 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : jewelry_context.py from dataclasses import dataclass @dataclass class JewelryContext: """ 珠宝上下文:存储所有珠宝属性 """ name: str = "" material: str = "" weight: float = 0.0 purity: float = 0.0 price: float = 0.0 clarity: str = ""
# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:50 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : base_expression.py from abc import ABC, abstractmethod from Interpreter.context.jewelry_context import JewelryContext class BaseExpression(ABC): """ 职责:抽象表达式(所有规则必须实现) """ @abstractmethod def interpret(self, context: JewelryContext) -> bool | float: """ 解释规则:返回判断结果 或 计算结果 :param context: :return: """ pass # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:54 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : calculate_exp.py from Interpreter.expression.base_expression import BaseExpression from Interpreter.context.jewelry_context import JewelryContext class GoldPriceExpression(BaseExpression): """ 原子规则:黄金价格计算(可扩展钻石 / 宝石) """ def __init__(self, base_price: float, craft_fee: float): """ :param base_price: :param craft_fee: """ self.base_price = base_price self.craft_fee = craft_fee def interpret(self, context: JewelryContext) -> float: """ :param context: :return: """ total = context.weight * self.base_price + self.craft_fee return round(total, 2) # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:52 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : compare_exp.py from Interpreter.expression.base_expression import BaseExpression from Interpreter.context.jewelry_context import JewelryContext class GreaterThanExpression(BaseExpression): """ 原子规则:数值比较 >= """ def __init__(self, field: str, threshold: float): """ :param field: :param threshold: """ self.field = field self.threshold = threshold def interpret(self, context: JewelryContext) -> bool: """ :param context: :return: """ value = getattr(context, self.field, 0.0) return value >= self.threshold # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:53 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : material_exp.py from Interpreter.expression.base_expression import BaseExpression from Interpreter.context.jewelry_context import JewelryContext class MaterialMatchExpression(BaseExpression): """ 原子规则:材质匹配 """ def __init__(self, material: str): """ :param material: """ self.material = material def interpret(self, context: JewelryContext) -> bool: """ :param context: :return: """ return context.material == self.material # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:55 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : and_exp.py from Interpreter.expression.base_expression import BaseExpression from Interpreter.context.jewelry_context import JewelryContext class AndExpression(BaseExpression): """ 组合规则:AND """ def __init__(self, exp1: BaseExpression, exp2: BaseExpression): """ :param exp1: :param exp2: """ self.exp1 = exp1 self.exp2 = exp2 def interpret(self, context: JewelryContext) -> bool: """ :param context: :return: """ return self.exp1.interpret(context) and self.exp2.interpret(context) # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:55 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : or_exp.py from Interpreter.expression.base_expression import BaseExpression from Interpreter.context.jewelry_context import JewelryContext class OrExpression(BaseExpression): """ 组合规则:OR """ def __init__(self, exp1: BaseExpression, exp2: BaseExpression): """ :param exp1: :param exp2: """ self.exp1 = exp1 self.exp2 = exp2 def interpret(self, context: JewelryContext) -> bool: """ :param context: :return: """ return self.exp1.interpret(context) or self.exp2.interpret(context)
# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 21:56 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : jewelry_rule_service.py from Interpreter.context.jewelry_context import JewelryContext from Interpreter.expression.base_expression import BaseExpression class JewelryRuleService: """ 业务服务层:对外提供规则解释能力(解耦调用方) """ @staticmethod def evaluate(expression: BaseExpression, context: JewelryContext) -> bool | float: """ 执行规则解释 :param expression: :param context: :return: """ return expression.interpret(context)

调用:

# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Interpreter Pattern 解释器模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/28 22:01 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : InterpreterBll.py ''' Interpreter / ├── cmd/ │ └── main.go ├── internal/ # 内部业务包(外部不可引用) │ ├── context/ # 上下文环境(变量容器) │ │ └── context.go │ ├── expression/ # 解释器表达式核心(终结符+非终结符) │ │ └── expr.go │ ├── parser/ # 语法解析器 │ │ └── parser.go │ └── service/ # 业务服务层 │ └── price_service.go └── go.mod ''' from Interpreter.context.jewelry_context import JewelryContext from Interpreter.expression.terminal.compare_exp import GreaterThanExpression from Interpreter.expression.terminal.material_exp import MaterialMatchExpression from Interpreter.expression.terminal.calculate_exp import GoldPriceExpression from Interpreter.expression.non_terminal.and_exp import AndExpression from Interpreter.service.jewelry_rule_service import JewelryRuleService class InterpreterBll(object): """ """ def demo(self): """ :return: """ # 1. 构建珠宝上下文 jewelry = JewelryContext( name="999足金戒指", material="黄金", weight=5.2, purity=99.9, clarity="VS1" ) # 2. 规则服务 service = JewelryRuleService() # ------------------- 规则1:计算黄金价格 ------------------- price_exp = GoldPriceExpression(base_price=450, craft_fee=150) price = service.evaluate(price_exp, jewelry) print(f"售价:{price} 元") # ------------------- 规则2:高品质黄金 ------------------- high_purity = GreaterThanExpression("purity", 99.9) is_gold = MaterialMatchExpression("黄金") high_quality_exp = AndExpression(high_purity, is_gold) result = service.evaluate(high_quality_exp, jewelry) print(f"是否高品质黄金:{result}") # ------------------- 规则3:高端珠宝 ------------------- platinum = JewelryContext(material="铂金", price=6800) high_price = GreaterThanExpression("price", 5000) is_platinum = MaterialMatchExpression("铂金") filter_exp = AndExpression(high_price, is_platinum) print(f"是否高端珠宝:{service.evaluate(filter_exp, platinum)}")

输出:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/30 2:11:33

IgH EtherCAT 从入门到精通:第 30 章 实战:高可用 EtherCAT 系统设计

第 30 章 实战:高可用 EtherCAT 系统设计 导读摘要:在生产环境中,EtherCAT 系统必须具备高可用性——单点故障不能导致整条产线停机。本章将从冗余链路架构设计出发,讲解链路状态监控 API、热插拔与自动重配置机制、故障诊断策略以及性能监控与报警集成,帮助你构建可靠的工…

作者头像 李华
网站建设 2026/4/30 2:06:58

私教服务 | “别一上来就撸测试平台,先想清楚这3个问题”

关注 霍格沃兹测试学院公众号,回复「资料」, 领取人工智能测试开发技术合集一个想转测开的售前同学,被我拉回了正轨01 “老师,我想写一个测试平台”语音接通的时候,对面的声音带着一点犹豫。“老师,我现在的问题是——…

作者头像 李华
网站建设 2026/4/30 1:58:24

001. MSP430G2553 入门总述:从零开始学习这颗单片机

001. MSP430G2553 入门总述:从零开始学习这颗单片机 建议文件名:001-msp430g2553-overview.md## 学习目标读完本篇后,你将能够:1. 知道 MSP430G2553 是什么,以及它为什么适合单片机入门学习。2. 了解开始学习前需要准备…

作者头像 李华
网站建设 2026/4/30 1:49:31

Layerdivider终极指南:一键将单张图片智能分层为可编辑PSD文件

Layerdivider终极指南:一键将单张图片智能分层为可编辑PSD文件 【免费下载链接】layerdivider A tool to divide a single illustration into a layered structure. 项目地址: https://gitcode.com/gh_mirrors/la/layerdivider 你是否曾为无法修改单张图片中…

作者头像 李华
网站建设 2026/4/30 1:48:29

学校党建管理系统:党建活动从筹备到总结全程数字化

在信息技术飞速发展的当下,学校党建管理系统帮助党组织成功破解传统党建难题,尤其是在组织活动管理方面,将学校党建活动从筹备到总结的各个环节紧密串联起来,让党建活动开展变得高效又便捷。活动创建与下发校党组工作人员通过学校…

作者头像 李华
网站建设 2026/4/30 1:45:37

云原生技术体系解析

云原生技术体系解析 随着数字化转型的加速,云原生技术已成为企业构建现代化应用的核心架构。它通过容器化、微服务、DevOps等关键技术,实现应用的弹性扩展、高效运维和快速迭代。本文将深入解析云原生技术体系的核心组成部分,帮助读者理解其…

作者头像 李华