pep8 and small perf improvement for human_readable()

This commit is contained in:
Kovid Goyal 2013-06-28 12:44:44 +05:30
parent 6579327a6d
commit 54a9a7c98e

View File

@ -310,9 +310,9 @@ def get_parsed_proxy(typ='http', debug=True):
proxy = proxies.get(typ, None)
if proxy:
pattern = re.compile((
'(?:ptype://)?' \
'(?:(?P<user>\w+):(?P<pass>.*)@)?' \
'(?P<host>[\w\-\.]+)' \
'(?:ptype://)?'
'(?:(?P<user>\w+):(?P<pass>.*)@)?'
'(?P<host>[\w\-\.]+)'
'(?::(?P<port>\d+))?').replace('ptype', typ)
)
@ -535,7 +535,7 @@ def entity_to_unicode(match, exceptions=[], encoding='cp1252',
ent = match.group(1)
if ent in exceptions:
return '&'+ent+';'
if ent in {'apos', 'squot'}: # squot is generated by some broken CMS software
if ent in {'apos', 'squot'}: # squot is generated by some broken CMS software
return check("'")
if ent == 'hellips':
ent = 'hellip'
@ -565,7 +565,7 @@ def entity_to_unicode(match, exceptions=[], encoding='cp1252',
return '&'+ent+';'
_ent_pat = re.compile(r'&(\S+?);')
xml_entity_to_unicode = partial(entity_to_unicode, result_exceptions = {
xml_entity_to_unicode = partial(entity_to_unicode, result_exceptions={
'"' : '&quot;',
"'" : '&apos;',
'<' : '&lt;',
@ -670,8 +670,8 @@ def human_readable(size, sep=' '):
""" Convert a size in bytes into a human readable form """
divisor, suffix = 1, "B"
for i, candidate in enumerate(('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')):
if size < 1024**(i+1):
divisor, suffix = 1024**(i), candidate
if size < (1 << ((i + 1) * 10)):
divisor, suffix = (1 << (i * 10)), candidate
break
size = str(float(size)/divisor)
if size.find(".") > -1: