Team Ai
Apppublic

Forrest99/codesearchBase

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
validate_data.py45 linesDownload Raw Back to root
1def validate_inputs(snippets):2    """数据验证工具"""3    errors = []4    valid_data = []5    6    for idx, s in enumerate(snippets):7        # 类型检查8        if not isinstance(s, str):9            errors.append(f"索引 {idx}: 类型错误 ({type(s)}) → 已转换")10            s = str(s)11            12        # 清理数据13        cleaned = s.replace("...", "").strip()14        15        # 有效性检查16        if len(cleaned) < 10:  # 最小代码长度17            errors.append(f"索引 {idx}: 代码过短 ({len(cleaned)} 字符)")18            continue19            20        valid_data.append(cleaned)21    22    # 输出报告23    print(f"验证结果:\n成功 {len(valid_data)} 条\n错误 {len(errors)} 条")24    for err in errors[:3]:  # 显示前3个错误25        print(f"  - {err}")26    if len(errors) > 3:27        print(f"  ...(共 {len(errors)} 个错误)")28    29    return valid_data30 31if __name__ == "__main__":32    # 测试数据33    test_data = [34        "def example(): pass",35        12345,  # 错误类型36        "print(...)",  # 包含...37        "   ",  # 空字符串38        """def valid_func():39            return '正确代码'"""40    ]41    42    validated = validate_inputs(test_data)43    print("\n有效数据示例:")44    for i, data in enumerate(validated[:2]):45        print(f"[{i}] {data[:50]}...")