CodeSampleX

Ejemplo

charset-normalizer 3.4.7: from_bytes

Muestra verificada para pypi charset-normalizer 3.4.7: from_bytes. El contrato se ejecutó en python 3.12 · linux alpine/x64 · docker y pasó.

sha256:1138b526fd037ae1e2e129c918b740d1eb018eb461a65a7c6c87eefcdcdfe826

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado python 3.12 linux 24 · alpine · musl 1.2 x64 python 3.12 python pip

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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

Caso

HOW
Objetivo
verify pkg:pypi/charset-normalizer@3.4.7
Paquetes
Símbolos
  • from_bytes
Entorno
python 3.12
Creado
2026-08-20T21:45:53Z

Contrato

  1. charset_normalizer.from_bytes(data) detects encoding from raw byte sequences
  2. matches.best() returns the highest-scoring CharsetMatch object
  3. best.encoding provides the normalized encoding identifier
  4. str(best) provides the decoded unicode string

Archivos

  • PROMPT.md
  • csx.json
  • requirements.txt
  • spec.json
  • src/detector.py
  • test/contract.py

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
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.
csx.json
{"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"}
requirements.txt
charset-normalizer==3.4.7
spec.json
{
  "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"
  }
}
src/detector.py
"""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,
    }
test/contract.py
"""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())

Seeder de origen

anónimo