cleanup pylint errors and py2/3 issues

This commit is contained in:
j 2016-06-08 15:32:46 +02:00
commit 77f8876fca
20 changed files with 232 additions and 197 deletions

View file

@ -4,13 +4,14 @@ import math
import re
import string
from six import text_type
def toAZ(num):
"""
Converts an integer to bijective base 26 string using A-Z
>>> for i in range(1, 1000): assert fromAZ(toAZ(i)) == i
>>> toAZ(1)
'A'
@ -20,7 +21,8 @@ def toAZ(num):
>>> toAZ(1234567890)
'CYWOQVJ'
"""
if num < 1: raise ValueError("must supply a positive integer")
if num < 1:
raise ValueError("must supply a positive integer")
digits = string.ascii_uppercase
az = ''
while num != 0:
@ -30,7 +32,7 @@ def toAZ(num):
az = digits[r] + az
return az
encode_base26=toAZ
encode_base26 = toAZ
def fromAZ(num):
"""
@ -45,7 +47,7 @@ def fromAZ(num):
>>> fromAZ('FOO')
4461
"""
num = num.replace('-','')
num = num.replace('-', '')
digits = string.ascii_uppercase
r = 0
for exp, char in enumerate(reversed(num)):
@ -64,7 +66,8 @@ def to26(q):
>>> to26(347485647)
'BDGKMAP'
"""
if q < 0: raise ValueError("must supply a positive integer")
if q < 0:
raise ValueError("must supply a positive integer")
base26 = string.ascii_uppercase
converted = []
while q != 0:
@ -73,7 +76,7 @@ def to26(q):
converted.insert(0, l)
return "".join(converted) or 'A'
decode_base26=toAZ
decode_base26 = toAZ
def from26(q):
"""
@ -82,7 +85,7 @@ def from26(q):
0
"""
base26 = string.ascii_uppercase
q = q.replace('-','')
q = q.replace('-', '')
r = 0
for i in q:
r = r * 26 + base26.index(i.upper())
@ -123,7 +126,8 @@ def to32(q):
ValueError: must supply a positive integer
"""
if q < 0: raise ValueError("must supply a positive integer")
if q < 0:
raise ValueError("must supply a positive integer")
letters = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
converted = []
while q != 0:
@ -188,7 +192,7 @@ def from32(q):
'Z': 31,
}
base32 = ('0123456789' + string.ascii_uppercase)[:32]
q = q.replace('-','')
q = q.replace('-', '')
q = ''.join([base32[_32map[i.upper()]] for i in q])
return int(q, 32)
@ -210,7 +214,8 @@ def to36(q):
...
ValueError: must supply a positive integer
"""
if q < 0: raise ValueError("must supply a positive integer")
if q < 0:
raise ValueError("must supply a positive integer")
letters = "0123456789abcdefghijklmnopqrstuvwxyz"
converted = []
while q != 0:
@ -233,7 +238,7 @@ def int_value(strValue, default=u''):
u''
"""
try:
val = re.compile('(\d+)').findall(unicode(strValue).strip())[0]
val = re.compile('(\d+)').findall(text_type(strValue).strip())[0]
except:
val = default
return val
@ -250,7 +255,7 @@ def float_value(strValue, default=u''):
u''
"""
try:
val = re.compile('([\d.]+)').findall(unicode(strValue).strip())[0]
val = re.compile('([\d.]+)').findall(text_type(strValue).strip())[0]
except:
val = default
return val
@ -286,7 +291,7 @@ def format_number(number, longName, shortName):
n = number / math.pow(1024, i + 1)
return '%s %s%s' % (format_thousands('%.*f' % (i, n)), prefix[i], shortName)
def format_thousands(number, separator = ','):
def format_thousands(number, separator=','):
"""
Return the number with separators (1,000,000)
@ -316,18 +321,18 @@ def format_pixels(number):
return format_number(number, 'pixel', 'px')
def format_currency(amount, currency="$"):
if amount:
temp = "%.2f" % amount
profile=re.compile(r"(\d)(\d\d\d[.,])")
while 1:
temp, count = re.subn(profile,r"\1,\2",temp)
if not count:
break
if temp.startswith('-'):
return "-"+ currency + temp[1:-3]
return currency + temp[:-3]
else:
return ""
if amount:
temp = "%.2f" % amount
profile = re.compile(r"(\d)(\d\d\d[.,])")
while 1:
temp, count = re.subn(profile, r"\1,\2", temp)
if not count:
break
if temp.startswith('-'):
return "-" + currency + temp[1:-3]
return currency + temp[:-3]
else:
return ""
def plural(amount, unit, plural='s'):
'''
@ -339,7 +344,8 @@ def plural(amount, unit, plural='s'):
if abs(amount) != 1:
if plural == 's':
unit = unit + plural
else: unit = plural
else:
unit = plural
return "%s %s" % (format_thousands(amount), unit)
def format_duration(ms, verbosity=0, years=True, hours=True, milliseconds=True):
@ -390,14 +396,14 @@ def format_duration(ms, verbosity=0, years=True, hours=True, milliseconds=True):
duration += ".%03d" % ms
else:
if verbosity == 1:
durations = ["%sd" % d, "%sh" % h, "%sm" % m, "%ss" % s]
durations = ["%sd" % d, "%sh" % h, "%sm" % m, "%ss" % s]
if years:
durations.insert(0, "%sy" % y)
if milliseconds:
durations.append("%sms" % ms)
else:
durations = [plural(d, 'day'), plural(h,'hour'),
plural(m, 'minute'), plural(s, 'second')]
durations = [plural(d, 'day'), plural(h, 'hour'),
plural(m, 'minute'), plural(s, 'second')]
if years:
durations.insert(0, plural(y, 'year'))
if milliseconds:
@ -434,7 +440,7 @@ def parse_timecode(string):
'''
timecode = 0
for i, v in enumerate(list(reversed(string.split(':')))[:4]):
timecode += float(v) * ( 86400 if i == 3 else pow(60, i))
timecode += float(v) * (86400 if i == 3 else pow(60, i))
return timecode
def ms2runtime(ms, shortenLong=False):
@ -482,7 +488,8 @@ def time2ms(timeString):
p = timeString.split(':')
for i in range(len(p)):
_p = p[i]
if _p.endswith('.'): _p =_p[:-1]
if _p.endswith('.'):
_p = _p[:-1]
ms = ms * 60 + float(_p)
return int(ms * 1000)