脚本编码规范【免费下载链接】ops-sparse本项目是CANN提供的高性能稀疏矩阵计算的算子库专注于优化稀疏矩阵的计算效率。项目地址: https://gitcode.com/cann/ops-sparse适用于 skill 目录下的所有代码文件.sh、.py1. 版权头强制所有代码文件必须在文件开头添加以下版权头。Shell 脚本.sh#!/bin/bash # ---------------------------------------------------------------------------------------------------------- # Copyright (c) 2026 Huawei Technologies Co., Ltd. # This program is free software; you can redistribute it and/or modify it under the terms and conditions of # CANN Open Software License Agreement Version 2.0 (the License). # Please refer to the License for details. You may not use this file except in compliance with the License. # THIS SOFTWARE IS PROVIDED ON AN AS IS BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. # See LICENSE in the root of the software repository for the full text of the License. # ----------------------------------------------------------------------------------------------------------Python 脚本.py# ---------------------------------------------------------------------------------------------------------- # Copyright (c) 2026 Huawei Technologies Co., Ltd. # This program is free software; you can redistribute it and/or modify it under the terms and conditions of # CANN Open Software License Agreement Version 2.0 (the License). # Please refer to the License for details. You may not use this file except in compliance with the License. # THIS SOFTWARE IS PROVIDED ON AN AS IS BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. # See LICENSE in the root of the software repository for the full text of the License. # ----------------------------------------------------------------------------------------------------------2. Python 日志规范强制Python 脚本中禁止使用print输出日志必须使用logging模块import logging logging.basicConfig( levellogging.INFO, format%(asctime)s [%(levelname)s] %(message)s ) logger logging.getLogger(__name__) logger.info(操作成功: %s, result) logger.warning(非预期情况: %s, detail) logger.error(操作失败: %s, error_msg)原因print无法控制日志级别、无法格式化时间戳、无法按模块过滤不利于问题排查。3. Python 函数体行数限制强制main()函数体不得超过 50 行不含空行和注释其他函数建议不超过 30 行。超过时必须拆分为子函数。原因超大函数可读性差难以测试和维护。# ❌ main 函数过长 def main(): # 80 行代码... # ✅ 拆分为子函数 def parse_args(): ... def load_comments(args): ... def post_all_comments(config, comments, pos_maps): ... def main(): args parse_args() config build_config(args) comments load_comments(args) pos_maps build_position_maps(config) success, failed post_all_comments(config, comments, pos_maps) logger.info(发布完成: 成功 %d, 失败 %d, success, failed) sys.exit(0 if failed 0 else 1)4. 异常转换保留原始调用栈强制对异常做类型转换时必须使用raise ... from e保留原始异常的调用栈信息禁止直接raise新异常导致原始堆栈丢失。# ❌ 丢失原始异常调用栈 try: line int(parts[1]) except ValueError: raise ValueError(fInvalid line number: {parts[1]}) # ✅ 保留原始异常调用栈 try: line int(parts[1]) except ValueError as e: raise ValueError(fInvalid line number: {parts[1]}) from e5. Python 函数参数封装建议当函数参数个数较多≥4 个且参数之间存在逻辑相关性时建议通过具名形式封装# ❌ 参数过多调用时容易传错顺序 def post_comment(token, repo, pr_number, file_path, line, body): ... # ✅ 使用 dataclass 封装相关参数 from dataclasses import dataclass dataclass class CommentRequest: token: str repo: str pr_number: int file_path: str line: int body: str def post_comment(req: CommentRequest): ...适用场景参数 ≥4 个参数之间存在逻辑分组如 API 认证参数、文件定位参数同一组参数在多个函数间传递可选封装方式dataclass推荐、namedtuple、普通class【免费下载链接】ops-sparse本项目是CANN提供的高性能稀疏矩阵计算的算子库专注于优化稀疏矩阵的计算效率。项目地址: https://gitcode.com/cann/ops-sparse创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考