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) proxy = proxies.get(typ, None)
if proxy: if proxy:
pattern = re.compile(( pattern = re.compile((
'(?:ptype://)?' \ '(?:ptype://)?'
'(?:(?P<user>\w+):(?P<pass>.*)@)?' \ '(?:(?P<user>\w+):(?P<pass>.*)@)?'
'(?P<host>[\w\-\.]+)' \ '(?P<host>[\w\-\.]+)'
'(?::(?P<port>\d+))?').replace('ptype', typ) '(?::(?P<port>\d+))?').replace('ptype', typ)
) )
@ -535,7 +535,7 @@ def entity_to_unicode(match, exceptions=[], encoding='cp1252',
ent = match.group(1) ent = match.group(1)
if ent in exceptions: if ent in exceptions:
return '&'+ent+';' 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("'") return check("'")
if ent == 'hellips': if ent == 'hellips':
ent = 'hellip' ent = 'hellip'
@ -565,7 +565,7 @@ def entity_to_unicode(match, exceptions=[], encoding='cp1252',
return '&'+ent+';' return '&'+ent+';'
_ent_pat = re.compile(r'&(\S+?);') _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;', '"' : '&quot;',
"'" : '&apos;', "'" : '&apos;',
'<' : '&lt;', '<' : '&lt;',
@ -670,8 +670,8 @@ def human_readable(size, sep=' '):
""" Convert a size in bytes into a human readable form """ """ Convert a size in bytes into a human readable form """
divisor, suffix = 1, "B" divisor, suffix = 1, "B"
for i, candidate in enumerate(('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')): for i, candidate in enumerate(('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')):
if size < 1024**(i+1): if size < (1 << ((i + 1) * 10)):
divisor, suffix = 1024**(i), candidate divisor, suffix = (1 << (i * 10)), candidate
break break
size = str(float(size)/divisor) size = str(float(size)/divisor)
if size.find(".") > -1: if size.find(".") > -1: