rename safe_int to parse_int; move parse_* to text module
This commit is contained in:
@@ -9,8 +9,8 @@
|
||||
"""Collection of functions that work in strings/text"""
|
||||
|
||||
import re
|
||||
import os.path
|
||||
import html
|
||||
import os.path
|
||||
import urllib.parse
|
||||
|
||||
|
||||
@@ -125,6 +125,35 @@ def extract_iter(txt, begin, end, pos=0):
|
||||
yield value
|
||||
|
||||
|
||||
def parse_bytes(value, default=0, suffixes="bkmgtp"):
|
||||
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
|
||||
try:
|
||||
last = value[-1].lower()
|
||||
except (TypeError, KeyError, IndexError):
|
||||
return default
|
||||
|
||||
if last in suffixes:
|
||||
mul = 1024 ** suffixes.index(last)
|
||||
value = value[:-1]
|
||||
else:
|
||||
mul = 1
|
||||
|
||||
try:
|
||||
return round(float(value) * mul)
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
|
||||
def parse_int(value, default=0):
|
||||
"""Convert 'value' to int"""
|
||||
if not value:
|
||||
return default
|
||||
try:
|
||||
return int(value)
|
||||
except (ValueError, TypeError):
|
||||
return default
|
||||
|
||||
|
||||
def parse_query(qs):
|
||||
"""Parse a query string into key-value pairs"""
|
||||
result = {}
|
||||
@@ -142,6 +171,7 @@ if os.name == "nt":
|
||||
else:
|
||||
clean_path = clean_path_posix
|
||||
|
||||
urljoin = urllib.parse.urljoin
|
||||
unquote = urllib.parse.unquote
|
||||
escape = html.escape
|
||||
|
||||
|
||||
Reference in New Issue
Block a user