Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

__init__.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import base64
  17. import functools
  18. import hashlib
  19. import time
  20. from pathlib import Path
  21. def encode_avatar(image_path):
  22. with Path.open(image_path, "rb") as file:
  23. binary_data = file.read()
  24. base64_encoded = base64.b64encode(binary_data).decode("utf-8")
  25. return base64_encoded
  26. def compare_by_hash(file1, file2, algorithm="sha256"):
  27. def _calc_hash(file_path):
  28. hash_func = hashlib.new(algorithm)
  29. with open(file_path, "rb") as f:
  30. while chunk := f.read(8192):
  31. hash_func.update(chunk)
  32. return hash_func.hexdigest()
  33. return _calc_hash(file1) == _calc_hash(file2)
  34. def wait_for(timeout=10, interval=1, error_msg="Timeout"):
  35. def decorator(func):
  36. @functools.wraps(func)
  37. def wrapper(*args, **kwargs):
  38. start_time = time.time()
  39. while True:
  40. result = func(*args, **kwargs)
  41. if result is True:
  42. return result
  43. elapsed = time.time() - start_time
  44. if elapsed > timeout:
  45. assert False, error_msg
  46. time.sleep(interval)
  47. return wrapper
  48. return decorator
  49. def is_sorted(data, field, descending=True):
  50. timestamps = [ds[field] for ds in data]
  51. return all(a >= b for a, b in zip(timestamps, timestamps[1:])) if descending else all(a <= b for a, b in zip(timestamps, timestamps[1:]))