MCPcopy Index your code
hub / github.com/python/cpython / _parse_sampling_rate

Function _parse_sampling_rate

Lib/profiling/sampling/cli.py:320–349  ·  view source on GitHub ↗

Parse sampling rate string to microseconds.

(rate_str: str)

Source from the content-addressed store, hash-verified

318
319
320def _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
352def _add_sampling_options(parser):

Callers

nothing calls this directly

Calls 5

lowerMethod · 0.45
stripMethod · 0.45
matchMethod · 0.45
groupMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…