Somewhat improve prettiness of hyphen range parse code.
This commit is contained in:
parent
6dcf47cab7
commit
5d7bc1ff4b
1 changed files with 23 additions and 19 deletions
|
@ -243,20 +243,23 @@ def hyphen_range(s):
|
||||||
s = s.replace(' ', '')
|
s = s.replace(' ', '')
|
||||||
if not s:
|
if not s:
|
||||||
return (None, None)
|
return (None, None)
|
||||||
|
|
||||||
parts = s.split('-')
|
parts = s.split('-')
|
||||||
parts = [part.strip() or None for part in parts]
|
parts = [part.strip() or None for part in parts]
|
||||||
|
|
||||||
if len(parts) == 1:
|
if len(parts) == 1:
|
||||||
low = parts[0]
|
(low, high) = (parts[0], None)
|
||||||
high = None
|
|
||||||
elif len(parts) == 2:
|
elif len(parts) == 2:
|
||||||
(low, high) = parts
|
(low, high) = parts
|
||||||
else:
|
else:
|
||||||
raise ValueError('Too many hyphens')
|
raise ValueError('Too many hyphens')
|
||||||
|
|
||||||
low = _unitconvert(low)
|
low = parse_unit_string(low)
|
||||||
high = _unitconvert(high)
|
high = parse_unit_string(high)
|
||||||
|
|
||||||
if low is not None and high is not None and low > high:
|
if low is not None and high is not None and low > high:
|
||||||
raise exceptions.OutOfOrder(range=s, min=low, max=high)
|
raise exceptions.OutOfOrder(range=s, min=low, max=high)
|
||||||
|
|
||||||
return low, high
|
return low, high
|
||||||
|
|
||||||
def hms_to_seconds(hms):
|
def hms_to_seconds(hms):
|
||||||
|
@ -290,6 +293,22 @@ def now(timestamp=True):
|
||||||
return n.timestamp()
|
return n.timestamp()
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
def parse_unit_string(s):
|
||||||
|
'''
|
||||||
|
Try to parse the string as a float, or bytestring, or hms.
|
||||||
|
'''
|
||||||
|
if s is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if ':' in s:
|
||||||
|
return hms_to_seconds(s)
|
||||||
|
|
||||||
|
elif all(c in '0123456789.' for c in s):
|
||||||
|
return float(s)
|
||||||
|
|
||||||
|
else:
|
||||||
|
return bytestring.parsebytes(s)
|
||||||
|
|
||||||
def random_hex(length=12):
|
def random_hex(length=12):
|
||||||
randbytes = os.urandom(math.ceil(length / 2))
|
randbytes = os.urandom(math.ceil(length / 2))
|
||||||
token = ''.join('{:02x}'.format(x) for x in randbytes)
|
token = ''.join('{:02x}'.format(x) for x in randbytes)
|
||||||
|
@ -547,18 +566,3 @@ def zip_photos(photos):
|
||||||
zipfile.write(filename=photo.real_path.absolute_path, arcname=arcname)
|
zipfile.write(filename=photo.real_path.absolute_path, arcname=arcname)
|
||||||
|
|
||||||
return zipfile
|
return zipfile
|
||||||
|
|
||||||
_numerical_characters = set('0123456789.')
|
|
||||||
def _unitconvert(value):
|
|
||||||
'''
|
|
||||||
When parsing hyphenated ranges, this function is used to convert
|
|
||||||
strings like "1k" to 1024 and "1:00" to 60.
|
|
||||||
'''
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
if ':' in value:
|
|
||||||
return hms_to_seconds(value)
|
|
||||||
elif all(c in _numerical_characters for c in value):
|
|
||||||
return float(value)
|
|
||||||
else:
|
|
||||||
return bytestring.parsebytes(value)
|
|
||||||
|
|
Loading…
Reference in a new issue