minor PEP8 cleanup (" -> ')

This commit is contained in:
Overwatch 2014-08-19 16:29:56 +02:00
parent 6416b66f6e
commit 61fb3b1044
18 changed files with 456 additions and 456 deletions

View file

@ -12,19 +12,19 @@ import time
# --------------------------------------------------
class ProgressBar:
"""
'''
An animated progress bar.
TODO derive Wait() from this
"""
'''
def __init__(self, width, top, unit, reverse=False):
"""
width width of the "widget" including all text
'''
width width of the 'widget' including all text
top the 100% value
unit name of the unit to display
reverse True to expand the progress bar from right to left
"""
'''
self.width = width
self.value = 0
@ -40,7 +40,7 @@ class ProgressBar:
self.t_start = time.time()
if self.old_len:
sys.stderr.write("\b" * self.old_len)
sys.stderr.write('\b' * self.old_len)
transferred = Unit(self.value, self.unit)
dt = time.time() - self.t_start
@ -50,18 +50,18 @@ class ProgressBar:
else:
speed = 0.0
speed = Unit(speed, "%s/s" %(self.unit))
speed = Unit(speed, '%s/s' %(self.unit))
ratio = self.value / self.top
pb_done = "=" * int(self.width * ratio)
pb_rem = " " * int(self.width * (1 - ratio))
symbol = ">"
pb_done = '=' * int(self.width * ratio)
pb_rem = ' ' * int(self.width * (1 - ratio))
symbol = '>'
if self.reverse:
symbol = "<"
symbol = '<'
pb_done, pb_rem = pb_rem, pb_done
text = "%s [%s%s%s] %s" %(transferred, pb_done, symbol, pb_rem, speed)
text = '%s [%s%s%s] %s' %(transferred, pb_done, symbol, pb_rem, speed)
sys.stderr.write(text)
@ -70,8 +70,8 @@ class ProgressBar:
self.old_len = current_len
if tail > 0:
sys.stderr.write(" " * tail)
sys.stderr.write("\b" * tail)
sys.stderr.write(' ' * tail)
sys.stderr.write('\b' * tail)
sys.stderr.flush()
@ -81,46 +81,46 @@ class ProgressBar:
self.draw()
def blank(self):
sys.stderr.write("\r" + self.old_len * " " + "\r")
sys.stderr.write('\r' + self.old_len * ' ' + '\r')
sys.stderr.flush()
# --------------------------------------------------
class Unit:
"""
'''
A object that represents numbers and units in human-readable form.
TODO use significant digits instead of rigid order boundaries
TODO float superclass?
TODO base_2 prefixes (Ki, Mi, ...)
"""
'''
_prefixes = (
("Y", 24), ("Z", 21), ("E", 18), ("P", 15), ("T", 12), ("G", 9), ("M", 6), ("k", 3),
("h", 2), ("D", 1), ("", 0), ("d", -1), ("c", -2), ("m", -3), ("μ", -6), ("n", -9),
("p", -12), ("f", -15), ("a", -18), ("z", -21), ("y", -24)
('Y', 24), ('Z', 21), ('E', 18), ('P', 15), ('T', 12), ('G', 9), ('M', 6), ('k', 3),
('h', 2), ('D', 1), ('', 0), ('d', -1), ('c', -2), ('m', -3), ('μ', -6), ('n', -9),
('p', -12), ('f', -15), ('a', -18), ('z', -21), ('y', -24)
)
def __init__(self,
value, unit=None, dimension=1,
use_prefixes="YZEPTGMkmμnpfazy", format="%.2f pU",
use_prefixes='YZEPTGMkmμnpfazy', format='%.2f pU',
logarithmic=False, log_base=10
):
"""
'''
value the numerical value of the variable (int or float)
unit the symbol to use, if any (str or None)
dimension the dimensionality of the value (1, 2, 3, ...)
use_prefixes which multiplier prefixes to use
- the default "YZEPTGMkmμnpfazy" omits "c" for centi- and "D" for deca-
- the default 'YZEPTGMkmμnpfazy' omits 'c' for centi- and 'D' for deca-
format use printf notation for value (e.g. %010.5f), p for prefix and U for unit
logarithmic when True, log_10 prefixes are used
log_base logarithm base value
note that deca- is correctly rendered as "da", the "D" is used in use_prefixes only
"""
note that deca- is correctly rendered as 'da', the 'D' is used in use_prefixes only
'''
self.value = float(value)
self.unit = unit if unit else ""
self.unit = unit if unit else ''
self.dimension = dimension
self.use_prefixes = use_prefixes
self.format = format
@ -128,7 +128,7 @@ class Unit:
self.log_base = log_base
if self.logarithmic and (self.value < 0 or self.log_base < 0):
raise ValueError("math domain error (negative values can't be represented in dB)")
raise ValueError('math domain error (negative values can\'t be represented in dB)')
def __str__(self):
if self.value == 0.0:
@ -137,7 +137,7 @@ class Unit:
e = round(math.log(abs(self.value), 10), 6)
if self.logarithmic:
prefix = "dB"
prefix = 'dB'
value = math.log(self.value, self.log_base) * 10
else:
@ -148,18 +148,18 @@ class Unit:
value = self.value / 10**(mul*self.dimension)
output = self.format %(value)
output = output.replace("p", prefix if prefix != "D" else "da") # deca- handler
output = output.replace("U", self.unit)
output = output.replace('p', prefix if prefix != 'D' else 'da') # deca- handler
output = output.replace('U', self.unit)
return output
def __repr__(self):
return "over.core.textui.Unit(%s)" %(self)
return 'over.core.textui.Unit(%s)' %(self)
# --------------------------------------------------
def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
"""
'''
str text text to format
int width required line length; if 0, current terminal width will be used;
- if negative, current terminal width minus supplied amount will be used
@ -169,7 +169,7 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
Formats text into an indented paragraph that fits the terminal and returns it.
Correctly handles color tags.
"""
'''
#words = [x.strip() for x in text.split() if x.strip()]
words = text.split()
@ -187,9 +187,9 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
while words:
if indent:
if first and stamp:
lines.append([stamp + " "*(indent-1-len(stamp))])
lines.append([stamp + ' '*(indent-1-len(stamp))])
else:
lines.append([" "*(indent-1)]) # first word = indent minus one space (that's gonna get back while joining)
lines.append([' '*(indent-1)]) # first word = indent minus one space (that's gonna get back while joining)
first = False
else:
@ -198,16 +198,16 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
while words:
word_added = False
if len(re.sub("§.", "", ' '.join(lines[-1]))) + len(re.sub("§.", "", words[0])) + 1 <= width:
if len(re.sub('§.', '', ' '.join(lines[-1]))) + len(re.sub('§.', '', words[0])) + 1 <= width:
lines[-1].append(words.pop(0))
word_added = True
elif not word_added and len(lines[-1]) == 1 and indent:
# no word added and just the indent's in here = word's too long -> screw indent
# we might try to keep at least a part of the indent - if possible
len_word = len(re.sub("§.", "", words[0]))
len_word = len(re.sub('§.', '', words[0]))
if len_word < width:
lines[-1] = [" "*(width - len_word - 1), words.pop(0)]
lines[-1] = [' '*(width - len_word - 1), words.pop(0)]
else:
lines[-1] = [words.pop(0)]
word_added = True
@ -234,13 +234,13 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# --------------------------------------------------
# def strtrim(text, length, mode=0, dots=True, colors=True):
# """
# '''
# str text text to trim
# int length desired length, >1
# int mode -1 = cut chars from the left, 0 = from the middle, 1 = from the right
# bool dots add an ellipsis to the point of cutting
# bool colors dont count color tags into length; also turns the ellipsis blue
# """
# '''
# if len(text) <= length:
# return text
@ -253,7 +253,7 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# if mode == -1:
# if dots:
# return "..." + cut(text, length, 1, colors)
# return '...' + cut(text, length, 1, colors)
# else:
# return cut(text, length, 1, colors)
@ -266,27 +266,27 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# part2 = cut(text, length/2, 1, colors)
# if dots:
# part1 += "..."
# part1 += '...'
# return part1 + part2
# else:
# if dots:
# return cut(text, length, 0, colors) + "..."
# return cut(text, length, 0, colors) + '...'
# else:
# return cut(text, length, 0, colors)
# --------------------------------------------------
# def textfilter(text, set=None):
# """
# '''
# str text text to filter
# dict set {to_replace: replacement}
# Text filter that replaces occurences of to_replace keys with their respective values.
# Defaults to filtering of 'bad' characters if no translational dictionary is provided.
# """
# '''
# if not set:
# set = badchars
@ -299,14 +299,14 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# --------------------------------------------------
# def cut(text, size, end=0, colors=True):
# """
# '''
# str text text to cut
# int size how many chars to return, >=0
# int end return chars from 0 = left, 1 = right
# bool colors skip color tags
# """
# '''
# out = ""
# out = ''
# strlen = len(text)
@ -317,7 +317,7 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# while size:
# if end == 0:
# if colors and text[ptr] == "<" and strlen - ptr >= 4 and text[ptr+1] == "C" and text[ptr+3] == ">": # we have a tag
# if colors and text[ptr] == '<' and strlen - ptr >= 4 and text[ptr+1] == 'C' and text[ptr+3] == '>': # we have a tag
# out += text[ptr:ptr+4]
# ptr += 4
# else:
@ -325,7 +325,7 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# ptr += 1
# size -= 1
# else:
# if colors and text[ptr] == ">" and ptr >= 4 and text[ptr-2] == "C" and text[ptr-3] == "<": # we have a tag
# if colors and text[ptr] == '>' and ptr >= 4 and text[ptr-2] == 'C' and text[ptr-3] == '<': # we have a tag
# out = text[ptr-3:ptr+1] + out
# ptr -= 4
# else:
@ -342,31 +342,31 @@ def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
# --------------------------------------------------
class prefix:
info = (" ", "INFO")
debug = (" §b?§/", "§bDEBG§/")
start = ("§B>>>§/", "§BEXEC§/")
info = (' ', 'INFO')
debug = (' §b?§/', '§bDEBG§/')
start = ('§B>>>§/', '§BEXEC§/')
exec = start
warn = (" §y#§/", "§yWARN§/")
fail = ("§r!!!§/", "§rFAIL§/")
done = (" §g*§/", "§gDONE§/")
warn = (' §y#§/', '§yWARN§/')
fail = ('§r!!!§/', '§rFAIL§/')
done = (' §g*§/', '§gDONE§/')
colortags = {
"§r": "\x1b[31;01m",
"§g": "\x1b[32;01m",
"§y": "\x1b[33;01m",
"§b": "\x1b[34;01m",
"§m": "\x1b[35;01m",
"§c": "\x1b[36;01m",
"§B": "\x1b[01m",
"§/": "\x1b[39;49;00m"
'§r': '\x1b[31;01m',
'§g': '\x1b[32;01m',
'§y': '\x1b[33;01m',
'§b': '\x1b[34;01m',
'§m': '\x1b[35;01m',
'§c': '\x1b[36;01m',
'§B': '\x1b[01m',
'§/': '\x1b[39;49;00m'
}
def render(text, colors=True):
"""
'''
Processes text with color tags and either
removes them (with colors=False) or replaces
them with terminal color codes.
"""
'''
text = str(text)
@ -389,9 +389,9 @@ def render(text, colors=True):
# --------------------------------------------------
def char_length(string):
"""
'''
Returns the length of a string minus all formatting tags.
"""
'''
plain_string = render(string, colors=False)
@ -400,7 +400,7 @@ def char_length(string):
# --------------------------------------------------
class Output:
"""
'''
Text UI output renderer.
Prints messages to the stdout with optional eye candy
@ -408,18 +408,18 @@ class Output:
Usage:
>>> from over import Output, prefix
>>> say = Output("test", timestamp=True)
>>> say("system initialized")
>>> say = Output('test', timestamp=True)
>>> say('system initialized')
[2013-02-28 16:41:28] INFO -- test, system initialized
>>> say("system is FUBAR", prefix.fail)
>>> say('system is FUBAR', prefix.fail)
[2013-02-28 16:41:46] FAIL -- test, system is FUBAR
>>> say("I just realized this will not work", prefix.fail, timestamp=False)
>>> say('I just realized this will not work', prefix.fail, timestamp=False)
!!! I just realized this will not work
TODO use a generic format string
"""
'''
def __init__(self, name, timestamp=True, colors=True, default_prefix=prefix.info, default_suffix=".\n", stream=sys.stdout):
def __init__(self, name, timestamp=True, colors=True, default_prefix=prefix.info, default_suffix='.\n', stream=sys.stdout):
self.name = name
self.timestamp = timestamp
self.colors = colors
@ -449,13 +449,13 @@ class Output:
if timestamp:
output.append(time.strftime('[%Y-%m-%d %H:%M:%S] '))
output.append(prefix[1])
output.append(" -- ")
output.append(' -- ')
elif prefix:
output.append(prefix[0])
output.append(" ")
output.append(' ')
if display_name and self.name:
output.append("%s, " %(self.name))
output.append('%s, ' %(self.name))
#output.append(paragraph(str(text), indent=indent))
output.append(str(text))
@ -463,7 +463,7 @@ class Output:
if suffix:
output.append(suffix)
output = "".join(output)
output = ''.join(output)
self.stream.write(render(output, colors))
self.stream.flush()
@ -471,12 +471,12 @@ class Output:
# --------------------------------------------------
def get_terminal_size():
"""
'''
Returns current terminal's (rows, cols).
"""
'''
terminal = sys.stdout.fileno()
try:
return struct.unpack("HHHH", fcntl.ioctl(terminal, termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0)))
return struct.unpack('HHHH', fcntl.ioctl(terminal, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0)))
except IOError:
return (40, 80)