Coverage for .claude/hooks/moai/lib/common.py: 100.00%

14 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-19 08:00 +0900

1"""Common utility functions for MoAI hooks 

2 

3Consolidated fallback implementations used across multiple hooks. 

4""" 

5 

6import statistics 

7from typing import List, Dict 

8 

9 

10def format_duration(seconds: float) -> str: 

11 """Format duration in seconds to readable string. 

12 

13 Converts seconds to human-readable format (s, m, h). 

14 

15 Args: 

16 seconds: Duration in seconds 

17 

18 Returns: 

19 Formatted duration string (e.g., "2.5m", "1.3h") 

20 """ 

21 if seconds < 60: 

22 return f"{seconds:.1f}s" 

23 minutes = seconds / 60 

24 if minutes < 60: 

25 return f"{minutes:.1f}m" 

26 hours = minutes / 60 

27 return f"{hours:.1f}h" 

28 

29 

30def get_summary_stats(values: List[float]) -> Dict[str, float]: 

31 """Get summary statistics for a list of values. 

32 

33 Calculates mean, min, max, and standard deviation. 

34 

35 Args: 

36 values: List of numeric values 

37 

38 Returns: 

39 Dictionary with keys: mean, min, max, std 

40 """ 

41 if not values: 

42 return {"mean": 0, "min": 0, "max": 0, "std": 0} 

43 

44 return { 

45 "mean": statistics.mean(values), 

46 "min": min(values), 

47 "max": max(values), 

48 "std": statistics.stdev(values) if len(values) > 1 else 0 

49 }