CodeSampleX

示例

sqlalchemy 2.0.51: sql.elements.BinaryExpression

已验证示例 — pypi sqlalchemy 2.0.51: sql.elements.BinaryExpression. contract 在 python 3.12 · linux debian/x64 · docker 上运行并通过:…

sha256:8fe1f5e5eea816685d88874fea42a289ec038e01bd2bcf9cba005b7a5a7f580a

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 pip

验证运行环境

环境 契约 阶段 运行日期
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-06

案例

HOW
目标
verify sqlalchemy.sql.elements.BinaryExpression in pkg:pypi/sqlalchemy@2.0.51
包
符号
  • sqlalchemy.sql.elements.BinaryExpression
创建时间
2026-09-06T23:57:29Z

契约

  1. sqlalchemy.sql.elements.BinaryExpression represents binary operations between SQL column expressions with left operand, right operand, and operator
  2. sqlalchemy.sql.elements.BinaryExpression can be instantiated directly with left element, right element, and an operator callable
  3. sqlalchemy.sql.elements.BinaryExpression supports bitwise inversion to produce a negated binary expression with corresponding inverted operator
  4. sqlalchemy.sql.elements.BinaryExpression compiles to SQL expression strings with parameter bindings and integrates into SELECT query WHERE clauses

文件

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

下载源代码构件 (tar.gz)

源代码

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 sqlalchemy.sql.elements.BinaryExpression in pkg:pypi/sqlalchemy@2.0.51
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/sqlalchemy@2.0.51
Demonstrate these symbols/APIs:
  - sqlalchemy.sql.elements.BinaryExpression

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:2afd88ba43fdd366eba59ff07fa4060bb91e03388f8dd2f0c114bdace09959f0","contract":["sqlalchemy.sql.elements.BinaryExpression represents binary operations between SQL column expressions with left operand, right operand, and operator","sqlalchemy.sql.elements.BinaryExpression can be instantiated directly with left element, right element, and an operator callable","sqlalchemy.sql.elements.BinaryExpression supports bitwise inversion to produce a negated binary expression with corresponding inverted operator","sqlalchemy.sql.elements.BinaryExpression compiles to SQL expression strings with parameter bindings and integrates into SELECT query WHERE clauses"],"goal":"verify sqlalchemy.sql.elements.BinaryExpression in pkg:pypi/sqlalchemy@2.0.51","kind":"HOW","packages":["pkg:pypi/sqlalchemy@2.0.51"],"schemaVersion":1,"symbols":["sqlalchemy.sql.elements.BinaryExpression"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"pypi","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"pip","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/sqlalchemy@2.0.51"],"schemaVersion":1,"subject":"pkg:pypi/sqlalchemy@2.0.51","symbols":["sqlalchemy.sql.elements.BinaryExpression"],"verifierAdapter":"python@1"}
requirements.txt
sqlalchemy==2.0.51
sample.py
"""Demonstration of SQL binary expressions using sqlalchemy.sql.elements.BinaryExpression."""

from typing import Any, Callable, Tuple
import operator
import sqlalchemy as sa
from sqlalchemy.sql.elements import BinaryExpression
from sqlalchemy.dialects import sqlite


def create_binary_comparison(
    column_name: str,
    value: Any,
) -> BinaryExpression:
    """Create a BinaryExpression using a column comparison operator."""
    col = sa.column(column_name)
    expr = col == value
    if not isinstance(expr, BinaryExpression):
        raise TypeError(f"Expected BinaryExpression, got {type(expr)}")
    return expr


def instantiate_binary_expression(
    column_name: str,
    value: Any,
    op: Callable[..., Any] = operator.gt,
) -> BinaryExpression:
    """Explicitly instantiate a BinaryExpression with left, right, and operator."""
    col = sa.column(column_name)
    val = sa.literal(value)
    return BinaryExpression(col, val, op)


def negate_binary_expression(expr: BinaryExpression) -> BinaryExpression:
    """Invert a BinaryExpression using the bitwise NOT operator."""
    negated = ~expr
    if not isinstance(negated, BinaryExpression):
        raise TypeError(f"Expected BinaryExpression after negation, got {type(negated)}")
    return negated


