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

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园_首页
博客园 - 【当耐特】
V
Visual Studio Blog
博客园 - 叶小钗
月光博客
月光博客
美团技术团队
J
Java Code Geeks
小众软件
小众软件
Y
Y Combinator Blog
博客园 - Franky
Martin Fowler
Martin Fowler
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG

Python

没人觉得 cursor 的模型思考很慢嘛,经常思考卡死要手动停止再继续 - V2EX [原创工具] hot-list 多平台热榜聚合+AI 分析系统,一键部署,开源免费 - V2EX 开发了一个查看 conda 镜像工作情况的项目,已经开源 - V2EX JD 评论风控问题 - V2EX [开源分享] QQ 空间 Flash 老游戏-红警大战坦克风暴-保活与每日脚本 - V2EX TG api 我用 google Voice 老申请失败 用 GPT5.6 填了之前的坑:在线的 Python 编辑器和运行终端 为什么这段 cuda 的并行前缀算法不会数据竞争 怎么搞定纯 Python 代码解码 jpg 图片,要求无外部依赖 使用 kkRepo 搭建 Python PyPI 私服 Python WebSocket 长连接到底怎么写才稳? 小白求推荐人工智能学习的网课 humanize-text 一个开源的 AI 文本拟人化工具集 9.9 元起!跨境卖家疯抢的纯净住宅 IP 辣椒 HTTP,到底有多香? 从会议录音到知识库全自动:我把数百段录音做成了可 RAG 问答的 Wiki(附开源代码) CodexSaver:在不让 Codex 变笨的前提下,让它更便宜。 [开源] 我正在用 Python 复刻经典游戏红色警戒 2 有准确绘制缠论中枢的 Python 代码借鉴么 做了一个面向张量计算的语言,可从 Python 调用,支持显式索引和自动求导 把电脑伪装成电视,用 DLNA 投屏拿到视频号直播流地址 爬虫开发工作中,你们是如何基于 AI 进行提效的? 发现 Python 一个有意思的小特性,发现很合适搞成面试题。问了 AI 都不行:),欢迎来挑战~ 大型 Python 开源项目都不会对变量进行类型注解? 使用 pycharm 开发 Python ,自定义代码风格,并实时提示 创造了 uv 的 Astral 公司被 OpenAI 收购 慎用 PyCharm Remote Development 功能 Python 3.15 JIT 的最新进展,已经有大概 5%的性能提升了 [开源] 做了个 feishu-docx,把飞书知识库变成 AI 更容易读写和管理的内容源,方便给 Agent 用 开源项目: clawOS 解决 openclaw 文件系统,一键安装 我用免费 A 股数据做了几个工具,欢迎一起开源!
哪位朋友帮忙用 mac 测试下我的 Python 脚本
gosky · 2026-07-10 · via Python

把输出贴给我
如果有异常,如果能附带帮忙 fix 更好

import signal
import subprocess
from typing import OrderedDict

APP_NAME = "Hello"


class CalledProcessError(subprocess.CalledProcessError):
    """
    `subprocess.CalledProcessError` does not print `stderr` and `output`.
    """

    def __str__(self):
        if self.returncode and self.returncode < 0:
            try:
                return "Command '%s' died with %r." % (
                    self.cmd,
                    signal.Signals(-self.returncode),
                )
            except ValueError:
                return "Command '%s' died with unknown signal %d." % (
                    self.cmd,
                    -self.returncode,
                )
        else:
            return (
                "Command '%s' returned non-zero exit status %d. stderr: '%s'. output: '%s'"
                % (self.cmd, self.returncode, self.stderr.strip(), self.output.strip())
            )


class DecryptError(Exception): ...


class DarwinCryptex:
    scheme = "security"

    def encrypt(self, attrs: OrderedDict[str, str], plaintext: str) -> str:
        account = "-".join([f"{k}:{v}" for k, v in attrs.items()])
        cmd = [
            "security",
            "add-generic-password",
            "-a",
            account,
            "-s",
            APP_NAME,
            "-w",
            plaintext,
            "-U",
        ]
        r = subprocess.run(
            cmd,
            capture_output=True,
            encoding="utf-8",
            text=True,
            timeout=5,
        )
        if r.returncode != 0:
            raise CalledProcessError(
                r.returncode, cmd, output=r.stdout, stderr=r.stderr
            )
        return "******"

    def decrypt(self, attrs: OrderedDict[str, str], ciphertext: str) -> str:
        account = "-".join([f"{k}:{v}" for k, v in attrs.items()])
        cmd = [
            "security",
            "find-generic-password",
            "-a",
            account,
            "-s",
            APP_NAME,
            "-w",
        ]
        r = subprocess.run(
            cmd,
            capture_output=True,
            encoding="utf-8",
            text=True,
            timeout=5,
        )
        if r.returncode == 0:
            return r.stdout.strip()
        elif r.returncode == 44:
            raise DecryptError()
        else:
            raise CalledProcessError(
                r.returncode, cmd, output=r.stdout, stderr=r.stderr
            )

    def clean(self, attrs: OrderedDict[str, str]):
        account = "-".join([f"{k}:{v}" for k, v in attrs.items()])
        cmd = ["security", "delete-generic-password", "-a", account, "-s", APP_NAME]
        r = subprocess.run(
            cmd, capture_output=True, encoding="utf-8", text=True, timeout=5
        )
        if r.returncode not in (0, 44):
            raise CalledProcessError(
                r.returncode, cmd, output=r.stdout, stderr=r.stderr
            )


cryptex = DarwinCryptex()
attrs: OrderedDict[str, str] = OrderedDict(
    [
        ("app", "test"),
        ("section", "hello"),
        ("key", "你好"),
    ]
)
plaintext = "Hello world! 你好,世界!"

try:
    ciphertext = cryptex.encrypt(attrs, plaintext)
    decrypted = cryptex.decrypt(attrs, ciphertext)
    assert decrypted == plaintext

    mismatch_attrs = attrs.copy()
    mismatch_attrs["needless"] = "有毒"
    try:
        cryptex.decrypt(mismatch_attrs, ciphertext)
    except DecryptError:
        pass
    else:
        raise AssertionError("Expected DecryptError")
finally:
    cryptex.clean(attrs)