通用工具类
| 7 | |
| 8 | |
| 9 | class CommonUtil: |
| 10 | """通用工具类""" |
| 11 | |
| 12 | @staticmethod |
| 13 | def file_to_bytes(file_path): |
| 14 | """读取文件为字节数组""" |
| 15 | with open(file_path, 'rb') as f: |
| 16 | return f.read() |
| 17 | |
| 18 | @staticmethod |
| 19 | def bytes_to_file(data, file_path): |
| 20 | """将字节数组写入文件""" |
| 21 | with open(file_path, 'wb') as f: |
| 22 | f.write(data) |
| 23 | |
| 24 | @staticmethod |
| 25 | def stream_to_bytes(stream): |
| 26 | """将流转换为字节数组""" |
| 27 | if isinstance(stream, bytes): |
| 28 | return stream |
| 29 | if hasattr(stream, 'read'): |
| 30 | return stream.read() |
| 31 | return bytes(stream) |
| 32 | |
| 33 | @staticmethod |
| 34 | def password_hash(password): |
| 35 | """计算密码哈希值""" |
| 36 | return int(hashlib.sha256(password.encode()).hexdigest()[:16], 16) |
| 37 | |
| 38 | @staticmethod |
| 39 | def get_file_extension(filename): |
| 40 | """获取文件扩展名""" |
| 41 | if not filename: |
| 42 | return "" |
| 43 | parts = filename.rsplit('.', 1) |
| 44 | return parts[1].lower() if len(parts) > 1 else "" |
| 45 |
nothing calls this directly
no outgoing calls
no test coverage detected