def inspect_binary_expression(expr: BinaryExpression) -> Tuple[Any, Any, Callable[..., Any]]:
    """Inspect the operands and operator of a BinaryExpression."""
    return expr.left, expr.right, expr.operator


def compile_binary_expression(expr: BinaryExpression, literal_binds: bool = True) -> str:
    """Compile a BinaryExpression to SQL string representation."""
    return str(expr.compile(dialect=sqlite.dialect(), compile_kwargs={"literal_binds": literal_binds}))


def build_filtered_query(table_name: str, expr: BinaryExpression) -> str:
    """Build and compile a SELECT statement filtered by the BinaryExpression."""
    table = sa.table(table_name, expr.left)
    stmt = sa.select(table).where(expr)
    return str(stmt.compile(dialect=sqlite.dialect(), compile_kwargs={"literal_binds": True}))
spec.json
{
  "schemaVersion": 1,
  "goal": "verify sqlalchemy.sql.elements.BinaryExpression in pkg:pypi/sqlalchemy@2.0.51",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/sqlalchemy@2.0.51"
  ],
  "symbols": [
    "sqlalchemy.sql.elements.BinaryExpression"
  ]
}
test/contract.py
"""Contract verification tests for sqlalchemy.sql.elements.BinaryExpression."""

import os
import sys
import operator

# Ensure sample root directory is in sys.path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from sqlalchemy.sql.elements import BinaryExpression, BindParameter, ColumnClause
from sqlalchemy.dialects import sqlite
import sample


def test_binary_expression_representation() -> None:
    """Assertion 1: sqlalchemy.sql.elements.BinaryExpression represents binary operations between SQL column expressions with left operand, right operand, and operator."""
    expr = sample.create_binary_comparison("status", "active")

    assert isinstance(expr, BinaryExpression)
    left, right, op = sample.inspect_binary_expression(expr)

    assert isinstance(left, ColumnClause)
    assert left.name == "status"
    assert isinstance(right, BindParameter)
    assert right.value == "active"
    assert op is operator.eq


def test_direct_instantiation() -> None:
    """Assertion 2: sqlalchemy.sql.elements.BinaryExpression can be instantiated directly with left element, right element, and an operator callable."""
    gt_expr = sample.instantiate_binary_expression("score", 90, operator.gt)

    assert isinstance(gt_expr, BinaryExpression)
    assert gt_expr.left.name == "score"
    assert gt_expr.right.value == 90
    assert gt_expr.operator is operator.gt

    add_expr = sample.instantiate_binary_expression("score", 10, operator.add)
    assert isinstance(add_expr, BinaryExpression)
    assert add_expr.operator is operator.add


def test_negation_inversion() -> None:
    """Assertion 3: sqlalchemy.sql.elements.BinaryExpression supports bitwise inversion to produce a negated binary expression with corresponding inverted operator."""
    expr = sample.create_binary_comparison("status", "active")
    negated = sample.negate_binary_expression(expr)

    assert isinstance(negated, BinaryExpression)
    assert negated.operator is operator.ne
    assert negated.left.name == "status"
    assert negated.right.value == "active"

    # Double negation restores equality semantics
    double_negated = sample.negate_binary_expression(negated)
    assert isinstance(double_negated, BinaryExpression)
    assert double_negated.operator is operator.eq


def test_compilation_and_select_integration() -> None:
    """Assertion 4: sqlalchemy.sql.elements.BinaryExpression compiles to SQL expression strings with parameter bindings and integrates into SELECT query WHERE clauses."""
    expr = sample.create_binary_comparison("status", "active")

    # Compile with literal binds
    compiled_literal = sample.compile_binary_expression(expr, literal_binds=True)
    assert compiled_literal == "status = 'active'"

    # Compile with parameter placeholder
    compiled_param = str(expr.compile(dialect=sqlite.dialect()))
    assert compiled_param == "status = ?"

    # Query integration in SELECT statement
    query_str = sample.build_filtered_query("users", expr)
    assert "WHERE users.status = 'active'" in query_str


def main() -> None:
    test_binary_expression_representation()
    test_direct_instantiation()
    test_negation_inversion()
    test_compilation_and_select_integration()
    print("All sqlalchemy.sql.elements.BinaryExpression contract assertions passed successfully.")


if __name__ == "__main__":
    main()

原始种子者

匿名