惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
N
News | PayPal Newsroom
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
S
Secure Thoughts
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 【当耐特】
博客园 - 聂微东
S
Securelist
宝玉的分享
宝玉的分享
爱范儿
爱范儿
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AI
AI
Security Latest
Security Latest
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Last Watchdog
The Last Watchdog
月光博客
月光博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Webroot Blog
Webroot Blog
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
N
News and Events Feed by Topic
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
S
Security Affairs
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
罗磊的独立博客
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
T
Tailwind CSS Blog
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - ®Geovin Du Dream Park™

go: Functional Options Pattern go:Timing Functions Pattern python: Timing Functions Pattern go: Steady-State Pattern python: Steady-State Pattern go: Handshaking Pattern python: Handshaking Pattern go: Fail-Fast Pattern python: Fail-Fast Pattern I go: Deadline Pattern python: Deadline Pattern go: Circuit-Breaker Pattern python: Circuit-Breaker Pattern go: Bulkheads Pattern python: Bulkheads Pattern go: Push & Pull Pattern python: Push & Pull Pattern python: Publish/Subscribe Pattern II go: Publish/Subscribe Pattern python: Publish/Subscribe Pattern go: Futures & Promises Pattern python: Futures & Promises Pattern go: Worker Pool Pattern go:Pipeline Pattern python: Worker Pool Pattern python: Pipeline Pattern go: Fan-Out Pattern python: Fan-Out Pattern go: Fan-In Pattern python: Fan-In Pattern Fan-In go: Producer Consumer  Pattern python: Producer Consumer Pattern go: Parallelism Pattern python: Parallelism Pattern go: Reactor Pattern python: Reactor Pattern go: Generators Pattern python: speech to text offline python: Generators Pattern go: Coroutines Pattern python:Coroutines Pattern go: Broadcast Pattern python: Broadcast Pattern python: Bounded Parallelism Pattern go: Bounded Parallelism Pattern python: N-Barrier Pattern go: N-Barrier Pattern python: Semaphore Pattern go: Semaphore Pattern python: Read-Write Lock Pattern go: Read-Write Lock Pattern python: Monitor Pattern go: Monitor Pattern python: Mutex Pattern go: Lock/Mutex Pattern Python: Condition Variable Pattern go:Condition Variable Pattern python: Registry Pattern go: Interpreter Pattern go: Registry Pattern go: Memento Pattern go: Command Pattern go: State Pattern go:Template Method Pattern go: Strategy Pattern go: Visitor Pattern go: Observer Pattern go: Mediator Pattern go: Iterator Pattern go: Chain of Responsibility Pattern go: Proxy Pattern go:Decorator Pattern go: Facade Pattern go: Flyweight Pattern go: Composite Pattern go: Singleton Pattern go: Prototype Pattern go: Bridge Pattern go: Adapter Pattern go: Builder Pattern 密码进行加盐哈希 using CSharp,Python,Go,Java go: Abstract Factory Pattern go: Model,Interface,DAL ,Factory,BLL using mysql go: Simple Factory Pattern go: Factory Method Pattern go: goLang 在Windows环境搭建Go语言开发环境 CSharp: Parallel Extensions cpp: class python: 蝴蝶跟随鼠标 javascript: 中国历史人物热力分布图using echart javascript: 中国历史人物热力图 python: 初养龙虾微信纯文字自动回复using workBuddy python: object 入门 python: Factory Method Pattern python: Simple Factory Pattern python: Abstract Factory Pattern 区域化 代码 python: Null Object Pattern python: Builder Pattern python: Adapter Pattern
python: Interpreter Pattern
®Geovin Du Dream Park™ · 2026-04-28 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

 珠宝行业适用场景:

  • 珠宝价格规则计算(黄金克价 + 工费 + 钻石溢价)
  • 珠宝真伪 / 等级校验规则(金纯度≥99.9% + 钻石净度≥VS1)
  • 珠宝筛选规则(按材质、品类、价格区间快速匹配)

珠宝行业完整实例

我们实现:珠宝价格 / 属性规则解释器

支持规则:

  1. 基础金价计算:黄金价格 = 克重 × 基础金价 + 工费
  2. 组合规则:黄金纯度达标 且 钻石净度达标
  3. 筛选规则:价格 ≥ 5000 且 材质是 铂金
# 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
'''


'''
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)}")

输出:

image

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)