示例
charset-normalizer 3.4.7: from_bytes
已验证示例 — pypi charset-normalizer 3.4.7: from_bytes. contract 在 python 3.12 · linux alpine/x64 · docker 上运行并通过: charset_normalizer.from_bytes(data) detects…
sha256:1138b526fd037ae1e2e129c918b740d1eb018eb461a65a7c6c87eefcdcdfe826
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
python 3.12 linux 24 · alpine · musl 1.2 x64 python 3.12 python pip
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| python 3.12 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · python@1 |
2026-08-20 |
案例
HOW- 目标
- verify pkg:pypi/charset-normalizer@3.4.7
- 符号
-
- from_bytes
- 环境
- python 3.12
- 创建时间
- 2026-08-20T21:45:53Z
契约
- charset_normalizer.from_bytes(data) detects encoding from raw byte sequences
- matches.best() returns the highest-scoring CharsetMatch object
- best.encoding provides the normalized encoding identifier
- str(best) provides the decoded unicode string
文件
- PROMPT.md
- csx.json
- requirements.txt
- spec.json
- src/detector.py
- test/contract.py
源代码
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: verify pkg:pypi/charset-normalizer@3.4.7
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:pypi/charset-normalizer@3.4.7
Demonstrate these symbols/APIs:
- from_bytes
Constraints:
- executionContext: node
Required runtime conditions:
- ecosystem: npm
- language: javascript
- moduleSystem: cjs
- runtime: node@22.23.2
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:8cb79b13b8a0f08f2638c06fb4c6c250fc5c6aa17b8390fee3d027a2af2d894c","contract":["charset_normalizer.from_bytes(data) detects encoding from raw byte sequences","matches.best() returns the highest-scoring CharsetMatch object","best.encoding provides the normalized encoding identifier","str(best) provides the decoded unicode string"],"goal":"verify pkg:pypi/charset-normalizer@3.4.7","kind":"HOW","packages":["pkg:pypi/charset-normalizer@3.4.7"],"schemaVersion":1,"symbols":["from_bytes"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"pypi","language":"python","libc":"musl","libcVersion":"1.2","os":"linux","osVersionBucket":"24","packageManager":"pip","runtime":"python","runtimeVersion":"3.12","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/charset-normalizer@3.4.7"],"schemaVersion":1,"subject":"pkg:pypi/charset-normalizer@3.4.7","symbols":["from_bytes"],"verifierAdapter":"python@1"}
charset-normalizer==3.4.7
{
"schemaVersion": 1,
"goal": "verify pkg:pypi/charset-normalizer@3.4.7",
"kind": "HOW",
"packages": [
"pkg:pypi/charset-normalizer@3.4.7"
],
"symbols": [
"from_bytes"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"runtime": "node@22.23.2"
}
}
"""Charset detector demonstration using charset_normalizer."""
import charset_normalizer
def detect_charset(data: bytes) -> dict:
"""Detect character encoding and decode input byte sequence.
Args:
data: Raw byte string to detect encoding for.
Returns:
dict containing 'encoding', 'confidence', 'text', and 'chaos'.
"""
matches = charset_normalizer.from_bytes(data)
best = matches.best()
if best is None:
return {
"encoding": None,
"confidence": 0.0,
"text": "",
"chaos": 1.0,
}
return {
"encoding": best.encoding,
"confidence": 1.0 - best.chaos,
"text": str(best),
"chaos": best.chaos,
}
"""Contract verification test for charset_normalizer.from_bytes."""
import os
import sys
# Ensure repository root is on sys.path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import charset_normalizer
from src.detector import detect_charset
def test_from_bytes_utf8():
sample_text = "Bonjour le monde! Ça va bien? Café & thé."
raw_bytes = sample_text.encode("utf-8")
matches = charset_normalizer.from_bytes(raw_bytes)
assert len(matches) > 0, "Expected at least one match"
best = matches.best()
assert best is not None, "Expected a valid best match"
assert best.encoding == "utf_8", f"Expected utf_8, got {best.encoding}"
assert str(best) == sample_text, f"Decoded text mismatch: {str(best)}"
assert best.chaos == 0.0, f"Expected 0.0 chaos score, got {best.chaos}"
def test_from_bytes_ascii():
sample_text = "Simple ASCII text payload"
raw_bytes = sample_text.encode("ascii")
matches = charset_normalizer.from_bytes(raw_bytes)
assert len(matches) > 0, "Expected at least one match"
best = matches.best()
assert best is not None, "Expected a valid best match"
assert best.encoding == "ascii", f"Expected ascii, got {best.encoding}"
assert str(best) == sample_text, f"Decoded text mismatch: {str(best)}"
def test_detector_helper():
sample_text = "Guten Tag! Schönen Tag noch."
raw_bytes = sample_text.encode("utf-8")
result = detect_charset(raw_bytes)
assert result["encoding"] == "utf_8", f"Unexpected encoding: {result['encoding']}"
assert result["text"] == sample_text, f"Unexpected text: {result['text']}"
assert result["chaos"] == 0.0, f"Unexpected chaos score: {result['chaos']}"
def main():
test_from_bytes_utf8()
test_from_bytes_ascii()
test_detector_helper()
print("Contract tests passed successfully.")
return 0
if __name__ == "__main__":
sys.exit(main())
原始种子者
匿名