P2PySonar2demo_project/pysonar_demo/syntax.pyView source
pysonar_demo/syntax.pyStatic analysis
Walrus bindings, match captures, typed comprehensions, and modern parameters.28 definitions41 references
   1 """Modern syntax examples that preserve bindings and inferred values."""
   2
   3 from .models import Market
   4
   5
   6 def classify_market(market: Market, /, *, threshold: float = 0.5) -> str:
   7     if (label := market.liquidity_label()) and market.yes_price >= threshold:
   8         match {"label": label, "active": True}:
   9             case {"label": captured, "active": True}:
  10                 return captured.upper()
  11     return "WATCH"
  12
  13
  14 def visible_market_names(markets: list[Market]) -> list[str]:
  15     return [
  16         market.display_name
  17         for market in markets
  18         if market.volume > 0
  19     ]
  20
  21
  22 def iter_prices(markets: list[Market]):
  23     for market in markets:
  24         yield market.yes_price
  25
  26
  27 def unpack_prices(prices: list[float]) -> tuple[float, list[float]]:
  28     match prices:
  29         case [first, *remaining]:
  30             return first, remaining
  31     return 0.0, []
  32
  33
  34 class LabelContext:
  35     def __enter__(self) -> str:
  36         return "ACTIVE"
  37
  38     def __exit__(self, exc_type, exc, traceback) -> bool:
  39         return False
  40
  41
  42 def context_label() -> str:
  43     with LabelContext() as label:
  44         return label