/ PyCrypCli / PyCrypCli / util.py
util.py
 1  import re
 2  from datetime import datetime, timezone
 3  from typing import Any, Sequence
 4  
 5  
 6  def is_uuid(x: str) -> bool:
 7      return bool(re.match(r"^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$", x))
 8  
 9  
10  def extract_wallet(content: str) -> tuple[str, str] | None:
11      if re.match(r"^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} [0-9a-f]{10}$", content):
12          uuid, key = content.split()
13          return uuid, key
14      return None
15  
16  
17  def utc_to_local(timestamp: datetime) -> datetime:
18      return timestamp.replace(tzinfo=timezone.utc).astimezone().replace(tzinfo=None)
19  
20  
21  def strip_float(num: float, precision: int) -> str:
22      return f"{num:.{precision}f}".rstrip("0").rstrip(".")
23  
24  
25  def print_tree(items: Sequence[tuple[str, Sequence[Any] | None]], indent: list[bool] | None = None) -> None:
26      if not indent:
27          indent = []
28      for i, (item, children) in enumerate(items):
29          branch = "└├"[i < len(items) - 1]
30          print("".join(" │"[ind] + "   " for ind in indent) + branch + "── " + item)
31          if children is not None:
32              print_tree(children, indent + [i < len(items) - 1])