Use a tiny clamp helper to clamp the 'on_progress' value

Add a misc.clamp function that will clamp a value to a given
range (it can also call a callback if clamping occurs). Use it
to clamp the progress value that was previously clamped with
a set of customized logic that can now be replaced with a more
generalized logic that can be shared.

Change-Id: I8369dbb61f73a60932d9e15c8b4d06db249ea38e
This commit is contained in:
Joshua Harlow
2014-12-12 23:03:12 -08:00
parent 97dd12e3e0
commit cdfd8ece61
3 changed files with 52 additions and 7 deletions

View File

@@ -100,6 +100,22 @@ def parse_uri(uri):
query=split.query)
def clamp(value, minimum, maximum, on_clamped=None):
"""Clamps a value to ensure its >= minimum and <= maximum."""
if minimum > maximum:
raise ValueError("Provided minimum '%s' must be less than or equal to"
" the provided maximum '%s'" % (minimum, maximum))
if value > maximum:
value = maximum
if on_clamped is not None:
on_clamped()
if value < minimum:
value = minimum
if on_clamped is not None:
on_clamped()
return value
def binary_encode(text, encoding='utf-8'):
"""Converts a string of into a binary type using given encoding.