Parse sampling rate string to microseconds.
(rate_str: str)
| 318 | |
| 319 | |
| 320 | def _parse_sampling_rate(rate_str: str) -> int: |
| 321 | """Parse sampling rate string to microseconds.""" |
| 322 | rate_str = rate_str.strip().lower() |
| 323 | |
| 324 | match = _RATE_PATTERN.match(rate_str) |
| 325 | if not match: |
| 326 | raise argparse.ArgumentTypeError( |
| 327 | f"Invalid sampling rate format: {rate_str}. " |
| 328 | "Expected: number followed by optional suffix (hz, khz, k) with no spaces (e.g., 10khz)" |
| 329 | ) |
| 330 | |
| 331 | number_part = match.group(1) |
| 332 | suffix = match.group(2) or '' |
| 333 | |
| 334 | # Determine multiplier based on suffix |
| 335 | suffix_map = { |
| 336 | 'hz': 1, |
| 337 | 'khz': 1000, |
| 338 | 'k': 1000, |
| 339 | } |
| 340 | multiplier = suffix_map.get(suffix, 1) |
| 341 | hz = float(number_part) * multiplier |
| 342 | if hz <= 0: |
| 343 | raise argparse.ArgumentTypeError(f"Sampling rate must be positive: {rate_str}") |
| 344 | |
| 345 | interval_usec = int(MICROSECONDS_PER_SECOND / hz) |
| 346 | if interval_usec < 1: |
| 347 | raise argparse.ArgumentTypeError(f"Sampling rate too high: {rate_str}") |
| 348 | |
| 349 | return interval_usec |
| 350 | |
| 351 | |
| 352 | def _add_sampling_options(parser): |