Commit 65f39cd8 authored by Michael Büsch's avatar Michael Büsch

util: Add clamp() helper to clamp value ranges

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent c82f43c4
......@@ -67,6 +67,7 @@ __all__ = [
"listIndex",
"listToHumanStr",
"listExpand",
"clamp",
"math_gcd",
"math_lcm",
"nopContext",
......@@ -426,6 +427,14 @@ def listExpand(lst, expander):
ret.extend(expander(item))
return ret
def clamp(value, minValue, maxValue):
"""Clamp value to the range minValue-maxValue.
ValueError is raised, if minValue is bigger than maxValue.
"""
if minValue > maxValue:
raise ValueError
return max(min(value, maxValue), minValue)
# Get "Greatest Common Divisor"
def math_gcd(*args):
return reduce(compat_gcd, args)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment